Site icon TechVidvan

Create Air Canvas using Python Open CV

air canvas opencv python project

We know that artists create paintings on a canvas. But what if we can paint on air just by waving our hands. So, in this project, we are going to build an air canvas using OpenCV and Python.

OpenCV is an open-source computer vision library for performing various advanced image processing tasks.

We’ll use color detection and segmentation techniques to achieve this objective.

Here, we’re going to use a green object to simulate a pencil for the canvas.

So what is Color Detection and Segmentation in Image Processing?

Project Prerequisites:

1. Python – 3.x (We used 3.8.8 for this project)
2. OpenCV – 4.4

3. Numpy – 1.20.1

Download Air Canvas OpenCV Python Code

Please download the source code of python air canvas project: Air Canvas OpenCV Project Code

Steps to develop air canvas project using opencv:

Step 1 – Import necessary packages and pre-define some settings:

To build this project, we need only two packages, OpenCV and Numpy. So first we’ve to import these.

# Import necessary packages.
import cv2
import numpy as np

Step 2 – Read frames from a webcam:

# Create videocapture object
cap = cv2.VideoCapture(0)

while True:
    # Read each frame from webcam
    success, frame = cap.read()

    # Flip the frame
    frame = cv2.flip(frame, 1)

    cv2.imshow("Frame", frame)

    # Open the OpenCV window until 'q' is pressed
    if cv2.waitKey(1) == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Step 3 – Create the canvas window:

# Define various colors
colors = [(255, 0, 0), (255, 0, 255), (0, 255, 0), (0, 0, 255), (0, 255, 255)]
color = colors[0]

We want to change the pencil color during painting, That’s why we have to create some buttons in the frame. Using those buttons we can select the pencil color during painting in real-time.

# Adding the colour buttons to the live frame for colour access
    cv2.rectangle(frame, (20,1), (120,65), (122,122,122), -1)
    cv2.rectangle(frame, (140,1), (220,65), colors[0], -1)
    cv2.rectangle(frame, (240,1), (320,65), colors[1], -1)
    cv2.rectangle(frame, (340,1), (420,65), colors[2], -1)
    cv2.rectangle(frame, (440,1), (520,65), colors[3], -1)
    cv2.rectangle(frame, (540,1), (620,65), colors[4], -1)
    cv2.putText(frame, "CLEAR ALL", (30, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, 
(255, 255, 255), 2, cv2.LINE_AA)
    cv2.putText(frame, "BLUE", (155, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
 (255, 255, 255), 2, cv2.LINE_AA)
    cv2.putText(frame, "VIOLET", (255, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
 (255,255, 255), 2, cv2.LINE_AA)
    cv2.putText(frame, "GREEN", (355, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, 
(255, 255, 255), 2, cv2.LINE_AA)
    cv2.putText(frame, "RED", (465, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
 							(255, 255, 255), 2, cv2.LINE_AA)
    cv2.putText(frame, "YELLOW", (555, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, 
(255,255,255), 2, cv2.LINE_AA)

Output:

Step 4 – Detect the green color:

Here we’re using a green object to simulate the pencil of our canvas. OpenCV reads frames as BGR color space. But to detect colors, we have to convert the frame to the HSV color space.

But what is HSV color-space?

HSV stands for HUE, SATURATION, and VALUE (or brightness). It is basically a cylindrical color space.

Note: This image is taken from google.

So let’s detect the green object:

# Color range for detecting green color
   lower_bound = np.array([50,100,100])
   upper_bound = np.array([90,255,255])

     # Convert the frame BGR to HSV color space
   hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
   
   # Create a binary segmented mask of green color
   mask = cv2.inRange(hsv, lower_bound, upper_bound)

   # Add some dialation to increase segmented area
   mask = cv2.dilate(mask, kernel, iterations=1)

Output:

 # Find all the contours of the segmented mask
    contours, h    = cv2.findContours(mask, cv2.RETR_TREE, 
cv2.CHAIN_APPROX_SIMPLE)

Now, let’s find the center of the detected object. The center point will be the reference point with which we’ll be painting something on the canvas.

# Checking if any contour is detected then run the following statements
    if len(contours) > 0:
        
        # Get the biggest contour from all the detected contours
        cmax = max(contours, key = cv2.contourArea)

    else:
        previous_center_point= 0
# Find the area of the contour
        area = cv2.contourArea(cmax)

It may be possible that the main object is not present in the frame but some noises are detected. So to fix this we need to filter that as well.

min_area = 1000
          # Checking if the area of the contour is greater than a threshold
        if area > min_area:

            # Find center point of the contour
            M = cv2.moments(cmax)
            cX = int(M["m10"] / M["m00"])
            cY = int(M["m01"] / M["m00"])
            
            # Drawing a circle in the center of the contour area
            cv2.circle(frame, (cX, cY), 10, (0, 0, 255), 2)

Output:

Now we’ve successfully detected our pencil. So let’s do the most exciting thing of the project.

Step 5 – Draw on the canvas:

To draw on the canvas first we’ve to select a color for drawing.

# Selecting the color for drawing in the canvas
            if previous_center_point== 0:
                if cY < 65:
                    # Clear all
                    if cX > 20 and cX < 120:
                        canvas = np.zeros((height, width, 3), np.uint8)
                    
                    elif cX > 140 and cX < 220:
                        color = colors[0]

                    elif cX > 240 and cX < 320:
                        color = colors[1]
                    
                    elif cX > 340 and cX < 420:
                        color = colors[2]
                    
                    elif cX > 440 and cX < 520:
                        color = colors[3]
                    
                    elif cX > 540 and cX < 620:
                        color = colors[4]
# If drawing is started then draw a line between each frames detected contour 
#  center point
            if previous_center_point!= 0:
                cv2.line(canvas, previous_center_point, (cX, cY), color, 2)

            # Update the center point
            previous_center_point= (cX, cY)

But we want to show the drawings in the original frame. So let’s see how we can do that also.

# Adding the canvas mask to the original frame
            canvas_gray = cv2.cvtColor(canvas, cv2.COLOR_BGR2GRAY)

            _, canvas_binary = cv2.threshold(canvas_gray, 20, 255,
 cv2.THRESH_BINARY_INV)

            canvas_binary = cv2.cvtColor(canvas_binary, cv2.COLOR_GRAY2BGR)

            frame = cv2.bitwise_and(frame, canvas_binary)
            frame = cv2.bitwise_or(frame, canvas)

Air Canvas OpenCV Output

Summary:

In this project, we’ve built an Air canvas using OpenCV – Python. We’ve learned about color detection and segmentation techniques, thresholding techniques, some logical operations, and some other image processing techniques through this project.

Exit mobile version