我用OpenCV编写了以下脚本
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
ix, iy = -1, -1
def draw_circle(event, x, y, flags, param):
global ix
global iy
ix,iy = x,y
if event == cv2.EVENT_LBUTTONDOWN:
cv2.circle(img, (200, 399), 10, (0, 255, 255), -1)
print("Text Put")
elif event == cv2.EVENT_LBUTTONUP:
print("EVENT_LBUTTONUP")
elif event == cv2.EVENT_RBUTTONDOWN:
print("Appuyé Droite")
elif event == cv2.EVENT_RBUTTONUP:
print("EVENT_RBUTTONUP")
elif event == cv2.EVENT_MOUSEMOVE:
print("Move on", x, y)
while(1):
ret, img = cap.read()
cv2.setMouseCallback('image', draw_circle)
cv2.imshow('image', img)
if cv2.waitKey(20) & 0xFF == 27:
break
cv2.destroyAllWindows()
但是当我按下右键时,显示屏上不会出现任何圆圈。我的代码出了什么问题?
答案 0 :(得分:2)
此代码段可以帮助您入门:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
ix, iy = -1,-1
def mouseCallback(event,x,y,flags,param):
global ix
global iy
if event == cv2.EVENT_LBUTTONDOWN:
ix = x # saves the position of the last click
iy = y
elif event == cv2.EVENT_LBUTTONUP:
print("EVENT_LBUTTONUP")
elif event == cv2.EVENT_RBUTTONDOWN:
print("Appuyé Droite")
elif event == cv2.EVENT_RBUTTONUP:
print("EVENT_RBUTTONUP")
def draw_circle_onscreen(frame, x,y):
cv2.circle(frame, (x,y), 10,(0, 0, 255),-1)
cv2.namedWindow('frame')
cv2.setMouseCallback('frame',mouseCallback) # mouse callback has to be set only once
while(1):
ret, img = cap.read()
draw_circle_onscreen(img,ix,iy) # draws circle on screen
cv2.imshow('frame',img)
if cv2.waitKey(20) & 0xFF == 27:
break
cv2.destroyAllWindows()