From cf0df733dad004df3505866a67c1b62474d8cbf0 Mon Sep 17 00:00:00 2001 From: Aleksandar Atanasov Date: Thu, 2 Jun 2016 10:58:46 +0200 Subject: [PATCH 1/7] Fix houghcircles.py when no circles found In the C++ equivalent of this example a check is made whether the vector (here in Python we have a list) actually has any circles in it that is whether the Hough circles function has managed to find any in the given image. This check is missing for the Python example and if no circles are found the application breaks. --- samples/python/houghcircles.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/samples/python/houghcircles.py b/samples/python/houghcircles.py index 5386fc2102..41d42eb1e1 100755 --- a/samples/python/houghcircles.py +++ b/samples/python/houghcircles.py @@ -29,10 +29,12 @@ if __name__ == '__main__': cimg = src.copy() # numpy function circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 10, np.array([]), 100, 30, 1, 30) - a, b, c = circles.shape - for i in range(b): - cv2.circle(cimg, (circles[0][i][0], circles[0][i][1]), circles[0][i][2], (0, 0, 255), 3, cv2.LINE_AA) - cv2.circle(cimg, (circles[0][i][0], circles[0][i][1]), 2, (0, 255, 0), 3, cv2.LINE_AA) # draw center of circle + + if circles != None: # Check if circles have been found and only then iterate over these and add them to the image + a, b, c = circles.shape + for i in range(b): + cv2.circle(cimg, (circles[0][i][0], circles[0][i][1]), circles[0][i][2], (0, 0, 255), 3, cv2.LINE_AA) + cv2.circle(cimg, (circles[0][i][0], circles[0][i][1]), 2, (0, 255, 0), 3, cv2.LINE_AA) # draw center of circle cv2.imshow("source", src) cv2.imshow("detected circles", cimg) From 14deab252bc50f2f99d0944bce3d80b04450ea1b Mon Sep 17 00:00:00 2001 From: Aleksandar Atanasov Date: Thu, 2 Jun 2016 11:00:23 +0200 Subject: [PATCH 2/7] Fix houghlines.py when no lines found In the C++ equivalent of this example a check is made whether the vector (here in Python we have a list) actually has any lines in it that is whether the Hough lines function has managed to find any in the given image. This check is missing for the Python example and if no lines are found the application breaks. --- samples/python/houghlines.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/samples/python/houghlines.py b/samples/python/houghlines.py index 120d8bbaae..98c3640147 100755 --- a/samples/python/houghlines.py +++ b/samples/python/houghlines.py @@ -36,16 +36,17 @@ if __name__ == '__main__': else: # HoughLines lines = cv2.HoughLines(dst, 1, math.pi/180.0, 50, np.array([]), 0, 0) - a,b,c = lines.shape - for i in range(a): - rho = lines[i][0][0] - theta = lines[i][0][1] - a = math.cos(theta) - b = math.sin(theta) - x0, y0 = a*rho, b*rho - pt1 = ( int(x0+1000*(-b)), int(y0+1000*(a)) ) - pt2 = ( int(x0-1000*(-b)), int(y0-1000*(a)) ) - cv2.line(cdst, pt1, pt2, (0, 0, 255), 3, cv2.LINE_AA) + if lines != None: + a,b,c = lines.shape + for i in range(a): + rho = lines[i][0][0] + theta = lines[i][0][1] + a = math.cos(theta) + b = math.sin(theta) + x0, y0 = a*rho, b*rho + pt1 = ( int(x0+1000*(-b)), int(y0+1000*(a)) ) + pt2 = ( int(x0-1000*(-b)), int(y0-1000*(a)) ) + cv2.line(cdst, pt1, pt2, (0, 0, 255), 3, cv2.LINE_AA) cv2.imshow("source", src) cv2.imshow("detected lines", cdst) From 25e2e8aa3c22ddbad5a2006ffb654ec28827cb30 Mon Sep 17 00:00:00 2001 From: Aleksandar Atanasov Date: Thu, 9 Jun 2016 07:18:47 +0200 Subject: [PATCH 3/7] Removed trailing spaces at line 32 --- samples/python/houghcircles.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/python/houghcircles.py b/samples/python/houghcircles.py index 41d42eb1e1..477dac00d4 100755 --- a/samples/python/houghcircles.py +++ b/samples/python/houghcircles.py @@ -29,7 +29,7 @@ if __name__ == '__main__': cimg = src.copy() # numpy function circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 10, np.array([]), 100, 30, 1, 30) - + if circles != None: # Check if circles have been found and only then iterate over these and add them to the image a, b, c = circles.shape for i in range(b): From c5bbc0353c1daadae8d953baf2940b146cd87185 Mon Sep 17 00:00:00 2001 From: Aleksandar Atanasov Date: Sun, 12 Jun 2016 12:54:16 +0200 Subject: [PATCH 4/7] Added small fix when circles are not detected I noticed that I missed the fact that `cimg` is used in the second `imshow()` call. Changed the scope of the second function call to be within the if-statement. Otherwise in cases where have not been detected the second `imshow()` will attempt to use `cimg` which will be empty leading to an error. --- samples/python/houghcircles.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/samples/python/houghcircles.py b/samples/python/houghcircles.py index 477dac00d4..0bfee42933 100755 --- a/samples/python/houghcircles.py +++ b/samples/python/houghcircles.py @@ -35,7 +35,8 @@ if __name__ == '__main__': for i in range(b): cv2.circle(cimg, (circles[0][i][0], circles[0][i][1]), circles[0][i][2], (0, 0, 255), 3, cv2.LINE_AA) cv2.circle(cimg, (circles[0][i][0], circles[0][i][1]), 2, (0, 255, 0), 3, cv2.LINE_AA) # draw center of circle + + cv2.imshow("detected circles", cimg) cv2.imshow("source", src) - cv2.imshow("detected circles", cimg) cv2.waitKey(0) From 445349dd7d2ab81f2fd027f6f0575277d35882f4 Mon Sep 17 00:00:00 2001 From: Aleksandar Atanasov Date: Sun, 12 Jun 2016 12:55:29 +0200 Subject: [PATCH 5/7] Same fix as with the houghcircles Moved second `imshow()` inside the if-statement to prevent error when no lines have been found and the function is called with an empty `cdst`. --- samples/python/houghlines.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/samples/python/houghlines.py b/samples/python/houghlines.py index 98c3640147..4a98828824 100755 --- a/samples/python/houghlines.py +++ b/samples/python/houghlines.py @@ -47,7 +47,8 @@ if __name__ == '__main__': pt1 = ( int(x0+1000*(-b)), int(y0+1000*(a)) ) pt2 = ( int(x0-1000*(-b)), int(y0-1000*(a)) ) cv2.line(cdst, pt1, pt2, (0, 0, 255), 3, cv2.LINE_AA) + + cv2.imshow("detected lines", cdst) cv2.imshow("source", src) - cv2.imshow("detected lines", cdst) cv2.waitKey(0) From 4fa86dad263bd8c31f40d7b1fcb151ca29c441cd Mon Sep 17 00:00:00 2001 From: Aleksandar Atanasov Date: Mon, 13 Jun 2016 09:00:29 +0200 Subject: [PATCH 6/7] Update houghcircles.py --- samples/python/houghcircles.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/python/houghcircles.py b/samples/python/houghcircles.py index 0bfee42933..41cce29be7 100755 --- a/samples/python/houghcircles.py +++ b/samples/python/houghcircles.py @@ -35,7 +35,7 @@ if __name__ == '__main__': for i in range(b): cv2.circle(cimg, (circles[0][i][0], circles[0][i][1]), circles[0][i][2], (0, 0, 255), 3, cv2.LINE_AA) cv2.circle(cimg, (circles[0][i][0], circles[0][i][1]), 2, (0, 255, 0), 3, cv2.LINE_AA) # draw center of circle - + cv2.imshow("detected circles", cimg) cv2.imshow("source", src) From 0637ca21dcc91bb2ebb6f26530107dc2b49693bf Mon Sep 17 00:00:00 2001 From: Aleksandar Atanasov Date: Mon, 13 Jun 2016 09:00:42 +0200 Subject: [PATCH 7/7] Update houghlines.py --- samples/python/houghlines.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/python/houghlines.py b/samples/python/houghlines.py index 4a98828824..445068aef4 100755 --- a/samples/python/houghlines.py +++ b/samples/python/houghlines.py @@ -47,7 +47,7 @@ if __name__ == '__main__': pt1 = ( int(x0+1000*(-b)), int(y0+1000*(a)) ) pt2 = ( int(x0-1000*(-b)), int(y0-1000*(a)) ) cv2.line(cdst, pt1, pt2, (0, 0, 255), 3, cv2.LINE_AA) - + cv2.imshow("detected lines", cdst) cv2.imshow("source", src)