OpenCV - Cannot open a window of my webcam - c++

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.

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

Related

opencv browser live streaming

Currently, I am using OpenCV to record a lifestream from my webcam. I now want to display that in a browser. I was thinking of using VideoWriter to write the video to a file, then somehow access that file from HTML5. Is this possible? Any other suggestions?
The following is the code I have.
int main(int argc, const char * argv[]) {
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) { // check if we succeeded
std::cout << "No camera found!\n";
}
namedWindow("Window",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
imshow("Window", frame);
char keypress;
keypress = waitKey(30);
if(keypress==27) break;
}
return 0;
}
As can be seen, I am displaying the live stream in a window, but, as said, I want a browser lifestream. Thanks.

Error R6010, when i want to use my webcamera. Why is it called?

I use VS 2010, with opencv. Whatever i try when i want to use my wecamera (on my laptop), i get this: "r6010 abort() has been called".
And a gray window shows up.
Here is the code:
#include<opencv\cv.h>
#include<opencv\highgui.h>
using namespace cv;
int main()
{
Mat image;
VideoCapture cap;
cap.open(0);
namedWindow("Window", 1);
while (1)
{
cap >> image;
imshow("Windwow",image);
waitKey(33);
}
}
Btw, in another program, what i got from youtube, it says "Error: Frame is NULL".
On my laptop, I have to use device 1 to access the webcam. You should also check whether cap is opened, e.g.
cap.open(1);
if(!cap.isOpened())
return -1;

Gray screen being displayed during video capture - OpenCV

I am trying to run a program for video capture from the webcam in OpenCV. Everytime I run the program, a gray screen is being displayed. I initially tried programming in C API using the CvCapture Function and it worked perfectly fine. But now in the C++ API when I try running the following code which uses VideoCapture, a gray screen is getting displayed.
How do I resolve this problem? Please help. My OpenCV version is 2.4.6 and I am running the code in MS Visual Studio 2010 Professional.
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
int main(int argc, char** argv)
{
VideoCapture capture(0);
Mat frame;
if( !capture.isOpened() )
throw "Error when reading steam_avi";
namedWindow( "w", 1);
for( ; ; )
{
capture.read(frame);
if(frame.empty())
break;
imshow("w", frame);
waitKey(1);
}
waitKey(0);
}
Your code is running fine on my laptop. Make sure that your camera device is not blocked by another application, or you can try to comment out the namedWindow call (but it should not be a problem), actually you can use following loop to grab video frames from camera:
VideoCapture capture(0);
Mat frame;
if( !capture.isOpened() )
throw "Error when reading steam_avi";
namedWindow( "w", 1);
while(capture.read(frame))
{
imshow("w", frame);
waitKey(1);
}
waitKey(0);
According to documentation: "If no frames has been grabbed (camera has been disconnected, or there are no more frames in video file), the methods return false and the functions return NULL pointer."

trying to use VideoCapture and imshow(), raises Assertion failed (size.width>0 && size.height>0) in cv::imshow

I'm using Visual Studio Express 2013 with OpenCV 2.4.7, following this tutorial.
I have spent hours searching the web for solutions, including all of the relevant SO questions. I have tried:
the return value of VideoCapture::open is 1
extending the waitKey() delay to 50ms and later 500ms
setting the dimensions of the window
creating another project on Visual C++
opening an existing image instead of reading from camera (same error)
but no luck, please help!
Here's my code:
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <iostream>
using namespace std;
using namespace cv;
int main() {
Mat image;
VideoCapture cap;
int camOpen = cap.open(CV_CAP_ANY);
namedWindow("window", CV_WINDOW_AUTOSIZE);
while (true) {
cap >> image;
imshow("window", image);
// delay 33ms
waitKey(33);
}
}
As I compiled and ran it, I got the following error:
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, file ........\opencv\modules\highgui\src\window.cpp, line 261
Error occurs at the line imshow("window", image);. When I commented it out, there are no complaints.
UPDATES:
A plausible explanation of why this error occured was that my webcam takes time to start, which is why image.empty() is true initially, hence the abort() function was called to exit the program.
With the code
if (!image.empty()) {
imshow("window", image);
}
we can wait for the camera to start
I tried your code and for me it works (it visualizes the current webcam input)!
I ran it on Visual Studio 2012 Ultimate with OpenCV 2.4.7.
...
The error occurs because the image is empty, so try this:
while (true) {
cap >> image;
if(!image.empty()){
imshow("window", image);
}
// delay 33ms
waitKey(33);
}
Maybe the first image you receive from your webcam is empty. In this case imshow will not throw an error. So hopefully the next input images are not empty.
Do this:
VideoCapture cap;
cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
int camOpen = cap.open(CV_CAP_ANY);
Or you can try changing this:
while (true) {
cap >> image;
imshow("window", image);
// delay 33ms
waitKey(33);
}
to
try
{
cap >> image;
imshow("window", image);
waitKey(33);
}
catch (Exception& e)
{
const char* err_msg = e.what();
std::cout << "exception caught: imshow:\n" << err_msg << std::endl;
}
Always check for errors.
You can either enable exception throwing from the VideoCapture instance, or you will have to check manually.
If checking manually, there are two places you need to check:
assert(cap.isOpened()); after creating/opening the VideoCapture instance. If it could not be opened, there is no exception being raised by that. You have to check and handle this. An assertion suffices but you can handle this more gracefully if you want.
if (image.empty()) break; after each cap >> image;, or if (!cap.read(image)) break; instead of cap.read(image);, because the video stream may have ended or there's some issue with the camera. Again, no automatic exception being thrown. You need to check and handle this. The read() may fail intermittently (i.e. next read() works again), but that's rare. Usually, once a read() fails, all following read() calls on that VideoCapture instance will also fail.
There is VideoCapture::setExceptionMode(), which allows read() and open() calls to actually throw exceptions on their own instead of failing silently. In that case, you still have to catch the exception and inspect it (e.what()). If you don't catch and inspect the exception explicitly, the runtime may just print to stderr that some exception occurred, but no details on it.
int i=0;
while(i<4)
{
VideoCapture cap(0); // force camera to open 4 tiMEs
i++;
}
waitKey(5000);
VideoCapture cap(0);
int camOpen = cap.open(CV_CAP_ANY);
namedWindow("window", CV_WINDOW_AUTOSIZE);
while (true) {
cap >> image;
imshow("window", image);
waitKey(33);
}
Do this it will work for you for sure.

VS 2010 turning on camera but only shows grey window

I'm new at this but have been doing my share of reading and trying different setups to help narrow down the problem! Any help tp get me past this road block would be much appreciated.
Currently I'm running: Win 7 Ultimate, Visual C++ 2010 Express, OpenCV 2.2.0, and a Microsoft - LifeCam Studio Webcam - Silver 1080p HD.
I'm getting no Build errors and when I run the program my camera comes on (blue light indicating it being on) and the screen pops up that i thought should show my camera feed but instead its just a grey box with nothing inside. The below code I thought would help narrow down the problem but I'm at a loss.
int main()
{
CvCapture *webcam = NULL;
webcam = cvCreateCameraCapture(-1);
if(webcam!=NULL)
{
IplImage *frame = cvQueryFrame(webcam);
cvShowImage("WEBCAM_TEST",frame);
cvWaitKey(0);
return 0;
}
else
{
std::cout<<"CAMERA NOT DETECTED"<<std::endl;
return 0;
}
}
your code is some times showing a black image sometimes showing a correct image on my system(Windows 7 64 VS2010 OpenCV 2.4.3)...how ever when I put it in a loop for non stop streaming the image is ok...so just modify your code slightly and try...
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
int main()
{
CvCapture *webcam = NULL;
webcam = cvCreateCameraCapture(-1);
if(webcam!=NULL)
{
while(true)
{
IplImage *frame = cvQueryFrame(webcam);
cvShowImage("WEBCAM_TEST",frame);
cvWaitKey(20);
}
}
else
{
std::cout<<"CAMERA NOT DETECTED"<<std::endl;
return 0;
}
return 0;
}
In OpenCV if you get frame just after creating camera capture usually it's grey. All you have to do is just get next frame or wait before getting first frame. This code:
int _tmain(int argc, _TCHAR* argv[])
{
VideoCapture cap(0);
if(!cap.isOpened())
return -1;
Mat frame;
namedWindow("01",1);
//cap >> frame; //option 1
//waitKey(5000); //option 2
cap >> frame;
imshow("01", frame);
int key = waitKey(30);
return 0;
}
will show grey frame, but if you uncomment option 1 or option 2 - it will work fine.