Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to get double click event in OpenCV for python

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

like image 219
Rajendrasinh Parmar Avatar asked Dec 30 '15 14:12

Rajendrasinh Parmar


People also ask

How does OpenCV Check mouse clicks?

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).

What does .read do OpenCV?

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.

What is import cv2 in Python?

OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2. imread() method loads an image from the specified file.


1 Answers

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)
like image 111
Kamil Szelag Avatar answered Oct 17 '22 00:10

Kamil Szelag