1+ #!/usr/bin/python
2+ # -*- coding:utf-8 -*-
3+ # @File : hsv_color_test.py
4+ # @Author : Wang Weijian
5+ # @Time : 2024/12/16 10:14:57
6+ # @function: the script is used to do something
7+ # @version : V1
8+ import cv2
9+ import platform
10+
11+ import numpy as np
12+
13+ if platform .system () == "Windows" :
14+ cap = cv2 .VideoCapture (1 , cv2 .CAP_DSHOW )
15+ elif platform .system () == "Linux" :
16+ cap = cv2 .VideoCapture (0 , cv2 .CAP_V4L )
17+
18+ for i in range (0 , 19 ):
19+ print (cap .get (i ))
20+
21+ HSV = {
22+ "yellow" : [np .array ([11 , 85 , 70 ]), np .array ([59 , 255 , 245 ])],
23+ "red" : [np .array ([0 , 43 , 46 ]), np .array ([8 , 255 , 255 ])],
24+ "green" : [np .array ([35 , 43 , 35 ]), np .array ([90 , 255 , 255 ])],
25+ "blue" : [np .array ([78 , 43 , 46 ]), np .array ([110 , 255 , 255 ])],
26+ "cyan" : [np .array ([78 , 43 , 46 ]), np .array ([99 , 255 , 255 ])],
27+ }
28+
29+ while 1 :
30+
31+ ret , frame = cap .read ()
32+
33+ hsv = cv2 .cvtColor (frame , cv2 .COLOR_BGR2HSV )
34+
35+ lower_red = np .array ([0 , 43 , 46 ])
36+
37+ upper_red = np .array ([8 , 255 , 255 ])
38+
39+ mask = cv2 .inRange (hsv , lower_red , upper_red ) # 红色掩模
40+
41+ res = cv2 .bitwise_and (frame , frame , mask = mask )
42+
43+ cv2 .imshow (u"Capture" , frame )
44+
45+ cv2 .imshow (u"mask" , mask )
46+
47+ cv2 .imshow (u"res" , res )
48+
49+ key = cv2 .waitKey (1 )
50+
51+ if key & 0xff == ord ('q' ) or key == 27 :
52+ print (frame .shape , ret )
53+
54+ break
55+
56+ cap .release ()
57+
58+ cv2 .destroyAllWindows ()
0 commit comments