2
2
import numpy as np
3
3
4
4
############################################
5
- cap = cv2 .VideoCapture (0 )
5
+ cap = cv2 .VideoCapture (1 )
6
6
path = 'test.png'
7
7
############################################
8
8
9
9
def empty (a ):
10
10
pass
11
11
12
- def stackImages (scale ,imgArray ):
12
+
13
+ def stackImages (scale , imgArray ):
13
14
rows = len (imgArray )
14
15
cols = len (imgArray [0 ])
15
16
rowsAvailable = isinstance (imgArray [0 ], list )
16
17
width = imgArray [0 ][0 ].shape [1 ]
17
18
height = imgArray [0 ][0 ].shape [0 ]
18
19
if rowsAvailable :
19
- for x in range ( 0 , rows ):
20
+ for x in range ( 0 , rows ):
20
21
for y in range (0 , cols ):
21
- if imgArray [x ][y ].shape [:2 ] == imgArray [0 ][0 ].shape [:2 ]:
22
+ if imgArray [x ][y ].shape [:2 ] == imgArray [0 ][0 ].shape [:2 ]:
22
23
imgArray [x ][y ] = cv2 .resize (imgArray [x ][y ], (0 , 0 ), None , scale , scale )
23
24
else :
24
- imgArray [x ][y ] = cv2 .resize (imgArray [x ][y ], (imgArray [0 ][0 ].shape [1 ], imgArray [0 ][0 ].shape [0 ]), None , scale , scale )
25
- if len (imgArray [x ][y ].shape ) == 2 : imgArray [x ][y ]= cv2 .cvtColor ( imgArray [x ][y ], cv2 .COLOR_GRAY2BGR )
25
+ imgArray [x ][y ] = cv2 .resize (imgArray [x ][y ], (imgArray [0 ][0 ].shape [1 ], imgArray [0 ][0 ].shape [0 ]),
26
+ None , scale , scale )
27
+ if len (imgArray [x ][y ].shape ) == 2 : imgArray [x ][y ] = cv2 .cvtColor (imgArray [x ][y ], cv2 .COLOR_GRAY2BGR )
26
28
imageBlank = np .zeros ((height , width , 3 ), np .uint8 )
27
- hor = [imageBlank ]* rows
28
- hor_con = [imageBlank ]* rows
29
+ hor = [imageBlank ] * rows
30
+ hor_con = [imageBlank ] * rows
29
31
for x in range (0 , rows ):
30
32
hor [x ] = np .hstack (imgArray [x ])
31
33
ver = np .vstack (hor )
@@ -34,45 +36,44 @@ def stackImages(scale,imgArray):
34
36
if imgArray [x ].shape [:2 ] == imgArray [0 ].shape [:2 ]:
35
37
imgArray [x ] = cv2 .resize (imgArray [x ], (0 , 0 ), None , scale , scale )
36
38
else :
37
- imgArray [x ] = cv2 .resize (imgArray [x ], (imgArray [0 ].shape [1 ], imgArray [0 ].shape [0 ]), None ,scale , scale )
39
+ imgArray [x ] = cv2 .resize (imgArray [x ], (imgArray [0 ].shape [1 ], imgArray [0 ].shape [0 ]), None , scale , scale )
38
40
if len (imgArray [x ].shape ) == 2 : imgArray [x ] = cv2 .cvtColor (imgArray [x ], cv2 .COLOR_GRAY2BGR )
39
- hor = np .hstack (imgArray )
41
+ hor = np .hstack (imgArray )
40
42
ver = hor
41
43
return ver
42
44
43
45
44
46
cv2 .namedWindow ("TrackBars" )
45
- cv2 .resizeWindow ("TrackBars" ,640 ,240 )
46
- cv2 .createTrackbar ("Hue Min" ,"TrackBars" ,0 , 179 ,empty )
47
- cv2 .createTrackbar ("Hue Max" ,"TrackBars" ,179 ,179 ,empty )
48
- cv2 .createTrackbar ("Sat Min" ,"TrackBars" ,0 , 255 ,empty )
49
- cv2 .createTrackbar ("Sat Max" ,"TrackBars" ,255 ,255 ,empty )
50
- cv2 .createTrackbar ("Val Min" ,"TrackBars" ,0 , 255 ,empty )
51
- cv2 .createTrackbar ("Val Max" ,"TrackBars" ,255 ,255 ,empty )
47
+ cv2 .resizeWindow ("TrackBars" , 640 , 240 )
48
+ cv2 .createTrackbar ("Hue Min" , "TrackBars" , 0 , 179 , empty )
49
+ cv2 .createTrackbar ("Hue Max" , "TrackBars" , 179 , 179 , empty )
50
+ cv2 .createTrackbar ("Sat Min" , "TrackBars" , 0 , 255 , empty )
51
+ cv2 .createTrackbar ("Sat Max" , "TrackBars" , 255 , 255 , empty )
52
+ cv2 .createTrackbar ("Val Min" , "TrackBars" , 0 , 255 , empty )
53
+ cv2 .createTrackbar ("Val Max" , "TrackBars" , 255 , 255 , empty )
52
54
53
55
while True :
54
- #img = cv2.imread(path) #for image
55
- _ , img = cap .read () # for Video
56
- imgHSV = cv2 .cvtColor (img ,cv2 .COLOR_BGR2HSV )
57
- h_min = cv2 .getTrackbarPos ("Hue Min" ,"TrackBars" )
56
+ # img = cv2.imread(path) #for image
57
+ _ , img = cap .read () # for Video
58
+ imgHSV = cv2 .cvtColor (img , cv2 .COLOR_BGR2HSV )
59
+ h_min = cv2 .getTrackbarPos ("Hue Min" , "TrackBars" )
58
60
h_max = cv2 .getTrackbarPos ("Hue Max" , "TrackBars" )
59
61
s_min = cv2 .getTrackbarPos ("Sat Min" , "TrackBars" )
60
62
s_max = cv2 .getTrackbarPos ("Sat Max" , "TrackBars" )
61
63
v_min = cv2 .getTrackbarPos ("Val Min" , "TrackBars" )
62
64
v_max = cv2 .getTrackbarPos ("Val Max" , "TrackBars" )
63
65
print (f'{ h_min } ,{ h_max } ,{ s_min } ,{ s_max } ,{ v_min } ,{ v_max } ' )
64
- lower = np .array ([h_min ,s_min ,v_min ])
65
- upper = np .array ([h_max ,s_max ,v_max ])
66
- mask = cv2 .inRange (imgHSV ,lower ,upper )
67
- imgResult = cv2 .bitwise_and (img ,img ,mask = mask )
68
-
66
+ lower = np .array ([h_min , s_min , v_min ])
67
+ upper = np .array ([h_max , s_max , v_max ])
68
+ mask = cv2 .inRange (imgHSV , lower , upper )
69
+ imgResult = cv2 .bitwise_and (img , img , mask = mask )
69
70
70
71
# cv2.imshow("Original",img)
71
72
# cv2.imshow("HSV",imgHSV)
72
73
# cv2.imshow("Mask", mask)
73
74
# cv2.imshow("Result", imgResult)
74
75
75
- imgStack = stackImages (0.6 ,([img ,imgHSV ],[mask ,imgResult ]))
76
+ imgStack = stackImages (0.6 , ([img , imgHSV ], [mask , imgResult ]))
76
77
cv2 .imshow ("Stacked Images" , imgStack )
77
78
78
79
cv2 .waitKey (1 )
0 commit comments