简体   繁体   English

C ++ OpenCV获取编码的网络摄像头流

[英]c++ opencv get encoded webcam stream

I am currently work on a project that capture video from webcam and send the encoded stream via UDP to do a real time streaming. 我目前正在从事一个项目,该项目从网络摄像头捕获视频并通过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;
}

Some people say that the frame get from cap.read(frame) is already the decoded frame,I have no idea how and when does that happen. 有人说从cap.read(frame)获得的已经是解码的帧,我不知道这是如何发生的以及何时发生。 And what I want is the encoded frame or stream. 我想要的是编码的帧或流。 What should I do to get it? 我应该怎么做才能得到它? Should I encoded it back again? 我应该再编码一次吗?

According to the docs , calling VideoCapture::read() is equivalent to calling VideoCapture::grab() then VideoCapture::retrieve() . 根据文档 ,调用VideoCapture::read()等效于先调用VideoCapture::grab()然后再调用VideoCapture::retrieve()

The docs for the Retrieve function say it does indeed decode the frame. Retrieve函数的文档说它确实对帧进行了解码。

Why not just use the decoded frame; 为什么不只使用解码帧呢? presumably you'd be decoding it at the far end in any case? 大概在任何情况下您都将在远端对其进行解码?

OpenCV API does not give access to the encoded frames. OpenCV API不允许访问编码的帧。

You will have to use a more low-level library, probably device and platform dependent. 您将不得不使用更底层的库,可能取决于设备和平台。 If your OS is Linux, Video4Linux2 may be an option, there must be equivalent libraries for Windows/MacOS. 如果您的操作系统是Linux,则可以选择Video4Linux2 ,Windows / MacOS必须具有等效的库。 You may also have a look at mjpg-streamer , which does something very similar to what you want to achieve (on linux only). 您还可以看一下mjpg-streamer ,它的功能与您要实现的功能非常相似(仅在linux上)。

Note that the exact encoding of the image will depend on your webcam, some usb webcam support mjpeg compression (or even h264), but other are only able to send raw data (usually in yuv colorspace). 请注意,图像的确切编码取决于您的网络摄像头,某些USB网络摄像头支持mjpeg压缩(甚至是h264),但其他只能发送原始数据(通常在yuv色彩空间中)。

Another option is to grab the decoded image wit Opencv, and reencode it, for example with imencode . 另一个选择是使用Opencv获取解码的图像,然后使用imencode重新编码它。 It has the advantages of simplicity and portability, but image reencoding will use more resource. 它具有简单和可移植的优点,但是图像重新编码将使用更多资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM