0

I am trying to open a window of my webcam using opencv in c++ but it seems the webcam does not want to open. I tested before in other apps like cheese and it works.

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

int main() {
int PORT = 0;
cv::Mat image;
cv::namedWindow("Webcam window", cv::WINDOW_AUTOSIZE);
cv::VideoCapture cap(PORT);
cap.set(cv::CAP_PROP_FRAME_WIDTH, 640);
cap.set(cv::CAP_PROP_FRAME_HEIGHT, 480);

if (!cap.isOpened()) {
    std::cout << "Could not open the camera" << std::endl;
    return -1;
}
while (true) {
    cap >> image;
    if (!image.empty())
        cv::imshow("Webcam window", image);
    if (cv::waitKey(10) >= 0) {
        break;
    }
}
return 0;
}

I am using ubuntu budgie 21.10.

  • What is the error you are getting? – ksohan Apr 04 '22 at 20:39
  • It just goes in the 1st if and prints the text on the screen and then it closes. If I remove the return then I get the following ```error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'``` – BrosidenLegend Apr 04 '22 at 20:45
  • `return -1;` If this is the return you removed you should not remove that. It returns because of the failure. – drescherjm Apr 04 '22 at 21:01
  • I know that. And I removed it on purpose to tell the guy above what the exception is. I am asking why opencv does not open the webcam. cap.open() returns false. – BrosidenLegend Apr 04 '22 at 21:03

1 Answers1

0

Well this might sound silly, but I don't see a call to cv::open function. You mentioned that when you call it it simply returns false, but I don't see it in the code you provided.

You should also try to explicitly open default camera device.

Have you tried official examples from OpenCV? (such as https://docs.opencv.org/3.4/d8/dfe/classcv_1_1VideoCapture.html)

Do they work?

Also, you should turn on logging on the most verbose level, it often provide additional info about what's going on: How to enable logging for OpenCV

  • I added back the line ```cap.open(PORT);``` right after I created the cap object. I also added logging. Calling the open function does not change anything. https://pastebin.com/DCVbWgfm This is what I get when I run. – BrosidenLegend Apr 05 '22 at 06:27
  • It seems you have a problem with plugin candidates. Quick google for _getPluginCandidates Found 0 plugin(s)_ revealed others have the same problem [github](https://github.com/microsoft/vcpkg/issues/13859), [opencv](https://answers.opencv.org/question/236001/what-are-the-video-plugins/) [some other site](https://community.nxp.com/t5/eIQ-Machine-Learning-Software/PyeIQ-OpenCV-Issue-GStreamer-warning-unable-to-query-duration-of/m-p/1183771) and no one found satisfying answer. Last one mentioned to use this: `cv::VideoCapture cap(PORT, cv::CAP_GSTREAMER);` instead of `cap(PORT)` – Vladimir Fekete Apr 05 '22 at 10:55
  • I already read that posts before. The solution with cv:CAP_GSTREAMER does not work for me. – BrosidenLegend Apr 05 '22 at 19:19