When opencv captures gstreamer video data, black screen!

As shown in the following code, when I use the following code to access the video (whether it is rtsp or mp4), the imshow display is always a black screen, and I try to output the frame, but I can only get a bunch of 0. But I can use the following pipeline normally inside the command line. In addition, opencv’s support for gstreamer is also yes。


int main(int argc, char *argv[]) {
    using namespace cv;
    using namespace std;

    gst_init(&argc, &argv);
    // const string gst_in = "rtspsrc location=rtsp://admin:fgfg5678@192.168.10.66:554/h264/ch1/main/av_stream latency=0 ! decodebin ! videoconvert ! appsink";
    const string gst_in="filesrc location=test.mp4 ! decodebin ! videoconvert ! appsink sync=false";
    VideoCapture cap(gst_in, cv::CAP_GSTREAMER);

    if(!cap.isOpened())
    {
	cout << "error to open camera." << endl;
        return -1;
    }

    namedWindow("Received Frame",WINDOW_NORMAL);
    Mat frame ;
    // 逐帧显示
    while(cap.read(frame))
    {
	
     	imshow("Received Frame", frame);
	    if (waitKey(10) == 27) // Esc key to exit
            break;
    }
    cap.release();
    return 0;
}
filesrc location=test.mp4 ! decodebin ! videoconvert ! autovideosink

I don’t know where the problem is, the version I use is as follows:
opencv:4.8.0
gstreamer:1.16.2

Hi,
There are some samples for reference:
Displaying to the screen with OpenCV and GStreamer - #9 by DaneLLL
Doesn't work nvv4l2decoder for decoding RTSP in gstreamer + opencv - #3 by DaneLLL

Please check and give it a try.

I tried adding some format conversions to the pipeline to convert the image format to BGR, but nothing worked:

filesrc location=./test.mp4 ! decodebin ! videoconvert ! video/x-raw,format=BGR ! appsink drop=true caps=video/x-raw,format=BGR

Hi,
We suggest refer to gstreamer user guide and try to work out a working gst-launch-1.0 command first. And then apply the pipeline to OpenCV.

Try:

filesrc location=./test.mp4 ! decodebin ! nvvidconv ! video/x-raw,format=BGRx ! videoconvert ! video/x-raw,format=BGR ! appsink drop=true