red0orange

red0orange

Opencv 手动获取图像中某点的坐标

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#

  • 输入:一张 numpy 格式的图像
  • 过程:点击显示图像中希望得到的点的位置
  • 输出:返回一个包含点击点坐标的列表
加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。