OlliJJJ
OlliJJJ

Reputation: 21

Helplessly lost with openCV and HoughCircles

I'm trying to detect this black circle here. Shouldn't be too difficult but for some reason I just get 0 circles or approximately 500 circles everywhere, depending on the arguments. But there is no middle ground. Feels like I have tried to play with the arguments for hours, but absolutely no success. Is there a problem using HoughCircles and black or white picture? The task seems simple to a human eye, but is this difficult to the computer for some reason?

Here's my code:

import numpy as np
import cv2

image = cv2.imread('temp.png')
output = image.copy()
blurred = cv2.blur(image,(10,10))

gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)


circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.5, 20, 100, 600, 10, 100)


if circles is not None:

    circles = np.round(circles[0, :]).astype("int")
        print len(circles)

    for (x, y, r) in circles:
        cv2.circle(output, (x, y), r, (0, 255, 0), 4)
        cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)

    show the output image
cv2.imshow("output", np.hstack([output]))
cv2.waitKey(0)

Upvotes: 1

Views: 766

Answers (1)

Jeru Luke
Jeru Luke

Reputation: 21233

There are few minor mistakes in your approach.

Here is the code I used from the documentation:

img = cv2.imread('temp.png',0)
img = cv2.medianBlur(img,5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
cimg1 = cimg.copy() 

circles = cv2.HoughCircles img,cv2.HOUGH_GRADIENT,1,20,param1=50,param2=30,minRadius=0,maxRadius=0)

circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(cimg,(i[0],i[1]),2,(0,255,255),3)

cv2.imshow('detected circles.jpg',cimg)

enter image description here

joint = np.hstack([cimg1, cimg])  #---Posting the original image along with the image having the detected circle
cv2.imshow('detected circle and output', joint )

enter image description here

Upvotes: 1

Related Questions