Objective:
Learn to use the mouse as paint-brush In computer vision( cv2.setMouseCallback() )
Introduction:
In
the world of computer vision, We need to develop computer vision
application, which has a flexible feature and usage. It also provides
many features like selection of the region of interest, drawing the
the shape on image and like that many features is there etc.
List of all key events in OpenCV:
EVENT_FLAG_ALTKEY
It represents the Alt Key on Keyboard.
EVENT_FLAG_CTRLKEY
It represents the Ctrl Key on Keyboard.
EVENT_FLAG_LBUTTON
Its uses for left button and returns boolean to check whether left button is pressed or not.
EVENT_FLAG_MBUTTON
Its uses for Middle scroll button and return boolean to check whether the Scroll button is pressed or not.
EVENT_FLAG_RBUTTON
Its uses for Right button and return boolean to check whether Right button is pressed or not.
EVENT_FLAG_SHIFTKEY
It represents the shift key on the keyboard and checks for the click event of the shift button.
EVENT_LBUTTONDBLCLK
It represents the left button event in mouse and activates when we press the left button of the mouse two times.
EVENT_LBUTTONDOWN
It represents the left button event in mouse and activates when we press the left button of the mouse down.
EVENT_LBUTTONUP
It represents the left button event in mouse and activates when we release the left button of the mouse Up.
EVENT_MBUTTONDBLCLK
It represents the middle button event in mouse and activates when we press the Middle button of the mouse two times.
EVENT_MBUTTONDOWN
It represents the middle button event in mouse and activates when we release the middle button of the mouse down.
EVENT_MBUTTONUP
It represents the middle button event in mouse and activates when we release the middle button of the mouse up.
EVENT_MOUSEMOVE
It will activate when we move the mouse over the image.
EVENT_MOUSEWHEEL
It represents the scroll button event in mouse and activates when we use the scroll wheel.
EVENT_RBUTTONDBLCLK
It represents the Right button event in mouse and activates when we press the Right button two times.
EVENT_RBUTTONDOWN
It represents the Right button event in mouse and activates when we press the Right button down.
EVENT_RBUTTONUP
It represents the Right button event in mouse and activates when we press the Right button up.
These events help the user to customise the mouse command and modify computer vision application as per there need.
Now, let us take an example of some code:
We can draw the square in the picture use these events, some sample code:
Code:
import cv2
import numpy as np
# mouse callback function
def draw_rectangle(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDBLCLK:
cv2.rectangle(img,(x,y),(x+20, y+20),(255,0,0),-1)
# Create a black image, a window and bind the function to window
img = np.zeros((256,256,3), np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_rectangle)
if __name__ == "__main__":
while(1):
cv2.imshow('image',img)
if cv2.waitKey(20) & 0xFF == 27:
break
cv2.destroyAllWindows()
Output:
***********************************Article Written by Abhishek Gupta*************************************
Comments