-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideo.py
More file actions
89 lines (73 loc) · 3.2 KB
/
Video.py
File metadata and controls
89 lines (73 loc) · 3.2 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
颜色分割阈值、视频调试
"""
import numpy as np
import cv2
# 颜色分割的阈值
class ColorSegHSV:
"HSV模式下常用颜色分割参数"
# 七色阈值分割
RED_RANGE_1 = (np.array([0, 43, 46]), np.array([10, 255, 255]))
ORANGE_RANGE = (np.array([11, 43, 46]), np.array([25, 255, 255]))
YELLOW_RANGE = (np.array([26, 43, 46]), np.array([34, 255, 255]))
GREEN_RANGE = (np.array([35, 43, 46]), np.array([77, 255, 255]))
CYAN_RANGE = (np.array([78, 43, 46]), np.array([99, 255, 255]))
BLUE_RANGE = (np.array([100, 43, 46]), np.array([124, 255, 255]))
PURPLE_RANGE = (np.array([125, 43, 46]), np.array([155, 255, 255]))
RED_RANGE_2 = (np.array([156, 43, 46]), np.array([180, 255, 255]))
# RGB三通道分割(推荐直接使用RGB模式)
R_RANGE_1 = (np.array([151, 43, 46]), np.array([180, 255, 255]))
R_RANGE_2 = (np.array([0, 43, 46]), np.array([30, 255, 255]))
G_RANGE = (np.array([31, 43, 46]), np.array([90, 255, 255]))
B_RANGE = (np.array([91, 43, 46]), np.array([150, 255, 255]))
class ColorSegHLS:
"HLS空间分割"
BLACK_RANGE = (np.array([0, 0, 0]), np.array([179, 90, 255])) # 黑色提取
def printVideoPara(capture: cv2.VideoCapture):
"返回摄像头各类参数"
print("帧宽:", capture.get(cv2.CAP_PROP_FRAME_WIDTH))
print("帧高:", capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
print("解码方式:", capture.get(cv2.CAP_PROP_FOURCC))
print("帧率:", capture.get(cv2.CAP_PROP_FPS))
print("亮度:", capture.get(cv2.CAP_PROP_BRIGHTNESS))
print("对比度:", capture.get(cv2.CAP_PROP_CONTRAST))
print("饱和度:", capture.get(cv2.CAP_PROP_SATURATION))
print("色调:", capture.get(cv2.CAP_PROP_HUE))
print("图像增益:", capture.get(cv2.CAP_PROP_GAIN))
print("曝光度:", capture.get(cv2.CAP_PROP_EXPOSURE))
def CaptureInit(
index: int = 0, /, width: int = 640, height: int = 480, PI_MODE=True
) -> cv2.VideoCapture:
"摄像头配置和打开"
if PI_MODE: # 是否使用树莓派模式(不同设备使用不同设置)
capture = cv2.VideoCapture(index)
else:
capture = cv2.VideoCapture(index, cv2.CAP_DSHOW)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, width)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
fourcc = cv2.VideoWriter_fourcc("M", "J", "P", "G") # 设置视频流格式,从而可在树莓派上调试
capture.set(cv2.CAP_PROP_FOURCC, fourcc)
capture.set(cv2.CAP_PROP_BUFFERSIZE, 1) # 缓存区设置为1,防止滞后过度使得控制效果较差
return capture
class TrackWindow:
"""
带有滑块的视频窗口
"""
def __init__(
self,
windowName: str,
paraname: str = "value", # 绑定参数
para: int = 0, # 参数初始值
maxP: int = 255,
) -> None:
self.name = paraname
self.window = windowName
cv2.namedWindow(windowName, cv2.WINDOW_AUTOSIZE)
cv2.createTrackbar(paraname, windowName, para, maxP, self.__nothing)
# self.show()
def __nothing(self, x):
pass
def getValue(self) -> int:
return cv2.getTrackbarPos(self.name, self.window)
def show(self, image: np.ndarray):
cv2.imshow(self.window, image)