OpenCV reads an empty image (gstreamer pipeline)

Hello everyone. I need to record a video from the camera. I get an error on the check, that is, an empty image is read from the camera. There is a check on the file size:

if (img.empty())
            {
                std::cout << "empty image" << std::endl;
                return 0;
            }

My code:

#include <opencv2/opencv.hpp>
#include <iostream>
#include <iostream>
using namespace std;
cv::Mat frame;
int main(int ac, char** av) {
 
    
    std::cout << cv::getBuildInformation();
    cv::VideoCapture cap(0);
    
 
 
 
 
    if (!cap.isOpened())
    {
        std::cout << "Can't open the camera" << std::endl;
        return -1;
    }
 
 
    cv::Mat img;
 
    cout << "Started video..."<<endl;
    while (cap.isOpened())
    {
        try
        {
 
 
            cap >> img;
            
            if (img.empty())
            {
                std::cout << "empty image" << std::endl;
                return 0;
            }
 
            cv::imshow("camera img", img);
 
            if (cv::waitKey(25) == 27)
                break;
        }
        catch( cv::Exception& e )
        {
            const char* err_msg = e.what();
            std::cout << "exception caught: " << err_msg << std::endl;
        }
    }
 
    return 0;
}

Hi

What’s problem?
Is next image empty too?why not send same image twice

I tried to do this, all images are empty and an error is displayed.
It initially reads an empty image

What’s result of getBuildInformation()?

1 Like

My logs

General configuration for OpenCV 4.5.2 =====================================
  Version control:               lf-5.10.72-2.2.0-dirty

  Extra modules:
    Location (extra):            /workdir/contrib/modules
    Version control (extra):     4.5.2-dirty

  Platform:
    Timestamp:                   2021-04-12T02:26:40Z
    Host:                        Linux 5.13.0-51-generic x86_64
    Target:                      Linux aarch64
    CMake:                       3.19.5
    CMake generator:             Ninja
    CMake build tool:            ninja
    Configuration:               Release

  CPU/HW features:
    Baseline:                    NEON FP16
      required:                  NEON
      disabled:                  VFPV3

  C/C++:
    Built as dynamic libs?:      YES
    C++ standard:                11
    C++ Compiler:                /workdir/recipe-sysroot-native/usr/bin/aarch64-phytec-linux/aarch64-phytec-linux-g++  (ver 10.2.0)
    C++ flags (Release):         -mcpu=cortex-a53 -march=armv8-a+crc+crypto -I/workdir/git/include  -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security  --sysroot=/workdir/recipe-sysroot  -O2 -pipe -g -feliminate-unused-debug-types -fmacro-prefix-map=/workdir=/usr/src/debug/opencv/4.5.2.imx-r0                      -fdebug-prefix-map=/workdir=/usr/src/debug/opencv/4.5.2.imx-r0                      -fdebug-prefix-map=/workdir/recipe-sysroot=                      -fdebug-prefix-map=/workdir/recipe-sysroot-native=  -fvisibility-inlines-hidden  -mcpu=cortex-a53 -march=armv8-a+crc+crypto -I/workdir/git/include  -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security  --sysroot=/workdir/recipe-sysroot   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -DNDEBUG  -DNDEBUG
    C++ flags (Debug):           -mcpu=cortex-a53 -march=armv8-a+crc+crypto -I/workdir/git/include  -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security  --sysroot=/workdir/recipe-sysroot  -O2 -pipe -g -feliminate-unused-debug-types -fmacro-prefix-map=/workdir=/usr/src/debug/opencv/4.5.2.imx-r0                      -fdebug-prefix-map=/workdir=/usr/src/debug/opencv/4.5.2.imx-r0                      -fdebug-prefix-map=/workdir/recipe-sysroot=                      -fdebug-prefix-map=/workdir/recipe-sysroot-native=  -fvisibility-inlines-hidden  -mcpu=cortex-a53 -march=armv8-a+crc+crypto -I/workdir/git/include  -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security  --sysroot=/workdir/recipe-sysroot   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -g  -DDEBUG -D_DEBUG
    C Compiler:                  /workdir/recipe-sysroot-native/usr/bin/aarch64-phytec-linux/aarch64-phytec-linux-gcc
    C flags (Release):           -mcpu=cortex-a53 -march=armv8-a+crc+crypto -I/workdir/git/include  -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security  --sysroot=/workdir/recipe-sysroot  -O2 -pipe -g -feliminate-unused-debug-types -fmacro-prefix-map=/workdir=/usr/src/debug/opencv/4.5.2.imx-r0                      -fdebug-prefix-map=/workdir=/usr/src/debug/opencv/4.5.2.imx-r0                      -fdebug-prefix-map=/workdir/recipe-sysroot=                      -fdebug-prefix-map=/workdir/recipe-sysroot-native=   -mcpu=cortex-a53 -march=armv8-a+crc+crypto -I/workdir/git/include  -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security  --sysroot=/workdir/recipe-sysroot   -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -DNDEBUG  -DNDEBUG
    C flags (Debug):             -mcpu=cortex-a53 -march=armv8-a+crc+crypto -I/workdir/git/include  -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security  --sysroot=/workdir/recipe-sysroot  -O2 -pipe -g -feliminate-unused-debug-types -fmacro-prefix-map=/workdir=/usr/src/debug/opencv/4.5.2.imx-r0                      -fdebug-prefix-map=/workdir=/usr/src/debug/opencv/4.5.2.imx-r0                      -fdebug-prefix-map=/workdir/recipe-sysroot=                      -fdebug-prefix-map=/workdir/recipe-sysroot-native=   -mcpu=cortex-a53 -march=armv8-a+crc+crypto -I/workdir/git/include  -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security  --sysroot=/workdir/recipe-sysroot   -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -g  -DDEBUG -D_DEBUG
    Linker flags (Release):      -Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed -Wl,-z,relro,-z,now  -Wl,--gc-sections -Wl,--as-needed  
    Linker flags (Debug):        -Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed -Wl,-z,relro,-z,now  -Wl,--gc-sections -Wl,--as-needed  
    ccache:                      NO
    Precompiled headers:         NO
    Extra dependencies:          dl m pthread rt
    3rdparty dependencies:

  OpenCV modules:
    To be built:                 aruco bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dnn_superres dpm face features2d flann fuzzy gapi hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python3 quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto
    Disabled:                    freetype world
    Disabled by dependency:      -
    Unavailable:                 alphamat cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv hdf java julia matlab ovis python2 sfm viz
    Applications:                perf_tests examples apps
    Documentation:               NO
    Non-free algorithms:         NO

  GUI: 
    GTK+:                        YES (ver 3.24.25)
      GThread :                  YES (ver 2.66.7)
      GtkGlExt:                  NO

  Media I/O: 
    ZLib:                        /workdir/recipe-sysroot/usr/lib/libz.so (ver 1.2.11)
    JPEG:                        /workdir/recipe-sysroot/usr/lib/libjpeg.so (ver 62)
    WEBP:                        /workdir/recipe-sysroot/usr/lib/libwebp.so (ver encoder: 0x020f)
    PNG:                         /workdir/recipe-sysroot/usr/lib/libpng.so (ver 1.6.37)
    TIFF:                        /workdir/recipe-sysroot/usr/lib/libtiff.so (ver 42 / 4.2.0)
    JPEG 2000:                   build (ver 2.4.0)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    GStreamer:                   YES (1.18.5)
    v4l/v4l2:                    YES (linux/videodev2.h)
    gPhoto2:                     YES

  Parallel framework:            TBB (ver 2021.2 interface 12020)

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Lapack:                      NO
    Custom HAL:                  YES (carotene (ver 0.0.1))
    Protobuf:                    /workdir/recipe-sysroot/usr/lib/libprotobuf.so (3.15.2)

  Python 3:
    Interpreter:                 /workdir/recipe-sysroot-native/usr/bin/python3-native/python3 (ver 3.9.5)
    Libraries:                   /workdir/recipe-sysroot/usr/lib/libpython3.9.so (ver 3.9.5)
    numpy:                       /workdir/recipe-sysroot/usr/lib/python3.9/site-packages/numpy/core/include (ver undefined - cannot be probed because of the cross-compilation)
    install path:                lib/python3.9/site-packages/cv2/python-3.9

  Python (for build):            /workdir/recipe-sysroot-native/usr/bin/python3-native/python3

  Install to:                    /usr
-----------------------------------------------------------------

I don’t know linux but can you open your camera with another application?

I assume that OPENCV is the case, as I execute the gst-launch-1.0 v4l2src device=/dev/video0 command on the video board! video/x-bayer,format=grbg,depth=8,width=1920,height=1080 ! bayer2rgbneon ! videoconvert ! jpegenc ! multifilesink location=live.avi and a video stream is created, everything works!

You can open with three api CAP_GPHOTO2 CAP_V4L or CAP_GSTREAMER
try

cv::VideoCapture cap(0, cv::CAP_V4L);

If it does not work try another api

1 Like

I tried all the above flags, nothing helps

Have you build opencv with test?
if yes there is a test opencv_test_videoio you can try it

please investigate “appsink” and “multifilesink”

gstreamer pipelines in a VideoCapture object must provide an appsink, or else opencv can’t read from the pipeline.