Former-commit-id: 1b34dc488f5306fcb5d20746c217255bb9ead3c5
This commit is contained in:
2025-06-06 23:02:15 +08:00
parent 8184179a17
commit c1f33df66d
7 changed files with 112 additions and 49 deletions

11
test.py
View File

@ -3,7 +3,7 @@ import numpy as np
from matplotlib import pyplot as plt
from scipy.signal import find_peaks
cap = cv2.VideoCapture(1) # 使用摄像头0通常更稳定
cap = cv2.VideoCapture(2) # 使用摄像头0通常更稳定
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640) # 降低分辨率提高处理速度
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
@ -25,7 +25,7 @@ while True:
# 直接提取饱和度通道避免完整HSV转换
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
s = hsv[:, :, 1]
s = hsv[:, :, 0]
s = s[s > 0] # 只保留非零饱和度值,减少噪声
# 使用更高效的直方图计算
hist = cv2.calcHist([s], [0], None, [256], [0, 256])
@ -33,9 +33,9 @@ while True:
# 峰值检测 - 找到直方图中的峰值
peaks, properties = find_peaks(hist,
# height=np.max(hist) * 0.1, # 峰值高度至少是最大值的10%
distance=5, # 峰值之间的最小距离
prominence=np.max(hist) * 0.05) # 峰值的突出度
height=np.max(hist) * 0.5, # 峰值高度至少是最大值的10%
distance=10, # 峰值之间的最小距离
prominence=np.max(hist) * 0.2) # 峰值的突出度
# 清除旧数据并绘制新直方图
ax.clear()
@ -43,6 +43,7 @@ while True:
# 标注峰值
if len(peaks) > 0:
print(peaks)
ax.text(0.5, 1.05, f'Found {len(peaks)} peaks')
ax.plot(peaks, hist[peaks], 'ro', markersize=8, label=f'Peaks ({len(peaks)})')
# 在峰值处添加文字标注