c++opencv获取编码的网络摄像头流

c++ opencv get encoded webcam stream

本文关键字:摄像头 网络 获取 编码 c++opencv      更新时间:2023-10-16

我目前正在从事一个项目,该项目从网络摄像头捕获视频,并通过UDP发送编码流以进行实时流传输。

#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
VideoCapture cap(0); // open the video camera no. 0
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
while (1)
{
    Mat frame;
    bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
return 0;
}

有人说从cap.read(frame)得到的帧已经是解码帧了,我不知道这是如何发生的,也不知道什么时候发生的。我想要的是编码的帧或流。我该怎么办才能拿到它?我应该重新编码吗?

根据文档,调用VideoCapture::read()相当于先调用VideoCapture::grab(),然后调用VideoCapture::retrieve()

Retrieve函数的文档表示,它确实对帧进行了解码。

为什么不只是使用解码的帧;大概你会在远端解码吧?

OpenCV API不提供对编码帧的访问。

您将不得不使用更低级的库,可能依赖于设备和平台。如果您的操作系统是Linux,Video4Linux2可能是一个选项,则必须有适用于Windows/MacOS的等效库。您还可以看看mjpg流媒体,它的功能与您想要实现的功能非常相似(仅在linux上)。

请注意,图像的确切编码将取决于您的网络摄像头,一些usb网络摄像头支持mjpeg压缩(甚至h264),但其他摄像头只能发送原始数据(通常在yuv颜色空间中)。

另一种选择是使用Opencv获取解码后的图像,并对其进行重新编码,例如使用imencode。它具有简单和便携的优点,但图像重新编码将占用更多的资源。