Thursday, September 14, 2023

User Interaction in OpenCV

OpenCV provides functions that allow users to interact with images and videos in various ways. In previous sections cv2.waitKey() function has been used to accept user input from the keyboard and the ESC key can be detected by checking the key code returned by the function. And cv2.destroy AllWindows() function closes all the windows that were created using the cv2.imshow(). These are very simple user interaction functions. In addition, OpenCV provides several other functions for user interaction but we will look some commonly used ones.

Mouse Operations

OpenCV provides a facility to control and manage a variety of mouse events, and it provides the flexibility for programmers to manipulate user interactions. It can capture the events generated by mouse operations, such as left-button-down, rightbutton-down, mouse-move and double-click etc. The callback functions can be used to capture these mouse events.

Below code snippets will print out the list of all mouse events that OpenCV supports:

1 import cv2

2

3 for event in dir(cv2):

4 if “EVENT” in event:

5 print(event)

It displays a list of mouse events which is outlined in below table:


This example will show how to use the callback function to capture the mouse events. Load and show an image, when a mouse click happens on the image, the x, y coordinates of the pixel and the blue, green and red values will be printed on a separate window.

1 def mouse_event(event, x, y, flags, param):

2 if event == cv2.EVENT_LBUTTONDOWN:

3 blue = img[y, x, 0]

4 green = img[y, x, 1]

5 red = img[y, x, 2]

6 mycolorImage = np.zeros((100, 280, 3), np.uint8)

7 mycolorImage[:] = [blue, green, red]

8 strBGR = "(B,G,R) =

(" + str(blue) + ", "+str(green)+",

" + str(red) + ")"

9 strXY = "(X,Y) = ("+str(x)+", "+str(y)+")"

10 txtFont = cv2.FONT_HERSHEY_COMPLEX

11 txtColor = (255, 255, 255)

12 cv2.putText(mycolorImage,strXY, (10,30), txtFont, .6, txtColor,1)

13 cv2.putText(mycolorImage,strBGR,(10,50), txtFont,.6, txtColor,1)

14 cv2.imshow("color", mycolorImage)

15

16 if __name__ == '__main__':

17 img = cv2.imread("../res/flower003.jpg")

18 cv2.imshow("image", img)

19 cv2.setMouseCallback("image", mouse_event)

20 cv2.waitKey(0)

21 cv2.destroyAllWindows()

Let's understand the code through following explanation-


Execute the code, the image is loaded and displayed, whenever a mouse click happens on the image, the pixel’s coordinates (x, y) and color values (B, G, R) are displayed in a separate window, and the background color is filled with the color of the pixel, as shown in Figure below-






Share:

0 comments:

Post a Comment