Ask Your Question
0

How can circles be detected in openCV?

asked 2022-06-28 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-04-09 05:00:00 +0000

woof gravatar image

Circles can be detected in OpenCV using the HoughCircles function. This function uses the Hough transform to detect circles in images based on their edge information.

Here's an example of how to use the HoughCircles function to detect circles in an image:

import cv2
import numpy as np

# Load image
img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)

# Blur image to reduce noise and improve circle detection
img = cv2.medianBlur(img, 5)

# Detect circles using Hough transform
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0)

# Draw circles on original image
if circles is not None:
    circles = np.round(circles[0, :]).astype("int")
    for (x, y, r) in circles:
        cv2.circle(img, (x, y), r, (0, 255, 0), 2)
        cv2.circle(img, (x, y), 2, (0, 0, 255), 3)

# Display result
cv2.imshow('Detected Circles', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

In this example, the HoughCircles function is called with several parameters:

  • img: The input grayscale image to detect circles in.
  • cv2.HOUGH_GRADIENT: The method used for circle detection.
  • 1: The inverse ratio of the accumulator resolution to the image resolution.
  • 20: The minimum distance between detected circles.
  • param1: The higher threshold for edge detection.
  • param2: The accumulator threshold for circle detection.
  • minRadius: The minimum radius of the detected circles.
  • maxRadius: The maximum radius of the detected circles.

The circles are then drawn on the original image using the cv2.circle function. The resulting image shows the detected circles in green.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-06-28 11:00:00 +0000

Seen: 17 times

Last updated: Apr 09 '21