OpenCV Tutorial

Image Processing

Feature Detection and Description

Drawing Functions

Video Processing

Applications and Projects

Find Co-ordinates of Contours in OpenCV

Extracting the coordinates of contours can be valuable in many computer vision applications, such as object recognition, shape analysis, and feature extraction. In this tutorial, we will walk through how to find and print the coordinates of contours in an image using Python and OpenCV.

Find Coordinates of Contours using OpenCV

  1. Setup:

    Ensure OpenCV is installed:

    pip install opencv-python
    
  2. Code to Find and Print Coordinates of Contours:

    import cv2
    
    def find_contour_coordinates(image_path):
        # Read the image
        image = cv2.imread(image_path)
    
        # Convert the image to grayscale
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
        # Apply binary thresholding
        _, thresholded = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
    
        # Find contours in the thresholded image
        contours, _ = cv2.findContours(thresholded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    
        # Loop over each contour
        for idx, contour in enumerate(contours):
            print(f"Coordinates of contour {idx + 1}:")
    
            # Loop over each point in the contour
            for point in contour:
                x, y = point[0]
                print(f"(x={x}, y={y})")
    
            print("-" * 30)
    
        # Display the image with contours (for visualization)
        cv2.drawContours(image, contours, -1, (0, 255, 0), 2)
        cv2.imshow('Contours', image)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
    
    # Call the function
    find_contour_coordinates('path_to_your_image.jpg')
    
  3. Run the Code:

    After running the code, you'll see the coordinates of each contour printed in the console. The image will also be displayed with the contours highlighted for visualization.

Notes:

  • cv2.findContours() retrieves contours from the binary image. The cv2.RETR_EXTERNAL flag retrieves only the external contours. If you wish to retrieve all the contours, including those inside the objects (like a contour inside 'O' or '8'), use cv2.RETR_LIST.

  • The cv2.CHAIN_APPROX_SIMPLE flag compresses horizontal, diagonal, and vertical segments of the contour and leaves only their end points. If you want all the contour points without any compression, use cv2.CHAIN_APPROX_NONE.

This tutorial provides a basic method to find and print the coordinates of contours in an image using OpenCV. The concept can be further extended based on specific requirements, such as extracting the contour's bounding box or computing the contour's centroid.

  1. Extracting coordinates of contours using OpenCV in Python:

    • Description: Learn how to extract the coordinates of contours in images using OpenCV in Python.
    • Sample Code:
      import cv2
      import numpy as np
      
      image = cv2.imread('input_image.jpg', cv2.IMREAD_GRAYSCALE)
      _, contours, _ = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
      
      for contour in contours:
          for point in contour:
              x, y = point[0]
              print(f'Coordinate: ({x}, {y})')
      
  2. Python OpenCV contour analysis for coordinate extraction:

    • Description: Understand how contour analysis can be used to extract coordinates of points along the contours.
    • Sample Code:
      # Similar to the previous code snippet
      
  3. Sample code for obtaining contour coordinates in OpenCV:

    • Description: Get hands-on experience with a sample code for extracting and printing contour coordinates in OpenCV.
    • Sample Code:
      # Similar to the previous code snippet
      
  4. Optimizing contour coordinate extraction in Python with OpenCV:

    • Description: Explore techniques to optimize the extraction of contour coordinates, especially for complex shapes.
    • Sample Code:
      # Experiment with contour analysis parameters for better coordinate extraction
      
  5. Python OpenCV contour coordinates vs bounding box:

    • Description: Compare and contrast the use of contour coordinates with bounding boxes in Python using OpenCV.
    • Sample Code:
      # Compare contour coordinate extraction with bounding box methods
      
  6. Contour filtering based on coordinates and geometry in OpenCV:

    • Description: Learn how to filter contours based on their coordinates and geometric properties.
    • Sample Code:
      # Filter contours based on coordinates and geometry criteria
      
  7. Python OpenCV contour coordinate visualization and manipulation:

    • Description: Understand how to visualize and manipulate contour coordinates for various purposes.
    • Sample Code:
      # Visualize and manipulate contour coordinates for specific applications