Ask Your Question
0

Can't detect the circle

asked 2018-10-18 17:57:51 -0600

Shrijan gravatar image

I tried using the function cv2.HoughCircles but it doesnt detect the hand drawn circle on the paper. Can anybody help me with this?

C:\fakepath\98_5cm.png

enter code here
import numpy as np
import imutils
import cv2

image = cv2.imread("images/136_5cm.png")
output = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#gray = cv2.GaussianBlur(gray, (5, 5), 0)
# detect circles in the image
print('hello')
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.4 ,100,)
# ensure at least some circles were found
if circles is not None:
    # convert the (x, y) coordinates and radius of the circles to integers
    circles = np.round(circles[0, :]).astype("int")
    print(circles)

    # loop over the (x, y) coordinates and radius of the circles
    for (x, y, r) in circles:
        # draw the circle in the output image, then draw a rectangle
        # corresponding to the center of the circle
        cv2.circle(output, (x, y), r, (0, 255, 0), thickness=4, lineType=8, shift=0)
        cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)

    # show the output image
    cv2.imshow("output", np.hstack([image, output]))
    cv2.waitKey(0)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-10-18 22:20:28 -0600

supra56 gravatar image
#Using Raspberry pi 3, kernel 4.14.76, OpenCV 4.0
import numpy as np
import cv2

image = cv2.imread("images/136_5cm.png")
output = image.copy()
gray = cv2.cvtColor(output, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (9, 9), 0)
# detect circles in the image
print('hello')
circles = cv2.HoughCircles(gray,cv2.HOUGH_GRADIENT,1.4,50, param1=80,param2=40,minRadius=10,maxRadius=30)

# ensure at least some circles were found
if circles is not None:
    # convert the (x, y) coordinates and radius of the circles to integers
    circles = np.round(circles[0, :]).astype("int")

    # loop over the (x, y) coordinates and radius of the circles
    for (x, y, r) in circles:
        r = 28
        # draw the circle in the output image, then draw a rectangle
        # corresponding to the center of the circle
        cv2.circle(output, (x, y),r , (0, 255, 0), 2)
        cv2.rectangle(output, (x -5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)

    # show the output image
    cv2.imshow("output", np.hstack([image, output]))
    cv2.waitKey(0)
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-10-18 17:57:51 -0600

Seen: 484 times

Last updated: Oct 18 '18