AIR CANVAS | Python OpenCV | Code & explanation | Computer Vision Projects
Imagine an artist standing before an invisible canvas, where each movement becomes a stroke of pure imagination, untethered by physical tools. This is the captivating realm of “Virtual Brushes in the Wind,” a fusion of OpenCV and Air Canvas that’s redefining art’s boundaries in a magical way.
How air canvas works ?
Ever wanted to draw your imagination by just waiving your finger in air. In this post we will learn to build an Air Canvas which can draw anything on it by just capturing the motion of a coloured marker with camera. Here a coloured object at tip of finger is used as the marker.
We will be using the computer vision techniques of OpenCV to build this project. The preffered language is python due to its exhaustive libraries and easy to use syntax but understanding the basics it can be implemented in any OpenCV supported language.
Here Colour Detection and tracking is used in order to achieve the objective. The colour marker in detected and a mask is produced. It includes the further steps of morphological operations on the mask produced which are Erosion and Dilation. Erosion reduces the impurities present in the mask and dilation further restores the eroded main mask.
🎥 Watch the Video:
Initialize:
- Import NumPy, OpenCV, and the deque module from the collections library.
- Define a placeholder callback function
setValues(x)
.
Trackbar Setup:
- Create a window named “Color detectors.”
- Create six trackbars for adjusting upper and lower HSV values.
Color Points and Indices Setup:
- Initialize deques to store color points for blue, green, red, and yellow.
- Initialize indices for each color.
Kernel and Colors Setup:
- Create a kernel for morphological operations.
- Define colors for blue, green, red, and yellow.
Canvas Setup:
- Create a canvas (
paintWindow
) for drawing. - Add rectangles and text for color buttons and clear button.
- Create a canvas (
Camera Initialization:
- Open the default camera (
cv2.VideoCapture(0)
).
- Open the default camera (
Main Loop:
- Continuously capture frames from the camera.
- Flip the frame horizontally.
- Convert the frame to the HSV color space.
Read Trackbar Values:
- Read trackbar positions for upper and lower HSV values.
Add Color Buttons to the Frame:
- Add rectangles and text for color buttons and clear button to the frame.
Identify Pointer:
- Create a mask based on the HSV values.
- Apply morphological operations to the mask.
- Find contours in the mask to identify the pointer.
Handle Button Clicks:
- If the pointer is near the color buttons or clear button, perform corresponding actions.
Draw Lines:
- Draw lines on both the frame and the canvas based on color points.
Show Windows:
- Display the tracking frame, paint window, and the mask.
Exit Condition:
- Break the loop and close the application if the ‘q’ key is pressed.
Release Resources:
- Release the camera and close all OpenCV windows.
Code Given at This Github Repository.
Air Canvas, Python OpenCV project, OpenCV Python, OpenCV projects, air canvas aryan verma, infoaryan, air canvas python project, Python projects for graduation
Please integrate this project with deep learning.
hey there i just used your code to run this project. I am facing some kind of problem regarding the map. It scans my body as well and sometimes the pointer draw some random stuff automatically.There is some kind of impurity in map.