OpenCV Python 版使用教程(三)摄像头读取延迟解决方法

文章目录


一、上篇回顾

在上一篇中,简单介绍了如何使用 OpenCV 操作摄像头,本期来讲在摄像头读取时,可能会出现画面延迟的解决方法。

二、产生原因

OpenCV 在读取的时候,会将视频流放在缓冲区中,然后每次调用的时候,会从缓冲区内读取视频帧。虽然说大多数情况难以出现写入缓冲区远大于读取缓冲区的速度,但是当设备出现性能瓶颈,尤其是在 树莓派 等嵌入式设备中,容易出现CPU 瓶颈,从而引发问题。

三、解决方法

1. cap.grab() 方法

cpp 复制代码
virtual bool cv::VideoCapture::grab()

官网对于这个函数的描述如下:

复制代码
The primary use of the function is in multi-camera environments, especially when the cameras do not have hardware synchronization. 
That is, you call VideoCapture::grab() for each camera and after that call the slower method VideoCapture::retrieve()
to decode and get frame from each camera. This way the overhead on demosaicing or motion jpeg
decompression etc. is eliminated and the retrieved frames from different cameras will be closer in time. 

从官网的描述来看,这个函数主要用在多个摄像机中,用于摄像头硬件同步,这个函数和 cap.retrieve() 配合使用。当然也可以使用 cap.read() 再次读取。

同时也可以进一步设置缓冲区的大小,代码如下:

python 复制代码
cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)

在这里设置 Capture 的缓冲区大小为 1 帧。

2. 多线程法

python3 复制代码
import cv2
import threading

class VideoCaptureThread:
    def __init__(self, index=0):
        self.cap = cv2.VideoCapture(index)
        self.frame = None
        self.running = True
        self.lock = threading.Lock()
        self.thread = threading.Thread(target=self.update)
        self.thread.start()

    def update(self):
        while self.running:
            ret, frame = self.cap.read()
            if ret:
                with self.lock:  # 确保线程安全
                    self.frame = frame

    def read(self):
        with self.lock:  # 确保线程安全
            return self.frame

    def stop(self):
        self.running = False
        self.thread.join()
        self.cap.release()


# 使用示例
video_capture = VideoCaptureThread()

while True:
    frame = video_capture.read()
    if frame is not None:
        cv2.imshow('Frame', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

video_capture.stop()
cv2.destroyAllWindows()

在本例中,读取视频帧放在了一个单独的线程,读取和处理分别单独运行,从而避免了两者速度不一致导致的读取延迟。


总结

本篇介绍了两种解决摄像头延迟的方法。在读取到图像后,接下来要做的就是图像处理了,下期将介绍图像的各种色彩空间。

相关推荐
2601_958352909 分钟前
I2S转模拟音频,真的只要5个引脚+1个电容?
前端·人工智能·单片机·嵌入式硬件
AI大模型-小雄16 分钟前
Codex 如何自动修复项目 Bug?从错误定位到测试验证的实战教程
python·程序员·bug·ai编程·codex·chatgpt plus·chatgpt pro
极客编程玩家23 分钟前
SpringAI RAG 检索增强完整实战案例
人工智能·ai·aigc·ai编程
LaughingZhu31 分钟前
Product Hunt 热榜 2026-12-05:SoduWork、finerd split、Otari by Mozilla AI
java·人工智能·docker·chatgpt·容器
风月说与山鬼44 分钟前
五、python基础——集合、映射类型
python
KingOTC1 小时前
FDE的组织方式:团队怎么搭、仗怎么打
人工智能·fde
欣欣之王来了1 小时前
信息论基础:熵、交叉熵与损失函数的关系
人工智能·opencv·目标检测·计算机视觉·cv·核心技术
BK贝壳1 小时前
初始化一亿个全 True 或全 False:bool-hybrid-array 的 TruesArray/FalsesArray 占位构造本机实测
开发语言·数据结构·python·性能优化·numpy
陈嘿萌1 小时前
TPAMI 2026 | 多模态自监督只做对齐够吗?MIIB同时学习冗余、独有与协同信息机制
图像处理·人工智能·深度学习·计算机视觉·学术研究与理论基础
Golden小狗种自己的花[哇]2 小时前
LLM/Token/Agent
人工智能·大语言模型·agent