I wrote the following script with OpenCV
import cv2
import numpy as npcap = cv2.VideoCapture(0)
ix, iy = -1, -1def draw_circle(event, x, y, flags, param):global ixglobal iyix,iy = x,yif 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:breakcv2.destroyAllWindows()
But when I do a right button click, no circle appears on the display. What is wrong with my code ?
This code snippet may help you get started:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
ix, iy = -1,-1def mouseCallback(event,x,y,flags,param):global ixglobal iyif event == cv2.EVENT_LBUTTONDOWN:ix = x # saves the position of the last clickiy = yelif 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 oncewhile(1):ret, img = cap.read()draw_circle_onscreen(img,ix,iy) # draws circle on screencv2.imshow('frame',img)if cv2.waitKey(20) & 0xFF == 27:breakcv2.destroyAllWindows()