Air-canvas

 

Air canvas using Opencv python

    

     If u wanted to draw your imagination by just waving your finger in the 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 colored marker with a camera. Here a colored object at the tip of the finger is used as the marker.


     We will be using the computer vision techniques of OpenCV to build this project. The preferred 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 are used to achieve the objective. The color marker is 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.

Algorithm -

  1. First, we have to set HSV values of a particular color that we want to detect, and here I have detected blue color by default.
  2. Then we remove impurities in the detective color.
  3. Then we find the center of detecting color.
  4. Finally, we can drow and erase anything.

Requirements -

  •  python3 
  •  numpy 
  •  opencv 


Importing Libraries and Modules

 

import NumPy as np
import cv2
import imutils
from collections import deque

 

Defining boundaries for colors

 To track our marker, we need to define the boundaries of its color. For example, if we want to use a red-colored marker, we need to define the boundaries of red color. While for a blue marker, we need to define the boundaries of blue color. So, first, we will show what to do for a red marker and then, later on, we shall show the same for a blue one. 

red_lower_bound = np.array([0, 100, 100]) # HSV format
red_upper_bound = np.array([20, 255, 255])
lower_bound = red_lower_bound
upper_bound = red_upper_bound

 Next, we shall define our color palette. We are going to use red and blue colors in our palette to be used by our marker to paint in space.

# BGR format
# Blue Red Green Yellow White
color_list = [(255, 0, 0), (0, 0, 255), (0, 255, 0), (0, 255, 255), (255, 255, 255)]
# Blue Red
color_palette_list = [(255, 0, 0), (0, 0, 255)]
# index for the colors in our palette
idx = 0

 Now we shall use deques (doubly ended queues) to store the points of movement left in the trail by our marker. Since we are using red and blue colors in our palette, we shall use two such deques for the two colors.

We can adjust the length of deque depending on how long we want the trail of the marker to be.

trace_blue = [deque(maxlen=1500)]
trace_red = [deque(maxlen=1500)]
# indexes
idx_blue = 0
idx_red = 0

 Now we build some components so the captured screen looks like an actual canvas. So, we shall create tabs for switching the colors and for clearing previous drawings.

font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 0.5
t = 2
cam_frame = cv2.rectangle(cam_frame, (125,60), (275,120), (90,0,100), -1)
cv2.putText(cam_frame, "CLEAR", (170, 95), font, font_scale, color_list[4], t, cv2.LINE_AA)
cam_frame = cv2.rectangle(cam_frame, (425,60), (575,120), color_palette_list[0], -1)
cv2.putText(cam_frame, "BLUE", (480, 95), font, font_scale, color_list[4], t, cv2.LINE_AA)
cam_frame = cv2.rectangle(cam_frame, (725,60), (875,120), color_palette_list[1], -1)
cv2.putText(cam_frame, "RED", (785, 95), font, font_scale, color_list[4], t, cv2.LINE_AA)

 After this, we draw a circle to specify the position of the marker detected by our application. To read more on ‘moments’ and ‘contours’ in OpenCV

if len(contours) > 0:
cont = sorted(contours, key = cv2.contourArea, reverse = True)[0]
((x, y), radius) = cv2.minEnclosingCircle(cont)
cv2.circle(cam_frame, (int(x), int(y)), int(radius), color_list[2], 2)
M = cv2.moments(cont)
center = (int(M['m10'] / M['m00']), int(M['m01'] / M['m00']))

Finally, to paint on the canvas, we run this loop below across the deque values to trace the stored points. Now, the starting color of the pencil is blue. If we want to change it, we can do so by changing the order of colors in the color palette list.

traced = [trace_blue, trace_red]
for p in range(len(traced)):
for m in range(len(traced[p])):
for n in range(1, len(traced[p][m])):
if traced[p][m][n] is None:
continue
cv2.line(cam_frame, traced[p][m][n - 1], traced[p][m][n], color_palette_list[p], 2)
cv2.imshow("Canvas Drawing", cam_frame)
if cv2.waitKey(1) & 0xFF == ord("w"):
break
camera.release()
cv2.destroyAllWindows()

 

Github link for source code -

     https://github.com/rutvik-balar/air_canvas/blob/master/Air-canvas.py

Output:

Here, I have tried to draw a leaf. My marker is a blue-colored dart.





Conclusion -

     We have successfully drawn a normal leaf and erase it by using OpenCV and python. we learned how to drow in the air by just detecting color.

References -

  • OpenCV tutorials
  • All about python by code with harry
  • air canvas by GFG 

Comments

Popular posts from this blog

My First Internship in Web Design