Skip to content

Commit

Permalink
Add EDColor code to EdgeDrawing Class
Browse files Browse the repository at this point in the history
fix for params.PFmode=true
Update edge_drawing.py
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 Sep 14, 2024
1 parent 438e67f commit 10ea4d9
Show file tree
Hide file tree
Showing 7 changed files with 881 additions and 197 deletions.
9 changes: 9 additions & 0 deletions modules/ximgproc/doc/ximgproc.bib
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,15 @@ @article{akinlar2013edcircles
publisher={Elsevier}
}

@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}

@article{loke2021accelerated,
title={Accelerated superpixel image segmentation with a parallelized DBSCAN algorithm},
author={Loke, Seng Cheong and MacDonald, Bruce A and Parsons, Matthew and W{\"u}nsche, Burkhard Claus},
Expand Down
10 changes: 5 additions & 5 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,13 +66,13 @@ 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 image and prepares them to detect lines and ellipses.
/** @brief Detects edges in a grayscale or color image and prepares them to detect lines and ellipses.
@param src 8-bit, single-channel, grayscale input image.
@param src 8-bit, single-channel or color input image.
*/
CV_WRAP virtual void detectEdges(InputArray src) = 0;

Expand Down
149 changes: 106 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,71 +33,117 @@
import random as rng
import sys

rng.seed(12345)

def main():
try:
fn = sys.argv[1]
except IndexError:
fn = 'board.jpg'

src = cv.imread(cv.samples.findFile(fn))
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
cv.imshow("source", src)

ssrc = src.copy()*0
def EdgeDrawingDemo(src, ed, EDParams, convert_to_gray):
rng.seed(12345)
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
cv.imshow("source image", img_to_detect)

ed.setParams(EDParams)
print("")
print("convert_to_gray:", convert_to_gray)
print("MinPathLength:", EDParams.MinPathLength)
print("MinLineLength:", EDParams.MinLineLength)
print("PFmode:", EDParams.PFmode)
print("NFAValidation:", EDParams.NFAValidation)

tm = cv.TickMeter()
tm.start()

# Detect edges
# you should call this before detectLines() and detectEllipses()
ed.detectEdges(gray)
ed.detectEdges(img_to_detect)

segments = ed.getSegments()
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]

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)

cv.imshow("detected circles and ellipses", esrc)
cv.waitKey(0)
print('Done')

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 != 27:
ed.setParams(EDParams)
EdgeDrawingDemo(src, ed, EDParams, convert_to_gray)
key = cv.waitKey()
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__)
Expand Down
Loading

0 comments on commit 10ea4d9

Please sign in to comment.