user3182080
user3182080

Reputation: 231

OpenCV3 Circle Detection - How to Find Simple Circles (Obvious solutions not being found)

Here is a simple image that has blobs of the same size and hard edge, as a greyscale image. Why does the code below not find these circles?

ret,thresh1 = cv2.threshold(img,180,250,cv2.THRESH_BINARY)
thresh1 = cv2.blur(thresh1,(15,15))
circles = cv2.HoughCircles(thresh1,cv2.HOUGH_GRADIENT, 2, np.shape(thresh1)[0]/8, param1=200, param2=10)
if circles is not None:
    plt.imshow(np.flipud(thresh1), origin='lower', cmap='bone')
    circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    currentAxis = plt.gca()
    currentAxis.add_patch(Circle((i[0] , i[1]), i[2],fill=False,color='red'))
    currentAxis.add_patch(Circle((i[0] , i[1]), 8,fill=True,color='green'))
plt.show()

Simple Blobs Not Detected

The output that I got is this:

Detection Not Correct

I've tried tweaking the param1 and param2. Either I end up with false detections (more than this) or nothing at all! The minimum distance parameter is set to 1/8th the width of the image only as an arbitrary value.

Upvotes: 0

Views: 316

Answers (1)

user3182080
user3182080

Reputation: 231

Image was being flipped (np.flipud) and the origin set to the bottom - this was part of the next phase of this project, and I just didn't spot it! This codes works fine.

ret,thresh1 = cv2.threshold(img,180,250,cv2.THRESH_BINARY)
thresh1 = cv2.blur(thresh1,(15,15))
circles = cv2.HoughCircles(thresh1,cv2.HOUGH_GRADIENT, 1.2, minDist = 10,    param1=200, param2=50, minRadius = 0, maxRadius = 100)
if circles is not None:
    plt.imshow((thresh1), cmap='bone')
    circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    currentAxis = plt.gca()
    currentAxis.add_patch(Circle((i[0] , i[1]), i[2],fill=False,color='red'))
    currentAxis.add_patch(Circle((i[0] , i[1]), 8,fill=True,color='green'))
plt.show()

produces the following: enter image description here

Upvotes: 1

Related Questions