-
Notifications
You must be signed in to change notification settings - Fork 49
/
color-4.py
89 lines (65 loc) · 2.43 KB
/
color-4.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
82
83
84
85
86
87
88
89
#!/usr/bin/env python
"""color-4.py: Color detection using openCV."""
"""
Performance @ 640x480 resolution:
RMBP -> 0.005s each detection or 20hz
RPI 2 -> 0.15s each detection or 6.6hz
RPI 3 -> 0.124s each detection or 8.06hz
"""
__author__ = "Aldo Vargas"
__copyright__ = "Copyright 2015 Altax.net"
__license__ = "GPL"
__version__ = "1"
__maintainer__ = "Aldo Vargas"
__email__ = "[email protected]"
__status__ = "Development"
import numpy as np
import cv2
import time
#cam = cv2.VideoCapture(0)
cam = cv2.VideoCapture('slung-load.mp4')
cam.set(3,640)
cam.set(4,480)
while ( True ):
t1 = time.time()
ret, frame = cam.read()
hsv=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
# Blue
#color = cv2.inRange(hsv,np.array([100,50,50]),np.array([140,255,255]))
# Black
color = cv2.inRange(hsv,np.array([0,0,0]),np.array([150,150,150]))
# Green
#color = cv2.inRange(hsv,np.array([40,50,50]),np.array([80,255,255]))
# Red
#color = cv2.inRange(hsv,np.array([0,150,0]),np.array([5,255,255]))
# White
#sensitivity = 10
#color = cv2.inRange(hsv,np.array([0,0,255-sensitivity]),np.array([255,sensitivity,255]))
# Change to select color
image_mask=color
element = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
image_mask = cv2.erode(image_mask,element, iterations=2)
image_mask = cv2.dilate(image_mask,element,iterations=2)
image_mask = cv2.erode(image_mask,element)
contours, hierarchy = cv2.findContours(image_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
maximumArea = 10
bestContour = None
for contour in contours:
currentArea = cv2.contourArea(contour)
if currentArea > maximumArea:
bestContour = contour
maximumArea = currentArea
#Create a bounding box around the biggest color object
if bestContour is not None:
x,y,w,h = cv2.boundingRect(bestContour)
cv2.rectangle(frame, (x,y),(x+w,y+h), (0,0,255), 3)
t2 = time.time()
print "detection time = %gs x=%d,y=%d" % ( round(t2-t1,3) , x, y)
#output=cv2.bitwise_and(frame,frame,mask=image_mask)
cv2.imshow('Original',frame)
#cv2.imshow('Image Mask',image_mask)
#cv2.imshow('Output',output)
if cv2.waitKey(1) == 27:
break
cv2.destroyAllWindows()
cam.release()