-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
56 lines (40 loc) · 1.38 KB
/
Copy pathtest.py
File metadata and controls
56 lines (40 loc) · 1.38 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
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# 检测轮廓
gray_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
_, threshold_img = cv2.threshold(gray_img, 127, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(threshold_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Canny 检测
edges_canny = cv2.Canny(frame, 100, 200) # Canny 检测
# sobel计算x,y方向梯度
sobel_x = cv2.Sobel(frame, cv2.CV_64F, 1, 0, ksize=3)
sobel_y = cv2.Sobel(frame, cv2.CV_64F, 0, 1, ksize=3)
#计算梯度幅值
sobel_combined = np.sqrt(sobel_x**2 + sobel_y**2)
#Laplacian 算子
laplacian = cv2.Laplacian(frame, cv2.CV_64F)
# 绘制轮廓, 这里会改变frame
cv2.drawContours(frame, contours, -1, (0, 255, 0), 3)
# 处理每一帧
cv2.imshow("Video Mine", frame)
# 显示canny 边缘检测
cv2.imshow("Canny Edges", edges_canny)
# 显示sobel边缘检测
cv2.imshow("Sobel X", sobel_x)
cv2.imshow("Sobel Y", sobel_y)
cv2.imshow("Sobel Combined", sobel_combined)
# 显示Laplacian
cv2.imshow("Laplacian", laplacian)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# hsv = np.arange(0, 27).reshape(3, 3, 3)
# h = hsv[:, :, 0]
# print(hsv)
# print("h:{0}".format(h))