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

OpenCV VideoCapture随机帧通道偏移损坏问题求助

相机帧损坏问题排查

相机硬件本身工作正常,使用验证软件未检测到损坏帧,因此推测问题出在OpenCV上。目前观察到每10-20帧中会随机出现1帧损坏,表现为图像某一通道偏移,示例如下:

损坏帧示例

我将相机封装为后台服务,让其他应用无需处理读帧循环和异步逻辑即可获取最新帧,相关代码如下:

import threading
import time
import cv2 as cv
import numpy as np

class CameraCU81():
    def __init__(self, W=1920, H=1080, hz=30):
        self.cap = cv.VideoCapture(0)
        self.last_frame = None
        # if recording mjgp the frames hangs...
        #self.cap.set(cv.CAP_PROP_FOURCC, cv.VideoWriter_fourcc('M', 'J', 'P', 'G'))
        print(str(cv.VideoWriter_fourcc('M', 'J', 'P', 'G')))
        self.cap.set(cv.CAP_PROP_FRAME_WIDTH, W)
        self.cap.set(cv.CAP_PROP_FRAME_HEIGHT, H)
        self.cap.set(cv.CAP_PROP_FPS, hz)
        print('Starting camera 81 fps at: ' +  str(self.cap.get(cv.CAP_PROP_FPS)))
        w = str(self.cap.get(cv.CAP_PROP_FRAME_WIDTH))
        h = str(self.cap.get(cv.CAP_PROP_FRAME_HEIGHT))
        print('Starting camera 81 resolution at: ' + w + ' x ' + h)
        format = str(self.cap.get(cv.CAP_PROP_FOURCC))
        print('Starting camera 81 format: ' + format)

    def __capture_frames(self):
        error_f = False
        while True:
            start_time = time.time()
            ret, frame = self.cap.read()
            if not ret:
                timeout_time = (time.time() - start_time)
                print('Frame could not be read ... is camera connected?')
                print(timeout_time)
                error_f = True
            else:
                self.last_frame = frame
                if error_f:
                    timeout_time = (time.time() - start_time)
                    print(timeout_time)

    def get_data(self):
        return self.last_frame

    def destroy(self):
        self.cap.release()

    def run(self):
        t1 = threading.Thread(target=self.__capture_frames)
        t1.daemon = True
        t1.start()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 15:42:24