OpenCV Tutorial

Image Processing

Feature Detection and Description

Drawing Functions

Video Processing

Applications and Projects

Displaying the coordinates of the points clicked on the image using Python-OpenCV

Displaying the coordinates of points clicked on an image is a common interaction in image processing tasks. Using OpenCV, you can easily achieve this by setting a mouse callback function that responds to mouse events.

Here's a step-by-step guide to display the coordinates of the points clicked on an image:

Display Coordinates on Image Click using OpenCV

  1. Setup: First, ensure you've installed OpenCV:

    pip install opencv-python
    
  2. Code to Display Coordinates:

    import cv2
    
    # Callback function for mouse events
    def click_event(event, x, y, flags, param):
        # Check for left mouse clicks
        if event == cv2.EVENT_LBUTTONDOWN:
            # Display the coordinates on the image
            cv2.circle(img, (x, y), 3, (0, 0, 255), -1)  # Draw a small circle at the click position
            coord = "(" + str(x) + "," + str(y) + ")"
            cv2.putText(img, coord, (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 1)
            cv2.imshow('Image', img)
    
    # Load the image and create a window
    img = cv2.imread('path_to_image.jpg')
    cv2.namedWindow('Image')
    
    # Set the callback function for mouse events
    cv2.setMouseCallback('Image', click_event)
    
    # Display the image
    cv2.imshow('Image', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
  3. Run the Code: When you run the code and click on the image, you should see the coordinates of the clicked point displayed on the image.

Explanation:

  • The click_event function is a callback that's executed when a mouse event occurs in the 'Image' window.

  • Inside the click_event function, we check if the event type is a left button click using cv2.EVENT_LBUTTONDOWN. If it is, we display the coordinates on the image.

  • The cv2.setMouseCallback function allows us to set the callback function (click_event in this case) to be called when a mouse event occurs in the specified window.

This method allows you to interactively click on the image and instantly see the coordinates of the clicked points.

  1. Capturing and displaying point coordinates on an image with OpenCV:

    • This involves capturing mouse events and displaying the coordinates of the clicked points on the image.
    import cv2
    
    # Callback function for mouse events
    def mouse_callback(event, x, y, flags, param):
        if event == cv2.EVENT_LBUTTONDOWN:
            print(f'Clicked coordinates: ({x}, {y})')
    
    # Read an image
    img = cv2.imread('image.jpg')
    
    # Create a window and set the callback function
    cv2.namedWindow('Image')
    cv2.setMouseCallback('Image', mouse_callback)
    
    # Display the image
    cv2.imshow('Image', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
  2. Interactive point selection and display using OpenCV in Python:

    • This involves continuously updating the display with newly selected points, creating an interactive experience.
    import cv2
    
    # List to store selected coordinates
    points = []
    
    # Callback function for mouse events
    def mouse_callback(event, x, y, flags, param):
        if event == cv2.EVENT_LBUTTONDOWN:
            points.append((x, y))
            print(f'Selected coordinates: {points}')
    
            # Display the points on the image
            for point in points:
                cv2.circle(img, point, 5, (0, 255, 0), -1)
            cv2.imshow('Image', img)
    
    # Read an image
    img = cv2.imread('image.jpg')
    
    # Create a window and set the callback function
    cv2.namedWindow('Image')
    cv2.setMouseCallback('Image', mouse_callback)
    
    # Display the image
    cv2.imshow('Image', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()