forked from rk080299/Image-Processing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor_detect.py
50 lines (36 loc) · 1.29 KB
/
color_detect.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
import cv2
import numpy as np
cap=cv2.VideoCapture(0)
while(1):
_, img = cap.read()
hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
blue_lower=np.array([110,115,150])
blue_upper=np.array([130,255,255])
blue=cv2.inRange(hsv,blue_lower,blue_upper)
bmask = cv2.GaussianBlur(blue, (5,5),0)
kernal=np.ones((5,5))
blue=cv2.erode(blue,kernal)
blue=cv2.dilate(blue,kernal)
res=cv2.bitwise_and(img,img,mask=blue)
(_,contours, hierarchy) = cv2.findContours(blue,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
if len(contours)>0:
largest = 0
area = 0
for i in range(len(contours)):
temp_area = cv2.contourArea(contours[i])
if temp_area > area:
area = temp_area
largest = i
coordinates = cv2.moments(contours[largest])
x = int(coordinates['m10']/coordinates['m00'])
y = int(coordinates['m01']/coordinates['m00'])
diam = int(np.sqrt(area)/4)
cv2.circle(res,(x,y),diam,(0,255,0),1)
cv2.line(res,(x-2*diam,y),(x+2*diam,y),(0,255,0),1)
cv2.line(res,(x,y-2*diam),(x,y+2*diam),(0,255,0),1)
cv2.imshow("color Tracking",img)
cv2.imshow("rk",res)
if cv2.waitKey(10) & 0xFF ==ord('q'):
cap.release()
cv2.destroyAllWindows()
break