Skip to content

Commit

Permalink
Update edge_drawing.py
Browse files Browse the repository at this point in the history
Create edge_drawing_demo.cpp
Update ximgproc.bib
Update test_fld.cpp
add CV_WRAP to read & write functions of Params
a bug fixed
  • Loading branch information
sturkmen72 committed Jul 15, 2024
1 parent 00aeb08 commit 99fb0d3
Show file tree
Hide file tree
Showing 6 changed files with 326 additions and 49 deletions.
12 changes: 11 additions & 1 deletion modules/ximgproc/doc/ximgproc.bib
Original file line number Diff line number Diff line change
Expand Up @@ -406,4 +406,14 @@ @article{jia2017fast
pages={3665--3679},
year={2017},
publisher={IEEE}
}
}

@article{akinlar201782,
title = {ColorED: Color edge and segment detection by Edge Drawing (ED)},
author = {Cuneyt Akinlar and Cihan Topal},
journal = {Journal of Visual Communication and Image Representation},
volume = {44},
pages = {82-94},
year = {2017},
publisher={Academic Press}
}
6 changes: 3 additions & 3 deletions modules/ximgproc/include/opencv2/ximgproc/edge_drawing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace ximgproc
//! @addtogroup ximgproc_edge_drawing
//! @{

/** @brief Class implementing the ED (EdgeDrawing) @cite topal2012edge, EDLines @cite akinlar2011edlines, EDPF @cite akinlar2012edpf and EDCircles @cite akinlar2013edcircles algorithms
/** @brief Class implementing the ED (EdgeDrawing) @cite topal2012edge, EDLines @cite akinlar2011edlines, EDPF @cite akinlar2012edpf, EDCircles @cite akinlar2013edcircles and ColorED @cite akinlar201782 algorithms.
*/

class CV_EXPORTS_W EdgeDrawing : public Algorithm
Expand Down Expand Up @@ -66,8 +66,8 @@ class CV_EXPORTS_W EdgeDrawing : public Algorithm
//! Default value is 1.3
CV_PROP_RW double MaxErrorThreshold;

void read(const FileNode& fn);
void write(FileStorage& fs) const;
CV_WRAP void read(const FileNode& fn);
CV_WRAP void write(FileStorage& fs) const;
};

/** @brief Detects edges in a grayscale or color image and prepares them to detect lines and ellipses.
Expand Down
141 changes: 98 additions & 43 deletions modules/ximgproc/samples/edge_drawing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
#!/usr/bin/python

'''
This example illustrates how to use cv.ximgproc.EdgeDrawing class.
This example script illustrates how to use cv.ximgproc.EdgeDrawing class.
It uses the OpenCV library to load an image, and then use the EdgeDrawing class
to detect edges, lines, and ellipses. The detected features are then drawn and displayed.
The main loop allows the user changing parameters of EdgeDrawing by pressing following keys:
to toggle the grayscale conversion press 'space' key
to increase MinPathLength value press '/' key
to decrease MinPathLength value press '*' key
to increase MinLineLength value press '+' key
to decrease MinLineLength value press '-' key
to toggle NFAValidation value press 'n' key
to toggle PFmode value press 'p' key
to save parameters to file press 's' key
to load parameters from file press 'l' key
The program exits when the Esc key is pressed.
Usage:
ed.py [<image_name>]
Expand All @@ -16,30 +33,25 @@
import random as rng
import sys

def EdgeDrawingDemo(src, convert_to_gray):

def EdgeDrawingDemo(src, ed, EDParams, convert_to_gray):
rng.seed(12345)
ssrc = src.copy()*0
ssrc = np.zeros_like(src)
lsrc = src.copy()
esrc = src.copy()

ed = cv.ximgproc.createEdgeDrawing()
img_to_detect = cv.cvtColor(src, cv.COLOR_BGR2GRAY) if convert_to_gray else src

# you can change parameters (refer the documentation to see all parameters)
EDParams = cv.ximgproc_EdgeDrawing_Params()
EDParams.MinPathLength = 50 # try changing this value between 5 to 1000
EDParams.PFmode = False # defaut value try to swich it to True
EDParams.MinLineLength = 10 # try changing this value between 5 to 100
EDParams.NFAValidation = True # defaut value try to swich it to False

ed.setParams(EDParams)
cv.imshow("source image", img_to_detect)

if convert_to_gray:
img_to_detect = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
else:
img_to_detect = src
print("")
print("convert_to_gray:", convert_to_gray)
print("MinPathLength:", EDParams.MinPathLength)
print("MinLineLength:", EDParams.MinLineLength)
print("PFmode:", EDParams.PFmode)
print("NFAValidation:", EDParams.NFAValidation)

cv.imshow("source image", img_to_detect)
tm = cv.TickMeter()
tm.start()

# Detect edges
# you should call this before detectLines() and detectEllipses()
Expand All @@ -49,48 +61,91 @@ def EdgeDrawingDemo(src, convert_to_gray):
lines = ed.detectLines()
ellipses = ed.detectEllipses()

#Draw detected edge segments
for i in range(len(segments)):
color = (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256))
cv.polylines(ssrc, [segments[i]], False, color, 1, cv.LINE_8)
tm.stop()

print("Detection time : {:.2f} ms. using the parameters above".format(tm.getTimeMilli()))

# Draw detected edge segments
for segment in segments:
color = (rng.randint(0, 256), rng.randint(0, 256), rng.randint(0, 256))
cv.polylines(ssrc, [segment], False, color, 1, cv.LINE_8)

cv.imshow("detected edge segments", ssrc)

#Draw detected lines
if lines is not None: # Check if the lines have been found and only then iterate over these and add them to the image
# Draw detected lines
if lines is not None: # Check if the lines have been found and only then iterate over these and add them to the image
lines = np.uint16(np.around(lines))
for i in range(len(lines)):
cv.line(lsrc, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 1, cv.LINE_AA)
for line in lines:
cv.line(lsrc, (line[0][0], line[0][1]), (line[0][2], line[0][3]), (0, 0, 255), 1, cv.LINE_AA)

cv.imshow("detected lines", lsrc)

#Draw detected circles and ellipses
if ellipses is not None: # Check if circles and ellipses have been found and only then iterate over these and add them to the image
for i in range(len(ellipses)):
center = (int(ellipses[i][0][0]), int(ellipses[i][0][1]))
axes = (int(ellipses[i][0][2])+int(ellipses[i][0][3]),int(ellipses[i][0][2])+int(ellipses[i][0][4]))
angle = ellipses[i][0][5]
color = (0, 0, 255)
if ellipses[i][0][2] == 0:
color = (0, 255, 0)
cv.ellipse(esrc, center, axes, angle,0, 360, color, 2, cv.LINE_AA)
# Draw detected circles and ellipses
if ellipses is not None: # Check if circles and ellipses have been found and only then iterate over these and add them to the image
for ellipse in ellipses:
center = (int(ellipse[0][0]), int(ellipse[0][1]))
axes = (int(ellipse[0][2] + ellipse[0][3]), int(ellipse[0][2] + ellipse[0][4]))
angle = ellipse[0][5]

cv.imshow("detected circles and ellipses", esrc)
print('Press any key to swich color conversion code. Press Esc to exit.')
color = (0, 255, 0) if ellipse[0][2] == 0 else (0, 0, 255)

cv.ellipse(esrc, center, axes, angle, 0, 360, color, 2, cv.LINE_AA)

if __name__ == '__main__':
print(__doc__)
cv.imshow("detected circles and ellipses", esrc)

def main():
try:
fn = sys.argv[1]
except IndexError:
fn = 'board.jpg'
src = cv.imread(cv.samples.findFile(fn))
if src is None:
print("Error loading image")
return

ed = cv.ximgproc.createEdgeDrawing()

# Set parameters (refer to the documentation for all parameters)
EDParams = cv.ximgproc_EdgeDrawing_Params()
EDParams.MinPathLength = 10 # try changing this value by pressing '/' and '*' keys
EDParams.MinLineLength = 10 # try changing this value by pressing '+' and '-' keys
EDParams.PFmode = False # default value is False, try switching by pressing 'p' key
EDParams.NFAValidation = True # default value is True, try switching by pressing 'n' key

convert_to_gray = True
key = 0
while key is not 27:
EdgeDrawingDemo(src, convert_to_gray)

while key != 27:
ed.setParams(EDParams)
EdgeDrawingDemo(src, ed, EDParams, convert_to_gray)
key = cv.waitKey()
convert_to_gray = not convert_to_gray
if key == 32: # space key
convert_to_gray = not convert_to_gray
if key == 112: # 'p' key
EDParams.PFmode = not EDParams.PFmode
if key == 110: # 'n' key
EDParams.NFAValidation = not EDParams.NFAValidation
if key == 43: # '+' key
EDParams.MinLineLength = EDParams.MinLineLength + 5
if key == 45: # '-' key
EDParams.MinLineLength = max(0, EDParams.MinLineLength - 5)
if key == 47: # '/' key
EDParams.MinPathLength = EDParams.MinPathLength + 20
if key == 42: # '*' key
EDParams.MinPathLength = max(0, EDParams.MinPathLength - 20)
if key == 115: # 's' key
fs = cv.FileStorage("ed-params.xml",cv.FileStorage_WRITE)
EDParams.write(fs)
fs.release()
print("parameters saved to ed-params.xml")
if key == 108: # 'l' key
fs = cv.FileStorage("ed-params.xml",cv.FileStorage_READ)
if fs.isOpened():
EDParams.read(fs.root())
fs.release()
print("parameters loaded from ed-params.xml")

if __name__ == '__main__':
print(__doc__)
main()
cv.destroyAllWindows()
Loading

0 comments on commit 99fb0d3

Please sign in to comment.