red0orange

red0orange

Opencv manually obtain the coordinates of a point in an image.

code#

import cv2


def click_event(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        print('Mouse click at coordinates : ', x, ',', y)
        param.append((x, y))

def get_click_coordinates(image):
    # create an empty list to store the coordinates
    coordinates = []

    # display the image in a window
    cv2.imshow('image', image)

    # set the callback function for mouse events
    cv2.setMouseCallback('image', click_event, param=coordinates)

    # wait until any key is pressed
    cv2.waitKey(0)

    # return the coordinates
    return coordinates

if __name__ == "__main__":
    ############# example #############
    image = cv2.imread("test.png")
    coordinates = get_click_coordinates(image)
    print(coordinates)

explain#

  • Input: An image in numpy format
  • Process: Click to display the position of the desired points in the image
  • Output: Returns a list containing the coordinates of the clicked points
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.