Search code examples
python-3.xopencvimage-processingscikit-image

get the numpy index for a given image location


Given an image, are there any ways to let user interactively knows its location by pointing to a specific image location. For instance, when I point to the location with angle, what are the corresponding index of the numpy array used to represent this image.

enter image description here


Solution

  • You can set a MouseCallback function to any window generated with OpenCV. Following is a short example code derived from this blog post. An image is shown in some window, and when the left mouse button is clicked, the x, y coordinates are printed. (That's how I understood your question.)

    import cv2
    
    # Actual mouse callback function
    def print_coords(event, x, y, flags, param):
    
        # If left mouse button is clicked, print image coordinates
        if (event == cv2.EVENT_LBUTTONDOWN):
            print([x, y])
    
    # Read an image
    image = cv2.imread('path/to/your/image.png', cv2.IMREAD_COLOR)
    
    # Set up window and mouse callback function
    cv2.namedWindow("image")
    cv2.setMouseCallback("image", print_coords)
    
    # Loop until the 'c' key is pressed
    while True:
    
        # Display image; wait for keypress
        cv2.imshow("image", image)
        key = cv2.waitKey(1) & 0xFF
    
        # If 'c' key is pressed, break from loop
        if  key == ord("c"):
            break
    
    cv2.destroyAllWindows()
    

    Some mouse clicking inside the window then gives such outputs:

    [158, 239]
    [63, 6]
    [9, 18]
    [59, 101]
    [128, 279]
    [298, 249]
    

    Now, of course, you can do whatever you want inside the mouse callback function. Pay attention, if you eventually need global variables.

    Hope that helps!