I'm creating a demo project to track mouse events in openCV. using standard mouseCallback from openCV.
following is my code for the same.
drawWithMouse.py
#!/usr/local/bin/local/python3
import numpy as np
import cv2 as cv
#Mouse callback function
def draw_shape(event,x,y,flags,param):
print("event : ",event)
if event == cv.EVENT_LBUTTONDBLCLK:
cv.circle(img,(x,y),100,(255,0,0),-1)
#Create a black image, a window and bind the function to the window
img = np.zeros((780,780,3),np.uint8)
cv.namedWindow('DrawWithMouse')
cv.setMouseCallback('DrawWithMouse',draw_shape)
while(1):
cv.imshow('DrawWithMouse',img)
if cv.waitKey(10) & 0xFF == 27: #ANDing with 0xFF as my machine is 64 bit
break
cv.destroyWindow('DrawWithMouse')
with this implementation i'm always getting mouse down and mouseup event and only single click event. i'm unable to get double click event(EVENT_LBUTTONDBLCLK). value for this constant is 7.
i'm getting following output
All you have do is to define a callback function in the OpenCV C++ code attaching to the OpenCV window. That callback function will be called every time, mouse events occur. That callback function will also give the coordinates of the mouse events. (e.g - (x, y) coordinate of a mouse click).
read() in OpenCV returns 2 things, boolean and data. If there are not 2 variables, a tuple will be assigned to one variable. The boolean is mostly used for error catching.
OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2. imread() method loads an image from the specified file.
You can try to workaround problem with time measurement, for example time.clock() (not precise butthe simplest) and calculation of time difference between click and previous one. If time is less than threshold perform double click action.
time =0
thresh = 1
#Mouse callback function
def draw_shape(event,x,y,flags,param):
print("event : ",event)
if event == cv.EVENT_LBUTTONDBLCLK:
if time.clock - time < thresh:
//double click
time = time.clock()
cv.circle(img,(x,y),100,(255,0,0),-1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With