forked from hibyby/GrandmaCan_python_opencv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject1.py
64 lines (50 loc) · 1.72 KB
/
project1.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
import cv2
import numpy as np
cap = cv2.VideoCapture(1)
# Blue Green Yellow
penColorHSV = [[86, 121, 205, 111, 245, 255],
[46, 78, 204, 71, 255, 255],
[22, 70, 214, 31, 255, 255]]
penColorBGR = [[255, 0, 0],
[0, 255, 0],
[0, 255, 255]]
# [x, y, colorId]
drawPoints = []
def findPen(img):
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
for i in range(len(penColorHSV)):
lower = np.array(penColorHSV[i][:3])
upper = np.array(penColorHSV[i][3:6])
mask = cv2.inRange(hsv, lower, upper)
result = cv2.bitwise_and(img, img, mask=mask)
penx, peny = findContour(mask)
cv2.circle(imgContour, (penx, peny), 10, penColorBGR[i], cv2.FILLED)
if peny!=-1:
drawPoints.append([penx, peny, i])
# cv2.imshow('result', result)
def findContour(img):
contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
x, y, w, h = -1, -1, -1, -1
for cnt in contours:
# cv2.drawContours(imgContour, cnt, -1, (255, 0, 0), 4)
area = cv2.contourArea(cnt)
if area > 500:
peri = cv2.arcLength(cnt, True)
vertices = cv2.approxPolyDP(cnt, peri * 0.02, True)
x, y, w, h = cv2.boundingRect(vertices)
return x+w//2, y
def draw(drawpoints):
for point in drawpoints:
cv2.circle(imgContour, (point[0], point[1]), 10, penColorBGR[point[2]], cv2.FILLED)
while True:
ret, frame = cap.read()
if ret:
imgContour = frame.copy()
# cv2.imshow('video', frame)
findPen(frame)
draw(drawPoints)
cv2.imshow('contour', imgContour)
else:
break
if cv2.waitKey(1) == ord('q'):
break