By Payal Nagar
The Air Canvas project utilizes OpenCV for real-time image processing, enabling users to draw in the air by detecting blue-colored objects.
This Python code creates a simple drawing application using OpenCV and Python. It allows the user to draw on a canvas using different colors by detecting colored objects (e.g., markers) in front of a webcam. Here's a step-by-step description of the code:
Importing Libraries:
The code imports necessary libraries: numpy as np, cv2 (OpenCV), and deque from the collections module.
Trackbars Setup:
The code creates a window titled "Color detectors" with six trackbars for adjusting upper and lower HSV (Hue, Saturation, Value) values. These trackbars allow the user to define the color range they want to detect.
Initializing Variables:
Several variables and data structures are initialized:
Lists (bluepoints, greenpoints, redpoints, yellowpoints) to store drawing points for different colors.
Indices (blue_index, green_index, red_index, yellow_index) to keep track of the current point in each color's list.
A kernel for morphological operations.
A list of colors.
A variable (colorIndex) to track the selected color.
A blank canvas (paintWindow) to display the drawing.
Webcam Capture:
The code sets up video capture from the default camera (camera index 0).
Main Loop:
Inside the main loop:
It continuously captures frames from the webcam feed and flips them horizontally.
Converts the frame to the HSV color space for color detection.
Color Detection:
Based on the trackbar values, it creates an HSV mask to detect objects of a specific color within the specified HSV range.
Morphological operations (erosion and dilation) are applied to clean up the mask.
Contour Detection:
It detects contours in the mask and finds the most prominent outline, assuming it corresponds to the colored object (e.g., marker).
The code calculates the center of the detected contour.
User Interaction:
Depending on the position of the center:
If it's in the top row of the frame, the user can interact with color selection and canvas clearing.
If it's in the lower part of the frame, it registers the points for drawing based on the selected color.
Drawing:
The code tracks and draws the user's hand movements on the canvas, updating the appropriate color's point list.
Display:
It displays the webcam feed with the detected contours and the drawing canvas.
Termination:
The loop continues until the user presses the "e" key, releasing the webcam and closing all OpenCV windows.
In summary, this code creates a basic interactive drawing application that uses color detection to select drawing colors. Users can draw on a virtual canvas by moving colored objects (markers) in front of a webcam, and the drawing is displayed in real time on the screen. It provides options for selecting different colors and clearing the canvas.
Submitted by Payal Nagar (Payal)
Download packets of source code on Coders Packet
Comments