-
Notifications
You must be signed in to change notification settings - Fork 1
/
multi.py
53 lines (33 loc) · 1.29 KB
/
multi.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
#importing modules
import cv2
import numpy as np
#capturing video through webcam
cap=cv2.imread('d.jpg')
while(1):
_, img = cap.read()
#converting frame(img i.e BGR) to HSV (hue-saturation-value)
hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
#definig the range of red color
red_lower=np.array([136,87,111],np.uint8)
red_upper=np.array([180,255,255],np.uint8)
#finding the range of red,blue and yellow color in the image
red=cv2.inRange(hsv, red_lower, red_upper)
#Morphological transformation, Dilation
kernal = np.ones((5 ,5), "uint8")
red=cv2.dilate(red, kernal)
res=cv2.bitwise_and(img, img, mask = red)
#Tracking the Red Color
(_,contours,hierarchy)=cv2.findContours(red,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for pic, contour in enumerate(contours):
area = cv2.contourArea(contour)
if(area>300):
x,y,w,h = cv2.boundingRect(contour)
img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
cv2.putText(img,"RED color",(x,y),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0,0,255))
#cv2.imshow("Redcolour",red)
cv2.imshow("Color Tracking",img)
#cv2.imshow("red",res)
if cv2.waitKey(10) & 0xFF == ord('q'):
cap.release()
cv2.destroyAllWindows()
break