Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024

Circle Detection

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

In the OpenCV Line Detection, we use the function of the Hough lines, which helped us detect lines in a picture; similar to this circle detection, we will use the function of Hough Circles. Circle detection finds various uses in biomedical applications, ranging from iris detection to white blood cell segmentation.

Hough Circles

The first step is standard in every OpenCV Detection program, i.e., to load the Image on which the algorithm is applied.

Then, the image is resized, and we store a colorful copy of that Image in another variable. Then, we convert the Image to the grayscale Image as we can apply the HoughCircles() function only to the grayscale images. Then, we apply HoughCircle() function to the grayscale image. Gray scaled Image is then blurred using medianBlur() function.

The HoughCircles() function in the OpenCV library takes many parameters.

  • image: 8-bit, single-channel image. If we work with a color image, convert it to grayscale first.
  • method: Defines the process to detect circles in images. Currently, the only implemented method is cv2.HOUGH_GRADIENT.
  • dp: It is the inverse ratio of the accumulator resolution to the image resolution. With the increase in dp, the accumulator array becomes small.
  • minDist: It is the minimum distance between the center (x, y) coordinates of detected circles. If the minDist is too tiny, multiple circles in the same neighborhood as the original may be (falsely) detected. If the minDist is too large, some circles may not be detected at all. The minDist parameter is essential. Without an optimal minDist value, we may end up missing out on some circles, or we may detect many false circles.
  • param1: This parameter is the gradient value used to handle edge detection.
  • param2: This parameter is the accumulator threshold value for the cv2.HOUGH_GRADIENT method. The smaller the threshold is, the more circles will be detected (including false circles). The larger the threshold is, the more circles will potentially be returned.
  • minRadius: It is the minimum size of the radius (in pixels).
  • maxRadius: It is the maximum radius size (in pixels).
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Implementation

Importing Libraries

We import the necessary packages we’ll need. We’ll utilize NumPy for numerical processing and cv2 for our OpenCV bindings.

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

Reading the Image

img = cv.imread('eg.jpg', cv.IMREAD_COLOR)

Displaying the Image

plt.imshow(img)

Converting to grayscale

gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# Blur using 3 * 3 kernel.
gray_blurred = cv.blur(gray, (3, 3))
  
# Apply Hough transform on the blurred image to detect circles.
detected_circles = cv.HoughCircles(gray_blurred, 
                   cv.HOUGH_GRADIENT, 1, 20, param1 = 50,
               param2 = 30, minRadius = 1, maxRadius = 30)

# Drawing circles that we can detect.
if detected_circles is not None:
  
    # Convert the circle parameters h, k and r to integers.
    detected_circles = np.uint16(np.around(detected_circles))
  
    for point in detected_circles[0, :]:
        h, k, r = point[0], point[1], point[2]
  
        # Draw the circumference of the circle.
        cv.circle(img, (a, b), r, (0, 255, 0), 2)
  
        # Drawing a small circle to show the center.
        cv.circle(img, (a, b), 1, (0, 0, 255), 3)
        cv.imshow("Detected Circle", img)
        cv.waitKey(0)
plt.imshow(img)

Output

Also read, Sampling and Quantization

Frequently Asked Questions

  1. How do you identify a circle?
    For detecting the circles or any geometric shape, we first need to detect or identify the edges of the objects and shapes present in the Image. The edges are the points for which there is a sharp change of color. For instance, the edge of a black ball on a white background is a circle.
     
  2. What algorithm is used to detect circles in OpenCV?
    We use cv2. HoughCircles to detect circles. We have to provide some parameters such as image where Image is the image file converted to greyscale. The method is the algorithm used to detect the circles. dp is the inverse ratio of the accumulator resolution to the image resolution. minDist is the Minimum distance between the center coordinates of detected circles.
     
  3. Why is Hough transform slow?
    The problem with the Hough Transform algorithm is that it uses ρ (the perpendicular distance of the line from origin) and θ (the angle made by ordinary to this line with the positive direction about the x-axis). This makes it time-consuming.

Key Takeaways

Let us brief the article.

Firstly, we learn about circle detection than its applications in different fields. Moving on, We learned about Hough circles and their various parameters. Finally, we saw the implementation part of the circle detection.
Check out this problem - First Missing Positive 

I hope you all like this article.

Happy Learning Ninajs!

Topics covered
1.
Introduction
2.
Hough Circles
3.
Implementation
3.1.
Importing Libraries
3.2.
Reading the Image
3.3.
Displaying the Image
3.4.
Converting to grayscale
4.
Frequently Asked Questions
5.
Key Takeaways