OpenCV Tutorial

Image Processing

Feature Detection and Description

Drawing Functions

Video Processing

Applications and Projects

Find Circles and Ellipses in an Image in OpenCV

Finding circles and ellipses in images is essential in many computer vision applications, from analyzing biological cell images to detecting eyes in facial recognition systems. OpenCV provides tools like the Hough Circle Transform and fitting ellipses over contours for these tasks.

Here's a tutorial on how to detect circles and ellipses in images using Python and OpenCV:

Find Circles and Ellipses in an Image using OpenCV

  1. Setup:

    Ensure OpenCV is installed:

    pip install opencv-python
    
  2. Detect Circles using Hough Circle Transform:

    import cv2
    import numpy as np
    
    def detect_circles(image_path):
        # Read the image
        image = cv2.imread(image_path, cv2.IMREAD_COLOR)
    
        # Convert the image to grayscale
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
        # Apply GaussianBlur to reduce noise and improve circle detection
        blurred = cv2.GaussianBlur(gray, (9, 9), 2)
    
        # Detect circles using Hough Circle Transform
        circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, dp=1.2, minDist=30, param1=50, param2=30, minRadius=5, maxRadius=150)
    
        if circles is not None:
            # Convert (x, y) coordinates and radius of the circles to integers
            circles = np.round(circles[0, :]).astype("int")
    
            # Loop over the detected circles and draw them
            for (x, y, r) in circles:
                cv2.circle(image, (x, y), r, (0, 255, 0), 4)
    
        # Display the output image
        cv2.imshow('Detected Circles', image)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
    
    detect_circles('path_to_your_image.jpg')
    
  3. Detect Ellipses by Fitting to Contours:

    def detect_ellipses(image_path):
        image = cv2.imread(image_path, cv2.IMREAD_COLOR)
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        blurred = cv2.GaussianBlur(gray, (5, 5), 0)
        _, thresholded = cv2.threshold(blurred, 127, 255, cv2.THRESH_BINARY)
        contours, _ = cv2.findContours(thresholded, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    
        for contour in contours:
            # To fit an ellipse, we need at least 5 points in the contour
            if len(contour) >= 5:
                ellipse = cv2.fitEllipse(contour)
                cv2.ellipse(image, ellipse, (0, 255, 0), 2)
    
        cv2.imshow('Detected Ellipses', image)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
    
    detect_ellipses('path_to_your_image.jpg')
    

Notes:

  • Parameters for cv2.HoughCircles like param1, param2, minRadius, and maxRadius might need tweaking based on the specific image or desired results.

  • The cv2.fitEllipse() function returns the rotated rectangle in which the ellipse is inscribed. The drawn ellipse will best fit the contour, but it requires at least 5 contour points to compute.

By using a combination of the circle detection and ellipse fitting methods, you can robustly detect various circular and elliptical shapes in images with OpenCV.

  1. Finding circular and elliptical shapes using OpenCV in Python:

    • Description: Learn how to detect circular and elliptical shapes in images using OpenCV in Python.
    • Sample Code:
      import cv2
      import numpy as np
      
      image = cv2.imread('input_image.jpg', cv2.IMREAD_GRAYSCALE)
      circles = cv2.HoughCircles(image, cv2.HOUGH_GRADIENT, dp=1, minDist=20, param1=50, param2=30, minRadius=10, maxRadius=50)
      
      if circles is not None:
          circles = np.uint16(np.around(circles))
          for i in circles[0, :]:
              cv2.circle(image, (i[0], i[1]), i[2], (255, 255, 255), 2)
      
      cv2.imshow('Circles', image)
      cv2.waitKey(0)
      cv2.destroyAllWindows()
      
  2. Python OpenCV circle and ellipse detection:

    • Description: Learn how to specifically detect circles and ellipses in images using OpenCV in Python.
    • Sample Code:
      # Similar to the previous code snippet, but with ellipse detection
      
  3. Sample code for finding circles and ellipses in OpenCV:

    • Description: Get hands-on experience with sample code for detecting circles and ellipses using OpenCV.
    • Sample Code:
      # Similar to the previous code snippet, but with variations for ellipses
      
  4. Optimizing circle and ellipse detection parameters in Python with OpenCV:

    • Description: Explore techniques to optimize parameters for circle and ellipse detection in different scenarios.
    • Sample Code:
      # Experiment with HoughCircles and HoughEllipses parameters for optimal results
      
  5. Circle and ellipse filtering based on size and eccentricity in OpenCV:

    • Description: Learn how to filter detected circles and ellipses based on their size and eccentricity.
    • Sample Code:
      # Filter circles and ellipses based on size and eccentricity criteria
      
  6. Python OpenCV circle and ellipse drawing for object recognition:

    • Description: Understand how drawing circles and ellipses can be employed for object recognition in computer vision applications.
    • Sample Code:
      # Implement object recognition using circle and ellipse drawing techniques