Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Try this:

#!/usr/bin/env python
#OpenCV 4.4.0, RPI 3B/3B+, 4B/4/8gb, Buster v10
#Date: 21th October, 2020

import cv2
import numpy as np
image = cv2.imread('circle.png')
output = image.copy()
img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Find circles
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1.5, 100)
# If some circle is found
if circles is not None:
   # Get the (x, y, r) as integers
   circles = np.round(circles[0, :]).astype("int")
   print(circles)
   # loop over the circles
   for (x, y, r) in circles:
      cv2.circle(output, (x, y), r, (0, 255, 0), 2)
# show the output image
cv2.imshow("circle",output)
cv2.imwrite('circleA.png', output)
cv2.waitKey(0)

Output:

image description

Try this:

#!/usr/bin/env python
#OpenCV 4.4.0, RPI 3B/3B+, 4B/4/8gb, Buster v10
#Date: 21th October, 2020

import cv2
import numpy as np
image = cv2.imread('circle.png')
output = image.copy()
img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Find circles
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1.5, 100)
# If some circle is found
if circles is not None:
   # Get the (x, y, r) as integers
   circles = np.round(circles[0, :]).astype("int")
   print(circles)
   # loop over the circles
   for (x, y, r) in circles:
      cv2.circle(output, (x, y), r, (0, 255, 0), 2)
# show the output image
cv2.imshow("circle",output)
cv2.imwrite('circleA.png', output)
cv2.waitKey(0)

Output:

image description

If u using this cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT_ALT, 1.5, 1, 300, 0.9). U get this. Output:

image description

Try this:

#!/usr/bin/env python
#OpenCV 4.4.0, RPI 3B/3B+, 4B/4/8gb, Buster v10
#Date: 21th October, 2020

import cv2
import numpy as np
image = cv2.imread('circle.png')
output = image.copy()
img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Find circles
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1.5, 100)
# If some circle is found
if circles is not None:
   # Get the (x, y, r) as integers
   circles = np.round(circles[0, :]).astype("int")
   print(circles)
   # loop over the circles
   for (x, y, r) in circles:
      cv2.circle(output, (x, y), r, (0, 255, 0), 2)
# show the output image
cv2.imshow("circle",output)
cv2.imwrite('circleA.png', output)
cv2.waitKey(0)

Output:

image description

If u using this cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT_ALT, 1.5, 1, 300, 0.9). U get this. Output:

image description

Apologised. I didn't see this cv2.HOUGH_GRADIENT_ALT. Here is code:

import cv2

img = cv2.imread('circle.png')
img2 = img.copy()
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

circles = cv2.HoughCircles(img_gray, cv2.HOUGH_GRADIENT_ALT,
                            2, 30, param1=300,
                            param2=0.85, minRadius=20)
print(circles)

for circle in circles[0]:
    center_x, center_y, radius = circle
    cv2.circle(img2, (center_x, center_y), int(radius),(0, 0, 255), 2)

cv2.imwrite('circle4.png', img2) 
cv2.imshow("img2", img2)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

image description

Try this:

#!/usr/bin/env python
#OpenCV 4.4.0, RPI 3B/3B+, 4B/4/8gb, Buster v10
#Date: 21th October, 2020

import cv2
import numpy as np
image = cv2.imread('circle.png')
output = image.copy()
img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Find circles
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1.5, 100)
# If some circle is found
if circles is not None:
   # Get the (x, y, r) as integers
   circles = np.round(circles[0, :]).astype("int")
   print(circles)
   # loop over the circles
   for (x, y, r) in circles:
      cv2.circle(output, (x, y), r, (0, 255, 0), 2)
# show the output image
cv2.imshow("circle",output)
cv2.imwrite('circleA.png', output)
cv2.waitKey(0)

Output:

image description

If u using this cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT_ALT, 1.5, 1, 300, 0.9). U get this. Output:

image description

Apologised. I didn't see this cv2.HOUGH_GRADIENT_ALT. Here is code:

import cv2

img = cv2.imread('circle.png')
img2 = img.copy()
img_gray = cv2.cvtColor(img, cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

circles = cv2.HoughCircles(img_gray, cv2.HOUGH_GRADIENT_ALT,
                            2, 30, param1=300,
                            param2=0.85, minRadius=20)
print(circles)

for circle in circles[0]:
    center_x, center_y, radius = circle
    cv2.circle(img2, (center_x, center_y), int(radius),(0, 0, 255), 2)

cv2.imwrite('circle4.png', img2) 
cv2.imshow("img2", img2)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

image description