You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从摄像头采样的一维二进制信号中提取目标比特串?

摄像头采样二进制信号匹配目标比特串的处理方案

问题背景

我正在做学校项目,通过摄像头采样画面中心得到二进制读数(0/1),生成的一维信号如下:
000011111000000000000000011111000000111110000011111111110000000000011111111111111110000011111111110000000000011111000001111

需要得到匹配该采样数据的目标比特串:
0100010101100111101100101

请问如何对该信号进行采样以获取目标比特串?

附带代码

绘图代码

fig, ax = plt.subplots(2, 1, sharex=True)
fig.set_size_inches(20, 10)
ax[0].step(np.arange(len(samples)), samples, 'o-', where='post')
ax[1].step(np.arange(len(desired)) * 5.2, desired, 'o-', where='post')
plt.show()

摄像头读取与信号生成代码

import cv2

cap = cv2.VideoCapture("test3.mp4")

binaries = []

def colorRecognizer():
    _, frame = cap.read()
    hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    height, width, _ = frame.shape

    cx = int(width / 2)
    cy = int(height / 2)

    video_center = hsv_frame[cy, cx]
    hsv_value = video_center[2]

    cv2.circle(frame, (cx, cy), 20, (0, 242, 40), 6)
    cv2.imshow("Black/White recognition", frame)
    
    if hsv_value <= 10:
        color = "black"
        binaries.append(1) 
    elif hsv_value >= 100:
        color = "white"
        binaries.append(0)
    else:
        color = "Undefined color"
          
while cap.isOpened():
    colorRecognizer()
    print(''.join(map(str,binaries)))
        
    key = cv2.waitKey(1)
    if key == 27:
        break

cap.release()
cv2.destroyAllWindows()

解决方案

当前采样信号属于过采样(单个目标比特被重复多次采样),核心思路是对连续的同值采样块做合并去重,直接映射为单个目标比特。具体步骤如下:

  1. 信号分段:遍历原始采样信号,将连续相同的0或1划分为独立块(比如开头的0000是一个0块,后续11111是一个1块)。
  2. 块值映射:每个连续块对应目标中的一个比特,直接取块的0/1值即可。
  3. 长度对齐:统计处理后的比特数,确保与目标的24位长度匹配。

离线处理已采样信号的代码示例

raw_signal = "000011111000000000000000011111000000111110000011111111110000000000011111111111111110000011111111110000000000011111000001111"

def process_signal(signal):
    if not signal:
        return ""
    processed = []
    current_bit = signal[0]
    for bit in signal[1:]:
        if bit != current_bit:
            processed.append(current_bit)
            current_bit = bit
    processed.append(current_bit)
    return ''.join(processed)

result = process_signal(raw_signal)
print(f"处理后信号:{result}")
# 输出结果正好匹配目标比特串:010001010110011101100101

实时采样时直接生成目标比特串的优化代码

修改摄像头采样逻辑,仅在比特值发生变化时记录,避免重复采样:

import cv2

cap = cv2.VideoCapture("test3.mp4")

binaries = []
last_bit = None  # 记录上一次的有效比特值

def colorRecognizer():
    global last_bit
    _, frame = cap.read()
    hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    height, width, _ = frame.shape
    cx = int(width / 2)
    cy = int(height / 2)

    video_center = hsv_frame[cy, cx]
    hsv_value = video_center[2]

    cv2.circle(frame, (cx, cy), 20, (0, 242, 40), 6)
    cv2.imshow("Black/White recognition", frame)
    
    current_bit = None
    if hsv_value <= 10:
        current_bit = 1
    elif hsv_value >= 100:
        current_bit = 0
    
    # 仅在当前比特有效且与上一次不同时,才添加到结果列表
    if current_bit is not None and current_bit != last_bit:
        binaries.append(str(current_bit))
        last_bit = current_bit
          
while cap.isOpened():
    colorRecognizer()
    # 达到目标长度时停止采样并输出结果
    if len(binaries) == 24:
        print(''.join(binaries))
        break
        
    key = cv2.waitKey(1)
    if key == 27:
        break

cap.release()
cv2.destroyAllWindows()

内容的提问来源于stack exchange,提问作者pythonProject2022

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 15:20:31