0

I would like to achieve the requirement of obtaining real-time camera feed from the currently connected camera on a Windows operating system using C++ environment. However, after multiple attempts and debugging, I have found that my computer is unable to successfully retrieve frames from the camera. So, I wrote a simple program that aimed to accomplish one task: displaying the video feed from camera device with ID 0 on the screen. However, it failed to work as expected. I noticed that the VideoCapture video successfully opened the device with ID 0, but after executing video >> picture;, the picture object remains empty, which is quite perplexing to me. The following is the complete code.

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    try {
        VideoCapture video;   
        Mat picture;  
        video.open(0);  
        if (!video.isOpened())  
        {
            cout << "Failed to open camera" << endl;
            return -1;
        }
        while (1) 
        {
            video >> picture; 

            if (picture.empty()) {
                cout << "Empty frame" << endl;
                break;
            }
            imshow("input", picture); 
            if (waitKey(30) == 27) 
                break;
        }
    }
    catch (const cv::Exception& e) {
        cout << "OpenCV exception: " << e.what() << endl;
    }
    catch (const std::exception& e) {
        cout << "Exception: " << e.what() << endl;
    }
    catch (...) {
        cout << "Unknown exception occurred" << endl;
    }

    return 0;
}

The following is the output. enter image description here By the way, I am sure that the camera that I am trying to open exists, because I can see the light on my camera turned blue when I run the process, which means the camera is on.

I don't know how to fix it, it seems that most people can easily achieve the target when running this process, but I am not.I hope someone can help me.

  • Try "if (picture.empty()) { continue;" sometimes cameras and video files fail to deliver the first frames. – Micka Aug 11 '23 at 12:33
  • Also try to open the canera with VLC and try to find out details about the stream. – Micka Aug 11 '23 at 12:34

1 Answers1

0

Have you checked the permissions? Did you gave your application permissions to access the camera via Windows? Here is a related question which is dealing with permission issues and could be helpful: opencv python camera permission issue on Windows 10

Zacryon
  • 639
  • 6
  • 11