python opencv鼠标点击图像显示坐标值(已验证)

python opencv鼠标点击图像显示坐标值(已验证)

直接上代码

import cv2
import numpy as np

img = cv2.imread("111.jpg")


# print img.shape

def on_EVENT_LBUTTONDOWN(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        xy = "%d,%d" % (x, y)
        print
        xy
        cv2.circle(img, (x, y), 1, (255, 0, 0), thickness=-1)
        cv2.putText(img, xy, (x, y), cv2.FONT_HERSHEY_PLAIN,
                    1.0, (255, 255, 255), thickness=1)
        cv2.imshow("image", img)


cv2.namedWindow("image",cv2.WINDOW_KEEPRATIO)
cv2.setMouseCallback("image", on_EVENT_LBUTTONDOWN)
cv2.imshow("image", img)

while (True):
    try:
        cv2.waitKey(100)
    except Exception:
        cv2.destroyWindow("image")
        break

cv2.waitKey(0)
cv2.destroyAllWindow()

运行结果:

python opencv鼠标点击图像显示坐标值(已验证)_第1张图片

 

你可能感兴趣的:(opencv,opencv,python,鼠标)