Why I can not open camera via opencv on Rasbery Pi?


I am trying to manipulate the video data from a camera. The camera works fine in opencv-python, but it fails to be opened in opencv c++.


#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <stdio.h>

using namespace cv;
using namespace std;

int main(int, char**)

    Mat frame;
    VideoCapture cap;
    // open the default camera using default API
    // cap.open(0);
    // OR advance usage: select any API backend
    int deviceID = 0;             // 0 = open default camera
    int apiID = cv::CAP_ANY;      // 0 = autodetect default API

    cout<< "cv version: " << CV_VERSION << endl;

    // open selected camera using selected API
    cap.open(deviceID, apiID);
//    cap = videoCapture(0);
    // check if we succeeded
    if (!cap.isOpened()) {
        cerr << "ERROR! Unable to open camera\n";
        return -1;

    cap.set(CAP_PROP_FRAME_WIDTH, 1280);
    cap.set(CAP_PROP_FRAME_HEIGHT, 400);

    cout << "Start grabbing" << endl
        << "Press any key to terminate" << endl;
    for (;;)
        // wait for a new frame from camera and store it into 'frame'
        // check if we succeeded
        if (frame.empty()) {
            cerr << "ERROR! blank frame grabbed\n";

        // show live and wait for a key with timeout long enough to show images
        imshow("Live", frame);
        if (waitKey(5) >= 0)
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;

"CMakeLists.txt "


# using C++11
set(CMAKE_CXX_FLAGS "${CAMKE_CXX_FLAGS} -std=c++11 ")

# find OpenCV

# show the message of OpenCV
message(STATUS "OpenCV library status:")
message(STATUS "    version:    ${OpenCV_VERSION}")
message(STATUS "    headers:    ${OpenCV_INCLUDE_DIRS}")
message(STATUS "    libraries:  ${OpenCV_LIBS}")

# link headers




It looks good when cmake generates the makefile:

root@ubuntu:/home/ubuntu/vision/testOpenCV# cmake .
-- OpenCV library status:
--     version:         4.5.4
--     headers:         /usr/local/include/opencv4
--     libraries:       opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_gapi;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_stitching;opencv_video;opencv_videoio;opencv_alphamat;opencv_aruco;opencv_barcode;opencv_bgsegm;opencv_bioinspired;opencv_ccalib;opencv_datasets;opencv_dnn_objdetect;opencv_dnn_superres;opencv_dpm;opencv_face;opencv_freetype;opencv_fuzzy;opencv_hdf;opencv_hfs;opencv_img_hash;opencv_intensity_transform;opencv_line_descriptor;opencv_mcc;opencv_optflow;opencv_phase_unwrapping;opencv_plot;opencv_quality;opencv_rapid;opencv_reg;opencv_rgbd;opencv_saliency;opencv_sfm;opencv_shape;opencv_stereo;opencv_structured_light;opencv_superres;opencv_surface_matching;opencv_text;opencv_tracking;opencv_videostab;opencv_viz;opencv_wechat_qrcode;opencv_xfeatures2d;opencv_ximgproc;opencv_xobjdetect;opencv_xphoto
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ubuntu/vision/testOpenCV

But it goes wrong when executes:

root@ubuntu:/home/ubuntu/vision/testOpenCV# sudo ./testOpenCV
cv version: 4.5.4-dev
[ WARN:0] global /home/ubuntu/opencv-master/modules/videoio/src/cap_v4l.cpp (890) open VIDEOIO(V4L2:/dev/video0): can't open camera by index
ERROR! Unable to open camera

How to solve the issue? Thanks!

try giving it /dev/video0 instead of the number.

cout << cv::getBuildInformation(), and python equivalent, and show both here. the Video I/O sections are most interesting.

Thank you for the reply.

The build information is here:

General configuration for OpenCV 4.5.4-dev =====================================
  Version control:               unknown

  Extra modules:
    Location (extra):            /home/ubuntu/opencv_contrib-master/modules
    Version control (extra):     unknown

    Timestamp:                   2021-11-04T08:27:52Z
    Host:                        Linux 5.4.0-1044-raspi aarch64
    CMake:                       3.10.2
    CMake generator:             Unix Makefiles
    CMake build tool:            /usr/bin/make
    Configuration:               Release

  CPU/HW features:
    Baseline:                    NEON FP16

    Built as dynamic libs?:      YES
    C++ standard:                11
    C++ Compiler:                /usr/bin/c++  (ver 7.5.0)
    C++ flags (Release):         -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    -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
    C++ flags (Debug):           -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    -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    C Compiler:                  /usr/bin/cc
    C flags (Release):           -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    -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
    C flags (Debug):             -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    -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
    Linker flags (Release):      -Wl,--gc-sections -Wl,--as-needed
    Linker flags (Debug):        -Wl,--gc-sections -Wl,--as-needed
    ccache:                      NO
    Precompiled headers:         NO
    Extra dependencies:          dl m pthread rt
    3rdparty dependencies:

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

  GUI:                           GTK2
    GTK+:                        YES (ver 2.24.32)
      GThread :                  YES (ver 2.56.4)
      GtkGlExt:                  NO
    VTK support:                 YES (ver 6.3.0)

  Media I/O:
    ZLib:                        /usr/lib/aarch64-linux-gnu/libz.so (ver 1.2.11)
    JPEG:                        /usr/lib/aarch64-linux-gnu/libjpeg.so (ver 80)
    WEBP:                        /usr/lib/aarch64-linux-gnu/libwebp.so (ver encoder: 0x020e)
    PNG:                         /usr/lib/aarch64-linux-gnu/libpng.so (ver 1.6.34)
    TIFF:                        /usr/lib/aarch64-linux-gnu/libtiff.so (ver 42 / 4.0.9)
    JPEG 2000:                   OpenJPEG (ver 2.3.0)
    OpenEXR:                     /usr/lib/aarch64-linux-gnu/libImath.so /usr/lib/aarch64-linux-gnu/libIlmImf.so /usr/lib/aarch64-linux-gnu/libIex.so /usr/lib/aarch64-linux-gnu/libHalf.so /usr/lib/aarch64-linux-gnu/libIlmThread.so (ver 2_2)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    DC1394:                      YES (2.2.5)
    FFMPEG:                      YES
      avcodec:                   YES (57.107.100)
      avformat:                  YES (57.83.100)
      avutil:                    YES (55.78.100)
      swscale:                   YES (4.8.100)
      avresample:                YES (3.7.0)
    GStreamer:                   NO
    v4l/v4l2:                    YES (linux/videodev2.h)

  Parallel framework:            pthreads

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Lapack:                      NO
    Eigen:                       YES (ver 3.3.4)
    Custom HAL:                  YES (carotene (ver 0.0.1))
    Protobuf:                    build (3.5.1)

  OpenCL:                        YES (no extra features)
    Include path:                /home/ubuntu/opencv-master/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python 2:
    Interpreter:                 /usr/bin/python2.7 (ver 2.7.17)
    Libraries:                   /usr/lib/aarch64-linux-gnu/libpython2.7.so (ver 2.7.17)
    numpy:                       /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.13.3)
    install path:                lib/python2.7/dist-packages/cv2/python-2.7

  Python 3:
    Interpreter:                 /usr/bin/python3 (ver 3.6.9)
    Libraries:                   /usr/lib/aarch64-linux-gnu/libpython3.6m.so (ver 3.6.9)
    numpy:                       /usr/local/lib/python3.6/dist-packages/numpy/core/include (ver 1.19.5)
    install path:                lib/python3.6/dist-packages/cv2/python-3.6

  Python (for build):            /usr/bin/python2.7

    ant:                         NO
    JNI:                         NO
    Java wrappers:               NO
    Java tests:                  NO

  Install to:                    /usr/local

When I try python script again, it reports the same error. The only difference from my early trial is the updating opencv from 3.2 to current. Is it related? How to locate the issue?


import cv2
cap = cv2.VideoCapture(0)

cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 400)

        ret, frame = cap.read()
        cv2.imshow('cam', frame)
        key = cv2.waitKey(1)
        if key & 0x00FF == ord('q'):

root@ubuntu:/home/ubuntu/vision/testOpenCV# sudo python3 cam.py
[ WARN:0] global /home/ubuntu/opencv-master/modules/videoio/src/cap_v4l.cpp (798) requestBuffers VIDEOIO(V4L2:/dev/video0): no support for memory mapping
[ WARN:0] global /home/ubuntu/opencv-master/modules/videoio/src/cap_v4l.cpp (798) requestBuffers VIDEOIO(V4L2:/dev/video0): no support for memory mapping
[ WARN:0] global /home/ubuntu/opencv-master/modules/videoio/src/cap_v4l.cpp (798) requestBuffers VIDEOIO(V4L2:/dev/video0): no support for memory mapping
[ WARN:0] global /home/ubuntu/opencv-master/modules/videoio/src/cap_v4l.cpp (798) requestBuffers VIDEOIO(V4L2:/dev/video0): no support for memory mapping
[ WARN:0] global /home/ubuntu/opencv-master/modules/videoio/src/cap_v4l.cpp (777) requestBuffers VIDEOIO(V4L2:/dev/video0): Insufficient buffer memory
[ WARN:0] global /home/ubuntu/opencv-master/modules/videoio/src/cap_v4l.cpp (890) open VIDEOIO(V4L2:/dev/video0): can't open camera by index

Please use ls /dev/video* to test which v4l2 device in your pi.

ubuntu@ubuntu:~/vision/testOpenCV$ ls /dev/video*
/dev/video0          /dev/video1   /dev/video11  /dev/video13  /dev/video15
/dev/video0_usb_cam  /dev/video10  /dev/video12  /dev/video14  /dev/video16

  1. Install media-ctl with sudo apt install v4l-utils
  2. Check v4l2 device information by using media-ctl -p

I geuss that you using a usb camera and your camera name may be /dev/video0_usb_cam.

1 Like

I connect 2 cameras on my Pi board. One is the original Pi camera by UART, other is a USB binocular camera. Unfortunately, none of them works with opencv on Pi.

ubuntu@ubuntu:~$ media-ctl -p
Media controller API version 5.4.143

Media device information
driver          bcm2835-isp
model           bcm2835-isp
bus info        platform:bcm2835-isp
hw revision     0x0
driver version  5.4.143

Device topology
- entity 1: bcm2835_isp0 (4 pads, 4 links)
            type Node subtype Unknown flags 0
        pad0: Sink
                <- "bcm2835-isp0-output0":0 [ENABLED,IMMUTABLE]
        pad1: Source
                -> "bcm2835-isp0-capture1":0 [ENABLED,IMMUTABLE]
        pad2: Source
                -> "bcm2835-isp0-capture2":0 [ENABLED,IMMUTABLE]
        pad3: Source
                -> "bcm2835-isp0-capture3":0 [ENABLED,IMMUTABLE]

- entity 6: bcm2835-isp0-output0 (1 pad, 1 link)
            type Node subtype V4L flags 0
            device node name /dev/video13
        pad0: Source
                -> "bcm2835_isp0":0 [ENABLED,IMMUTABLE]

- entity 12: bcm2835-isp0-capture1 (1 pad, 1 link)
             type Node subtype V4L flags 0
             device node name /dev/video14
        pad0: Sink
                <- "bcm2835_isp0":1 [ENABLED,IMMUTABLE]

- entity 18: bcm2835-isp0-capture2 (1 pad, 1 link)
             type Node subtype V4L flags 0
             device node name /dev/video15
        pad0: Sink
                <- "bcm2835_isp0":2 [ENABLED,IMMUTABLE]

- entity 24: bcm2835-isp0-capture3 (1 pad, 1 link)
             type Node subtype V4L flags 0
             device node name /dev/video16
        pad0: Sink
                <- "bcm2835_isp0":3 [ENABLED,IMMUTABLE]

those dimensions might not be accepted by the video device

Thanks for the information. The Pi can read the cammer data after I remove the dimension setting.

However, the binocular camera device combines two 640x400 images as one 1280x400 image output.

The 1280x400 output works fine in windows. How can I make it works on Pi system?