想通过连续点击图片,在图片上显示点击的点和坐标,便于以后抠图用。
使用opencv python,定义一个供setMouseCallback使用的回调函数,这个回调函数在捕获到鼠标左键点击事件时,就在图片上点击处绘制一个实心的圆、并显示出坐标。
# coding: utf-8
import cv2
import numpy as np
img = cv2.imread("E:\\workspace\\cvimg\\1.png")
#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, (0,0,0), thickness = 1)
cv2.imshow("image", img)
cv2.namedWindow("image")
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()