import cv2
clicked = False
def onMouse(event, x, y, flags, param):
global clicked
if event == cv2.EVENT_LBUTTONUP:
clicked = True
cameraCapture = cv2.VideoCapture(0)
cv2.namedWindow('MyWindow')
cv2.setMouseCallback('MyWindow', onMouse)
success, frame = cameraCapture.read()
while success and cv2.waitKey(1) == 255:
keycode = cv2.waitKey(1)
cv2.imshow('MyWindow',frame)
success, frame = cameraCapture.read()
cameraCapture.release()
这里的cv2.waitKey(1) == 255的255我是在Ubuntu是测试的结果。也有人说是bug。
本文介绍如何使用Python和OpenCV库实现摄像头捕获图像并响应鼠标点击事件的功能。通过定义一个回调函数来捕捉鼠标左键释放的动作,并设置一个全局变量用以标记是否已点击。文中展示了具体的代码实现细节,包括初始化摄像头、创建窗口、设置鼠标回调函数等步骤。
2181





