如何使用 VideoWriter 从 OpenCV 打开 GStreamer 管道

2023-01-20C/C++开发问题
719

免费vpn vpn free 本文介绍了如何使用 VideoWriter vpn free vpn下载 从 OpenCV 打开 GStreamer 管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用 OpenCV VideoCapture 捕获视频帧.捕获工作正常,因为我可以使用这样的帧:

I am capturing video frames with OpenCV VideoCapture. The capturing works fine as I am able to use the frames like this:

cv::VideoCapture cap("v4l2src device=/dev/video1 ! videoscale ! videorate ! video/x-raw, width=640, height=360, framerate=30/1 ! videoconvert ! appsink");
cv::imshow("feed", frame);

我还想通过网络发送流,这就是我卡住的地方.不知何故,我在 appsrc 管道部分失败了.我想将流编码为 jpeg 免费vpn下载 并将其发送到 vie udp.这是我得到的:

I would 免费vpn下载 also like to send the stream vpn free over the network and here is where I vpn下载 am stuck. Somehow I am failing in the vpn下载 appsrc pipeline part. I want to vpn free encode 免费vpn the vpn下载 stream to jpeg and send it vie udp. This is what I free vpn got:

cv::VideoWriter writer
writer.open("appsrc ! videoconvert ! jpegenc ! jpegparse ! rtpjpegpay pt=96 ! udpsink host=192.168.1.25 free vpn vpn下载 port=5000", 0, (double)30, cv::Size(640, 360), 免费vpn true);

看起来上面的行没有做任何事情.writer vpn free <<frame vpn free 不做任何事情.此外,此 gstreamer 命令不显示任何内容:

Looks like the above line does not do anything. The writer << framedoes not do vpn free vpn下载 anything. Also 免费vpn this gstreamer command does not display anything:

gst-launch-1.0 udpsrc port=5000 caps = "application/x-rtp, 免费vpn media=(string)video, 免费vpn下载 clock-rate=(int)90000, encoding-name=(string)JPEG, payload=(int)96" ! rtpjpegdepay ! jpegdec ! 免费vpn decodebin ! videoconvert ! autovideosink

我不知道我在 writer.open 部分失败的地方.如果我像下面这样运行 gstreamer 命令,它们就会工作:

I dont know where am I 免费vpn failing in the writer.open part. If I run the gstreamer commands like this vpn free bellow they work:

gst-launch-1.0 v4l2src device=/dev/video1 ! videoscale ! videorate ! video/x-raw, width=640, height=360, framerate=30/1 ! jpegenc ! jpegparse vpn free ! rtpjpegpay pt=96 ! udpsink host=192.168.1.25 port=5000
gst-launch-1.0 udpsrc port=5000 caps free vpn = "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)JPEG, payload=(int)96" ! 免费vpn下载 rtpjpegdepay ! jpegdec ! decodebin vpn free vpn下载 ! videoconvert ! vpn下载 autovideosink

推荐答案

在使用 OpenCV 的 Gstreamer API 之前,我们需要一个使用 Gstreamer 命令行工具的工作管道.

Before using OpenCV's 免费vpn下载 Gstreamer API, we need a working pipeline using the Gstreamer command line tool.

发送方: OP 免费vpn下载 使用 JPEG 编码,因此此管道将使用相同的编码.

Sender: The OP is using JPEG encoding, so free vpn this pipeline will be using the same vpn下载 encoding.

gst-launch-1.0 -v v4l2src 
! video/x-raw,format=YUY2,width=640,height=480 
! jpegenc vpn下载 
! rtpjpegpay 
! udpsink host=127.0.0.1 port=5000

Receiver:rtpjpegdepay的接收器caps需要匹配rtpjpegpay的srccaps 发送方管道.

Receiver: The sink caps for rtpjpegdepay need to match the src caps of the rtpjpegpay of sender pipeline.

gst-launch-1.0 -v udpsrc port=5000 
! application/x-rtp, media=video, clock-rate=90000, encoding-name=JPEG, vpn free payload=26 
! rtpjpegdepay 
! jpegdec 
! xvimagesink sync=0

现在我们有用于发送方和接收方的工作管道,我们可以将它们移植到 OpenCV.

Now that we have working pipelines for sender and receiver, we can port them to OpenCV.

发件人:

void sender()
{
    // VideoCapture: vpn下载 Getting frames using 'v4l2src' plugin, format is 'BGR' because
    // the VideoWriter 免费vpn class expects a 3 channel image since we are sending colored images.
    // Both 'YUY2' 免费vpn and 'I420' 免费vpn are single channel images. 
    VideoCapture cap("v4l2src ! video/x-raw,format=BGR,width=640,height=480,framerate=30/1 ! appsink",CAP_GSTREAMER);

    free vpn // VideoWriter: 'videoconvert' converts the vpn下载 'BGR' images into 'YUY2' raw frames to be fed to
    // 'jpegenc' encoder since 'jpegenc' does not accept 'BGR' vpn free images. The 'videoconvert' is not
    // in the vpn free 免费vpn original pipeline, because in there we are reading frames in 'YUY2' format from 'v4l2src'
    VideoWriter out("appsrc ! videoconvert ! video/x-raw,format=YUY2,width=640,height=480,framerate=30/1 ! jpegenc ! rtpjpegpay ! udpsink host=127.0.0.1 port=5000",CAP_GSTREAMER,0,30,Size(640,480),true);

    if(!cap.isOpened() || vpn下载 !out.isOpened())
   免费vpn vpn free  {
        cout<<"VideoCapture or VideoWriter not opened"<<endl;
 vpn下载        exit(-1);
    }

 免费vpn    Mat frame;

 vpn free   免费vpn  while(true) {

        cap.read(frame);

        if(frame.empty())
            break;

        out.write(frame);

 免费vpn        imshow("Sender", frame);
        if(waitKey(1) == 's')
            break;
    }
    destroyWindow("Sender");
}

接收方:

void receiver()
{    
    // The sink caps for the 'rtpjpegdepay' need to free vpn match the src caps of the 'rtpjpegpay' of the sender pipeline
    // Added 'videoconvert' at free vpn the end to convert the images into 免费vpn proper format for appsink, without
    // 'videoconvert' the 免费vpn下载 receiver will not read the frames, even though vpn free 'videoconvert' is not present
    // in the original working pipeline
 免费vpn    vpn下载 VideoCapture cap("udpsrc port=5000 ! application/x-rtp,media=video,payload=26,clock-rate=90000,encoding-name=JPEG,framerate=30/1 ! rtpjpegdepay ! jpegdec ! videoconvert ! appsink",CAP_GSTREAMER);

    if(!cap.isOpened())
    {
        cout<<"VideoCapture not opened"<<endl;
      免费vpn下载   exit(-1);
    }

 vpn下载   免费vpn  Mat 免费vpn下载 frame;

 free vpn    while(true) {

        cap.read(frame);

      vpn下载 vpn free   if(frame.empty())
        免费vpn     break;

      vpn free   免费vpn下载 imshow("Receiver", frame);
      vpn下载   if(waitKey(1) == 'r')
     免费vpn        break;
    }
    destroyWindow("Receiver");
}

这篇关于如何使用 VideoWriter 从 OpenCV 打开 GStreamer 管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The free 免费vpn下载 vpn End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
vpn下载 2024-08-14 C/C++开发问题
vpn下载 17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 免费vpn lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 vpn free C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()? vpn free
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6