-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaintProject.py
83 lines (65 loc) · 2.28 KB
/
paintProject.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Color detection and drawing on the screen
# Importing the required module
import cv2 as cv
import numpy as np
# creating points list
myPoints = [] # [x,y,colorId]
# Creating color database (lists)
colorsAvl = [[90,109,49,147,255,255],[10,179,0,255,0,255],[15,179,0,255,0,255]]
colorValue = [[255,0,0],[0,255,0],[0,0,255]]
# Creating draw on canvas function
def drawOnCanvas(myPoints,colorValues):
for point in myPoints:
cv.circle(resultedImg,(point[0],point[1]),5,colorValues[point[2]],cv.FILLED)
# Get contour function
def getContours(img):
contours,hierarchy = cv.findContours(img,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_NONE)
x,y,w,h=0,0,0,0
for cnt in contours:
area = cv.contourArea(cnt)
if area>50:
print()
print("Area =",area)
# cv.drawContours(resultedImg,cnt,-1,(0,255,0),2)
peri = cv.arcLength(cnt,True)
approx = cv.approxPolyDP(cnt,0.02*peri,True)
# creating bounding box around the detected images
x,y,w,h = cv.boundingRect(approx)
# cv.rectangle(resultedImg,(x,y),(x+w,y+h),(0,255,0),2)
return x+w//2,y
# Finding color and start drawing over the image
def findColor(img,colorsAvl,colorValues):
hsvImg = cv.cvtColor(img,cv.COLOR_BGR2HSV)
count = 0
newPoints = []
for color in colorsAvl:
lowerLimit = np.array(color[0:3])
upperLimit = np.array(color[3:6])
mask = cv.inRange(hsvImg,lowerLimit,upperLimit)
x,y=getContours(mask)
cv.circle(resultedImg,(x,y),5,colorValues[count],cv.FILLED)
if x!=0 and y!=0:
newPoints.append([x,y,count])
count += 1
return newPoints
# Access the webcam
frameWidth = 620
frameHeight = 480
brightness = 100
webcam = cv.VideoCapture(0)
webcam.set(3,frameWidth) # width
webcam.set(4,frameHeight) # height
webcam.set(10,brightness) # britness
while True:
success, img = webcam.read()
resultedImg = img.copy()
newPoints = findColor(img,colorsAvl,colorValue)
if len(newPoints) != 0:
for newPoint in newPoints:
myPoints.append(newPoint)
if len(myPoints) != 0:
drawOnCanvas(myPoints,colorValue)
cv.imshow("Resulted video",resultedImg)
# To quit
if cv.waitKey(1) & 0xFF == ord("q"):
break