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

时间:2015-03-23 08:44:08

标签: c++ opencv video-streaming webcam

我目前正致力于一个从网络摄像头捕获视频的项目,并通过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)的帧已经是解码后的帧,我不知道这是怎么回事。我想要的是编码的帧或流。我该怎么做才能得到它?我应该重新编码吗?

2 个答案:

答案 0 :(得分:0)

根据the docs,调用VideoCapture::read()相当于调用VideoCapture::grab()然后调用VideoCapture::retrieve()

The docs for the Retrieve function说它确实对帧进行了解码。

为什么不使用解码后的帧;大概在任何情况下你都会在远端解码它?

答案 1 :(得分:0)

OpenCV API不允许访问编码的帧。

您将不得不使用更低级别的库,可能取决于设备和平台。如果您的操作系统是Linux,Video4Linux2可能是一个选项,则必须有适用于Windows / MacOS的库。您也可以查看mjpg-streamer,它与您想要实现的目标非常相似(仅限Linux)。

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

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

相关问题