I N F O A R Y A N

OPENCV-PYTHON | Mouse events and Trackbars | OpenCV GUI | Mini Paint Application | Mouse Drawing

Mouse events and trackbars play a crucial role in creating interactive graphical user interfaces (GUIs) using OpenCV-Python. These features enable users to interact with images and videos dynamically, facilitating tasks such as image annotation, object selection, and parameter tuning. In this section, we’ll delve into the concepts of mouse events and trackbars, elucidating their functionalities, providing code examples for implementation in Python using OpenCV, and exploring their applications in building a mini paint application for mouse drawing.

Flow of the Article:

  1. Explaining the Mouse Events
  2. The callbacks 
  3. Explaining the Trackbars
  4. Implementing the project

 

Video Explanation:

 

Mouse Events:

Mouse events in OpenCV enable users to capture various interactions with images, such as mouse clicks, mouse movement, and mouse button releases. These events provide valuable information, including the position of the mouse cursor and the type of event triggered, allowing developers to implement custom functionalities based on user actions.

 

Event Types and Callbacks

OpenCV supports several types of mouse events, including:

Mouse Click: Triggered when a mouse button is pressed and released.
Mouse Movement: Triggered when the mouse is moved while a button is pressed.
Mouse Button Down: Triggered when a mouse button is pressed.
Mouse Button Up: Triggered when a mouse button is released.

 

OpenCV Code for Handling Mouse Events:

import cv2

# Mouse callback function
def draw_circle(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
cv2.circle(img, (x, y), 10, (0, 255, 0), -1)

# Create a black image
img = np.zeros((512, 512, 3), np.uint8)
cv2.namedWindow(‘image’)

# Set mouse callback function
cv2.setMouseCallback(‘image’, draw_circle)

while True:
cv2.imshow(‘image’, img)
if cv2.waitKey(20) & 0xFF == 27:
break

cv2.destroyAllWindows()

Uses:

  • Mouse events are utilized in applications such as image annotation, object selection, and interactive image processing.
  • They enable users to define regions of interest (ROIs), annotate keypoints, and interactively adjust parameters in real-time.

 

Trackbars:

Trackbars in OpenCV provide a graphical interface for adjusting parameter values dynamically. They consist of a slider and optional text labels, allowing users to control parameter settings by dragging the slider or typing numerical values. Trackbars are commonly used for parameter tuning, threshold adjustment, and real-time visualization of effects.

OpenCV Code for Creating Trackbars:

import cv2

# Trackbar callback function
def update_value(x):
print(‘Trackbar value:’, x)

# Create a black image
img = np.zeros((300, 512, 3), np.uint8)
cv2.namedWindow(‘image’)

# Create trackbars
cv2.createTrackbar(‘Trackbar’, ‘image’, 0, 255, update_value)

while True:
cv2.imshow(‘image’, img)
if cv2.waitKey(1) & 0xFF == 27:
break

cv2.destroyAllWindows()

Uses:

  • Trackbars are employed for parameter tuning in image processing algorithms, such as thresholding, filtering, and color manipulation.
  • They provide a user-friendly interface for adjusting parameters in real-time, facilitating experimentation and optimization.

 

 

Paint Application 

Find Code here: Github Repo

Algorithm for developing the apint application using the opencv:

This code implements a mini paint application using OpenCV-Python. Here’s a breakdown of how it works:

  1. Initialization:

    • The code initializes some variables like draw, mode, and a, b. draw is a boolean variable that indicates whether the mouse button is pressed or not. mode is a boolean variable that determines whether to draw rectangles or circles. a and b store the initial coordinates of the mouse click.
  2. Trackbar Callback Function:

    • The onChange() function is called whenever the trackbar value changes. It toggles the mode variable between drawing rectangles (value=0) and circles (value=1).
  3. Mouse Callback Function:

    • The draw_circle() function is called whenever a mouse event occurs. It handles mouse button presses, releases, and movements.
    • When the left mouse button is pressed (EVENT_LBUTTONDOWN), it sets draw to True and stores the initial coordinates (a, b).
    • When the mouse is moved (EVENT_MOUSEMOVE) with the left button pressed, it draws either a rectangle or a circle based on the current mode setting.
    • When the left mouse button is released (EVENT_LBUTTONUP), it sets draw to False and completes the drawing of either a rectangle or a circle.
  4. Main Loop:

    • The main loop continuously displays the image and listens for key presses.
    • Pressing the ‘m’ key toggles the drawing mode between rectangle and circle.
    • Pressing the ‘Esc’ key (27) exits the program.
  5. Trackbar Creation:

    • A trackbar named ‘shape’ is created in a separate window named ‘track’. This trackbar controls the drawing mode (rectangle or circle) using the onChange() function.
  6. Image Initialization:

    • An empty black image (img) is created to draw shapes on.
  7. Mouse Callback Registration:

    • The draw_circle() function is registered as the mouse callback function for the ‘image’ window.
  8. Window Display:

    • Inside the main loop, the ‘image’ window displays the current state of the image (img) where the user can draw shapes.

Overall, this code provides a simple interface for users to draw rectangles or circles on an image using mouse interactions and trackbar controls, demonstrating the versatility of OpenCV in creating interactive GUI applications.

Conclusion

In conclusion, the implementation of mouse events and trackbars in OpenCV-Python facilitates the development of interactive graphical user interfaces (GUIs) with enhanced functionality and user engagement. By leveraging mouse events, users can intuitively interact with images through actions such as mouse clicks, movements, and button releases, enabling tasks like annotation, object selection, and parameter adjustment. Additionally, trackbars offer a convenient means of dynamically controlling parameters and settings in real-time, empowering users to fine-tune image processing algorithms and visualize effects instantly. Together, these features enable the creation of applications such as mini paint tools, image annotation utilities, and parameter tuning interfaces, enhancing the user experience and enabling efficient image analysis and manipulation. Incorporating mouse events and trackbars into OpenCV-Python projects opens up a wide range of possibilities for interactive image processing applications across diverse domains, from computer vision and robotics to medical imaging and multimedia analysis.

For further exploration and reference, readers can consult the following links:
1. [OpenCV Documentation on Mouse Events]
2. [OpenCV Documentation on Trackbars]