Ask Your Question
0

Getting error Assertion failed.

asked 2015-04-08 10:37:49 -0600

Akki gravatar image

updated 2015-04-08 10:43:27 -0600

berak gravatar image

hello,

I am trying to communicate with simple camera of my laptop, but some time this code work fine and some time I am getting error.

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

#include <iostream>
using namespace cv;
using namespace std;

int main() {
VideoCapture stream1(0);   //0 is the id of video device.0 if you have only one camera.
stream1.set(CV_CAP_PROP_FRAME_WIDTH, 640);
stream1.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
if (!stream1.isOpened()) { //check if video device has been initialised
cout << "cannot open camera";
}

//unconditional loop
while (true) {
Mat cameraFrame;
stream1.read(cameraFrame);
imshow("cam", cameraFrame);
Mat roi = cameraFrame( Rect(150,50,150,250) );
imshow("cam1",roi);

if (waitKey(30) >= 0)
break;
}
return 0;
}

is there any suggestion to overcome this error.

getting error as shown in attached image

Thank you very much in advance.

edit retag flag offensive close merge delete

Comments

1

could you replace the screenshot with the text of the error msg ?

berak gravatar imageberak ( 2015-04-08 10:42:23 -0600 )edit

Your roi is a single row... is that what you intend? Just a guess, but that single dimensionality may confuse the following imshow.

ClintFromVa gravatar imageClintFromVa ( 2015-04-08 12:33:12 -0600 )edit

thank you very much for comment.

If i remove the roi even though i am getting the same error.

Akki gravatar imageAkki ( 2015-04-08 13:44:50 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2015-04-08 15:50:30 -0600

ClintFromVa gravatar image

OK, I zoomed in on your screen grab enough to read the actual error message...it is saying that when imshow ran, it had an input that was size 0x0.

My best guess is that the camera is slower than your code and sometimes the capture fails, producing an empty image. I would put in a test for empty, just like when you use imread on a file.

edit flag offensive delete link more

Comments

Thank you very much for comment.

That means I have to add delay to capture the frame or?? How should I do that...??

thank you very much for time.

Akki gravatar imageAkki ( 2015-04-09 03:46:51 -0600 )edit

Sorry for the long absence, busy weekend. Anyway, for testing you could just add a raw delay at the top of your loop...something like waitkey(1000); Play with the number and see if there is a threshold which works, it would be cool to know just how fast the camera refreshes.

A better answer would be to check the size of the returned image and just try again when image.empty (or image.rows == 0 && image.cols == 0 ) is true.

Good luck!

ClintFromVa gravatar imageClintFromVa ( 2015-04-14 08:19:08 -0600 )edit
0

answered 2015-04-14 08:38:46 -0600

Akki gravatar image

Thank you very much I will try it.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-04-08 10:37:49 -0600

Seen: 262 times

Last updated: Apr 14 '15