Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3356531

Browse files
Merge pull request avinashkranjan#561 from mehabhalodiya/VP
Added virtual paint
2 parents 9550b47 + 12b69ef commit 3356531

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

‎Virtual Paint/README.md‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Project Description
2+
About: "Detecting Color and using colored marker to draw virtually."
3+
4+
It is an OpenCV application that can track an object’s movement, using which a user can draw on the screen by moving the object around — I call it Webcam Paint.
5+
6+
## Code Explanation
7+
8+
### Step 1: Initialize Some Stuff
9+
Firstly, I need to import the necessary libraries.
10+
11+
Then I initialize variables that are used in the following steps.
12+
13+
The *lower* and the *upper* numpy arrays help us in finding the colored cap. The *myColors* and *myColorValues* are used to create list of colors. The *myPoints* helps to set the coordinates of each of the color.
14+
15+
### Step 2: Start Reading The Video (Frame by Frame)
16+
Now I use the OpenCV function **cv2.VideoCapture()** method to read a video, frame by frame (using a while loop), either from a video file or from a webcam in real-time. In this case, I pass 0 to the method to read from a webcam.
17+
18+
### Step 3: Find The Contour-Of-Interest
19+
Once I start reading the webcam feed, I constantly look for a color object in the frames with the help of **cv2.inRange()** method and use the *upper* and *lower* variables initialized in Step 1. Once I find the contour(the if condition passes when a contour is found), I do a series of image operations and make it smooth. I use the center of the contour to draw on the screen as it moves. The **cv2.circle()** method draws a circle around it.
20+
21+
### Step 4: Start Drawing And Store The Drawings
22+
Now I start tracking coordinates of each and every point the center of the contour touches on the screen, along with its color.
23+
24+
I have created a *drawOnCanvas()* function to store the points in its respective color.
25+
26+
### Step 5: Show The Drawings On The Screen
27+
So far I stored all the points in their respective color. Now I just join all the points in each and every frame with a line and put it on a window we created using **cv2.imshow()** method and it all fits perfectly to work!
28+
29+
That’s it! I have successfully used a bunch of basic image processing tools and operations in OpenCV!

‎Virtual Paint/virtual_paint.py‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import cv2
2+
import numpy as np
3+
4+
frameWidth = 640
5+
frameHeight = 480
6+
cap = cv2.VideoCapture(0)
7+
cap.set(3, frameWidth)
8+
cap.set(4, frameHeight)
9+
cap.set(10, 150)
10+
11+
myColors = [[5, 107, 0, 19, 255, 255], # orange
12+
[133, 56, 0, 159, 156, 255], # purple
13+
[57, 76, 0, 100, 255, 255], # green
14+
[90, 48, 0, 118, 255, 255]] # blue
15+
16+
myColorValues = [[51, 153, 255], # BGR
17+
[255, 0, 255],
18+
[0, 255, 0],
19+
[255, 0, 0]]
20+
21+
myPoints = [] ## [x , y , colorId ]
22+
23+
24+
def findColor(img, myColors, myColorValues):
25+
imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
26+
count = 0
27+
newPoints = []
28+
for color in myColors:
29+
lower = np.array(color[0:3])
30+
upper = np.array(color[3:6])
31+
mask = cv2.inRange(imgHSV, lower, upper)
32+
x, y = getContours(mask)
33+
cv2.circle(imgResult, (x, y), 15, myColorValues[count], cv2.FILLED)
34+
if x != 0 and y != 0:
35+
newPoints.append([x, y, count])
36+
count += 1
37+
# cv2.imshow(str(color[0]),mask)
38+
return newPoints
39+
40+
41+
def getContours(img):
42+
contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
43+
x, y, w, h = 0, 0, 0, 0
44+
for cnt in contours:
45+
area = cv2.contourArea(cnt)
46+
if area > 500:
47+
# cv2.drawContours(imgResult, cnt, -1, (255, 0, 0), 3)
48+
peri = cv2.arcLength(cnt, True)
49+
approx = cv2.approxPolyDP(cnt, 0.02 * peri, True)
50+
x, y, w, h = cv2.boundingRect(approx)
51+
return x + w // 2, y
52+
53+
54+
def drawOnCanvas(myPoints, myColorValues):
55+
for point in myPoints:
56+
cv2.circle(imgResult, (point[0], point[1]), 10, myColorValues[point[2]], cv2.FILLED)
57+
58+
59+
while True:
60+
success, img = cap.read()
61+
imgResult = img.copy()
62+
newPoints = findColor(img, myColors, myColorValues)
63+
if len(newPoints) != 0:
64+
for newP in newPoints:
65+
myPoints.append(newP)
66+
if len(myPoints) != 0:
67+
drawOnCanvas(myPoints, myColorValues)
68+
69+
cv2.imshow("Result", imgResult)
70+
if cv2.waitKey(1) & 0xFF == ord('q'):
71+
break
72+
73+
cap.release()
74+
cv2.destroyAllWindows()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /