Thursday, September 28, 2017

Unable to get double click event in OpenCV for python

Leave a Comment

OpenCV with python(MAC OS X EL Capitan)

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 event : 1 is mouse down and event: 4 is mouse up

2 Answers

Answers 1

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) 

Answers 2

I just tried running your code, to me everything seems fine. I click down and hold a 1 comes up, I release and there is a 4. When I double click there is a 7. This however doesn't work if the mouse is moving. Try keeping your mouse still while you double click or try another mouseTerminal is in the bottom right

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment