如何结合opencv和gstreamer?

时间:2017-06-28 06:04:14

标签: gstreamer

我完成了视频流jetson tx1和我的电脑

现在我有视频处理的opencvcode

所以,我想在jetson中将opencv代码改编为gstreamer

下一个代码是PC-jetson tx1流媒体和opencv代码

//jetson code//


CLIENT_IP=10.100.0.70

gst-launch-1.0 nvcamerasrc fpsRange="30 30" intent=3 ! nvvidconv flip-method=6 \

! 'video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080, format=(string)I420, framerate=(fraction)30/1' ! \

omxh264enc control-rate=2 bitrate=4000000 ! 'video/x-h264, stream-format=(string)byte-stream' ! \

h264parse ! rtph264pay mtu=1400 ! udpsink host=$CLIENT_IP port=5000 sync=false async=false


// PC code//

gst-launch-1.0 udpsrc port=5000 ! application/x-rtp,encoding-name=H264,payload=96 ! rtph264depay ! h264parse ! queue ! avdec_h264 ! xvimagesink sync=false async=false -e



opencv code


#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

int main()
{
    cv::Mat img,img_gray;

    cv::VideoCapture input(0);

    for(;;)
    {
    if (!input.read(img))
        break;

    cv::cvtColor(img, img_gray, CV_RGB2GRAY);

    cv::imshow("img", img);
    cv::imshow("gray", img_gray);
    char c = cv::waitKey(30);

    if (c == 27)
        break;
    }
} 

1 个答案:

答案 0 :(得分:0)

OpenCV支持VideoCapture和VideoWriter gstreamer管道支持gstreamer管道。您需要将管道作为参数传递,并使用appsink /开始为appsrc完成。

char* inputPipe = "nvcamerasrc fpsRange="30 30" intent=3 ! nvvidconv flip-method=6 ! 'video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080, format=(string)I420, framerate=(fraction)30/1' ! videoconvert ! appsink";
char* outputPipe = "appsrc ! videoconvert ! omxh264enc control-rate=2 bitrate=4000000 ! 'video/x-h264, stream-format=(string)byte-stream' ! 264parse ! rtph264pay mtu=1400 ! udpsink host=$CLIENT_IP port=5000 sync=false async=false";

cv::VideoCapture input(inputPipe);
cv::VideoWriter  writer(outputPipe, 0, 25.0, cv::Size(1920,1080));
for(;;)
{
if (!input.read(img))
    break;

cv::cvtColor(img, img_gray, CV_RGB2GRAY);
//cv::imshow("img", img);
//cv::imshow("gray", img_gray);
// instead of imshow pass the frame to VideoWriter
writer.write(img_gray);

char c = cv::waitKey(30);

if (c == 27)
    break;
}

效果提示:

OpenCv使用VideoCapture使用BGR色彩空间,nvcamerasrc在I420色彩空间提供输出。 如果使用以下管道,videoconvert元素将使用太多的CPU。

char* inputPipe = "nvcamerasrc fpsRange="30 30" intent=3 ! nvvidconv flip-method=6 ! 'video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080, format=(string)I420, framerate=(fraction)30/1' ! videoconvert ! appsink";

相反,如果使用nvvidconv将色彩空间转换为BGRx或RGBA,则色彩空间转换将在专用硬件上完成,CPU使用率将显着降低

 char* inputPipe = "nvcamerasrc fpsRange="30 30" intent=3 ! nvvidconv flip-method=6 ! 'video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080, format=(string)BGRx, framerate=(fraction)30/1' ! videoconvert ! appsink";