Hello. This is codingwalks.
Event handling is an important element that enables interaction between users and programs. OpenCV also provides this event handling function to allow more intuitive manipulation of images and videos. It is possible to perform certain actions by detecting mouse clicks or keyboard inputs, or to control frames by dynamically changing values through the trackbar. In particular, the ability to move directly to a desired frame using the trackbar during video playback can be useful in video editing programs or analysis tools.
In this article, we will look at how to handle mouse and keyboard events using Python and OpenCV, and how to move video frames using the trackbar.
1. Mouse Event Processing
In OpenCV, you can process mouse events that occur in a window using the setMouseCallback() callback function. In addition, if you pass a parameter to param as a parameter of the function, you can use the passed variable to perform additional tasks. There are various events for mouse events, and the frequently used events are as follows.
Frequently Used Events
cv2.EVENT_LBUTTONDOWN | When the left mouse button is pressed |
cv2.EVENT_RBUTTONDOWN | When the right mouse button is pressed |
cv2.EVENT_MOUSEMOVE | When the mouse cursor moves within the window |
Frequently used flags
cv2.EVENT_FLAG_LBUTTON | Left mouse button pressed |
cv2.EVENT_FLAG_RBUTTON | Right mouse button pressed |
cv2.EVENT_FLAG_MBUTTON | Middle mouse button pressed |
cv2.EVENT_FLAG_CTRLKEY | Ctrl key pressed |
cv2.EVENT_FLAG_SHIFTKEY | Shift key pressed |
cv2.EVENT_FLAG_ALTKEY | Alt key pressed |
Keyboard event handling example code
import cv2
import numpy as np
# Mouse event handler function
def mouse_event(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
print(f"Left button clicked at ({x}, {y})")
if flags & cv2.EVENT_FLAG_SHIFTKEY:
cv2.rectangle(param, (x-5,y-5), (x+5,y+5), (0,0,255), 2)
else:
cv2.circle(param, (x,y), 5, (0,0,255), 2)
elif event == cv2.EVENT_RBUTTONDOWN:
print(f"Right button clicked at ({x}, {y})")
cv2.circle(param, (x, y), 5, (0, 0, 0), 2)
elif event == cv2.EVENT_MOUSEMOVE:
print(f"Mouse moved at ({x}, {y})")
cv2.circle(param, (x, y), 1, (0, 255, 0), 2)
# Create a blank image
img = 255 * np.ones((300, 300, 3), dtype=np.uint8)
# Create an empty window
cv2.namedWindow('Mouse Event Window')
# Setting the mouse callback function
cv2.setMouseCallback('Mouse Event Window', mouse_event, img)
while True:
cv2.imshow('Mouse Event Window', img)
# Exit with ESC key
if cv2.waitKey(1) & 0xFF == 27:
break
cv2.destroyAllWindows()
Additional mouse events available
cv2.EVENT_LBUTTONUP | Triggered when the left mouse button is released |
cv2.EVENT_RBUTTONUP | Triggered when the right mouse button is released |
cv2.EVENT_LBUTTONDBLCLK | Triggered when the left mouse button is double-clicked |
cv2.EVENT_RBUTTONDBLCLK | Triggered when the right mouse button is double-clicked |
cv2.EVENT_MBUTTONDOWN | Triggered when the middle (wheel) mouse button is pressed |
cv2.EVENT_MBUTTONUP | Triggered when the middle mouse button is released |
cv2.EVENT_MBUTTONDBLCLK | Triggered when the middle mouse button is double-clicked |
cv2.EVENT_MOUSEWHEEL | Triggered when the mouse wheel is rolled up (omnidirectional) |
cv2.EVENT_MOUSEHWHEEL | Triggered when the mouse wheel is moved left or right (horizontal) |
These additional mouse events allow you to detect and process a wider range of mouse inputs, such as using a double-click to quickly execute a specific function, or implementing zooming with the mouse wheel.
Example code to handle all mouse events
import cv2
import numpy as np
# A function that handles all mouse events
def mouse_event(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
print(f"Left button down at ({x}, {y})")
elif event == cv2.EVENT_LBUTTONUP:
print(f"Left button released at ({x}, {y})")
elif event == cv2.EVENT_RBUTTONDOWN:
print(f"Right button down at ({x}, {y})")
elif event == cv2.EVENT_RBUTTONUP:
print(f"Right button released at ({x}, {y})")
elif event == cv2.EVENT_LBUTTONDBLCLK:
print(f"Left button double clicked at ({x}, {y})")
elif event == cv2.EVENT_RBUTTONDBLCLK:
print(f"Right button double clicked at ({x}, {y})")
elif event == cv2.EVENT_MBUTTONDOWN:
print(f"Middle button down at ({x}, {y})")
elif event == cv2.EVENT_MBUTTONUP:
print(f"Middle button released at ({x}, {y})")
elif event == cv2.EVENT_MBUTTONDBLCLK:
print(f"Middle button double clicked at ({x}, {y})")
elif event == cv2.EVENT_MOUSEMOVE:
print(f"Mouse moved to ({x}, {y})")
elif event == cv2.EVENT_MOUSEWHEEL:
print(f"Mouse wheel scrolled vertically at ({x}, {y})")
elif event == cv2.EVENT_MOUSEHWHEEL:
print(f"Mouse horizontal wheel scrolled at ({x}, {y})")
# Create an empty window
cv2.namedWindow('Mouse Event Window')
# Setting the mouse callback function
cv2.setMouseCallback('Mouse Event Window', mouse_event)
while True:
# Display a blank image in the window
img = 255 * np.ones((300, 300, 3), dtype=np.uint8)
cv2.imshow('Mouse Event Window', img)
# Exit with ESC key
if cv2.waitKey(1) & 0xFF == 27:
break
cv2.destroyAllWindows()
2. Handling Keyboard Events
OpenCV can handle keyboard events using the waitKey() function. You can specify the action that occurs when a specific key is pressed. Typically, the ord() function is used to compare the ASCII values of specific characters.
Example code for handling keyboard events
import cv2
import numpy as np
cv2.namedWindow('Keyboard Event Window')
while True:
img = 255 * np.ones((300, 300, 3), dtype=np.uint8)
cv2.imshow('Keyboard Event Window', img)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
print("Key 'q' pressed - Exiting")
break
elif key == ord('s'):
print("Key 's' pressed - Saving image")
cv2.imwrite('saved_image.jpg', img)
cv2.destroyAllWindows()
3. Using the Trackbar
Trackbar is a tool that allows you to create a slider in OpenCV and dynamically adjust variable values through it. Trackbar is created using the createTrackbar() function. This allows you to change variable values in real time based on user input.
Important arguments when creating a trackbar:
Trackbar name
Window name to which the trackbar belongs
Set minimum and maximum values
Callback function to be called when the trackbar position changes
Trackbar Example Code (Simple Slider)
import cv2
import numpy as np
# Trackbar callback function
def on_trackbar(val):
print(f"Trackbar value: {val}")
cv2.namedWindow('Trackbar Window')
# Create trackbar (name, window name, initial value, maximum value, callback function)
cv2.createTrackbar('Slider', 'Trackbar Window', 0, 100, on_trackbar)
while True:
img = 255 * np.ones((300, 300, 3), dtype=np.uint8)
cv2.imshow('Trackbar Window', img)
if cv2.waitKey(1) & 0xFF == 27:
break
cv2.destroyAllWindows()
4. Controlling video frame movement with the trackbar
You can implement the ability to move to a specific frame in a video file using the trackbar. This makes it easier to control video playback.
Example code for controlling a video file with the trackbar
import cv2
# Trackbar callback function (frame change)
def on_trackbar(val):
global cap
cap.set(cv2.CAP_PROP_POS_FRAMES, val)
# Load video file
cap = cv2.VideoCapture('resources/TownCentreXVID.mp4')
# Check if the video file is opened
if not cap.isOpened():
print("Error: Unable to open video file")
exit()
# Total number of frames
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
# Create a window and set up the trackbar
cv2.namedWindow('Video Control')
cv2.createTrackbar('Frame', 'Video Control', 0, total_frames - 1, on_trackbar)
while True:
ret, frame = cap.read()
# End when end of video file is reached
if not ret:
break
# Set current trackbar position
current_frame = int(cap.get(cv2.CAP_PROP_POS_FRAMES))
cv2.setTrackbarPos('Frame', 'Video Control', current_frame)
# Frame display
cv2.imshow('Video Control', frame)
# Exit with ESC key
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
5. Conclusion
We have learned how to utilize mouse events, keyboard events, and trackbars in OpenCV. These event handling and interfaces allow us to control images and videos in a more interactive way. In particular, the ability to control video frames through trackbars is practical and can be applied to a variety of applications. We hope that this example will help you understand event handling and user interface implementation in OpenCV.
If you found this post useful, please like and subscribe below. ^^
★ All contents are referenced from the link below. ★
'Lecture > OpenCV Master with Python (Beginner)' 카테고리의 다른 글
OpenCV + Python Angle Measuring Instrument (Practical) (0) | 2024.10.23 |
---|---|
OpenCV + Python Outputting Unicode Fonts (0) | 2024.10.23 |
OpenCV + Python Various drawing functions and character output (0) | 2024.10.23 |
OpenCV + Python Processing images, videos, webcams, RTSP and RTMP streams (0) | 2024.10.23 |
OpenCV + Python Introduction and installation process (0) | 2024.10.23 |