-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect-rect.py
More file actions
executable file
·65 lines (51 loc) · 2.07 KB
/
detect-rect.py
File metadata and controls
executable file
·65 lines (51 loc) · 2.07 KB
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
import cv2
import numpy as np
def nothing(x):
pass
cv2.namedWindow('mask')
cv2.namedWindow('info')
# create trackbars for color change
# A HSV range for a red color - 90, 100, 100 - 255, 255, 255
# one of the variants [ 90 , 113 , 81 ] - [ 255 , 255 , 255 ]
cv2.createTrackbar('H-Low','mask', 90, 255, nothing)
cv2.createTrackbar('H-High','mask', 255, 255, nothing)
cv2.createTrackbar('S-Low','mask', 113, 255, nothing)
cv2.createTrackbar('S-High','mask', 255, 255, nothing)
cv2.createTrackbar('L-Low','mask', 81, 255, nothing)
cv2.createTrackbar('L-High','mask', 255, 255, nothing)
filename = 'stouch-video-rgb-mapped.jpg'
img = cv2.imread(filename)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
infoImage = np.zeros([480,640,3])
while(1):
h_low = cv2.getTrackbarPos('H-Low','mask')
h_high = cv2.getTrackbarPos('H-High','mask')
s_low = cv2.getTrackbarPos('S-Low','mask')
s_high = cv2.getTrackbarPos('S-High','mask')
l_low = cv2.getTrackbarPos('L-Low','mask')
l_high = cv2.getTrackbarPos('L-High','mask')
hsv_lower = np.array([h_low, s_low, l_low])
hsv_upper = np.array([h_high, s_high, l_high])
mask = cv2.inRange(hsv, hsv_lower, hsv_upper)
lower_format = str((h_low,s_low,l_low))
upper_format = str((h_high,s_high,s_high))
hsv_range = ''.join(["[",lower_format," - ",upper_format,"]"])
cv2.putText(infoImage, hsv_range, (20,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0,0,255) , 2)
print hsv_range
cv2.imshow("info", infoImage)
cv2.imshow("mask", mask)
#im2, contours, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#cnt = contours[0]
#epsilon = 0.1*cv2.arcLength(cnt,True)
#approx = cv2.approxPolyDP(cnt,epsilon,True)
#x,y,w,h = cv2.boundingRect(cnt)
#cv2.rectangle(cntImg,(x,y),(x+w,y+h),(0,255,0),2)
#rect = cv2.minAreaRect(cnt)
#box = cv2.boxPoints(rect)
#box = np.int0(box)
#cv2.drawContours(cntImg,[box],0,(0,0,255),2)
#cv2.imshow("contour", cntImg)
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()