Getting opencv error in c++ - c++

I'm try to get the error of opencv! say I have this program:
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
int main (){
cv::Mat frame;
cv::VideoCapture cap(1); // I don't have a second videoinput device!
int key = 0;
while(key !=27){
cap >> frame;
cv::imshow("frame",frame);
key = cv::waitKey(10);
}
cap.release();
return 0;
}
when I run this program I get in the console this message :
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in unknown functi
on, file ..\..\..\opencv\modules\highgui\src\window.cpp, line 261
My question is how can I get this message and save it in a string for every error that I get!
and if it'S possible escaping the program crash!
thanks in advance!

It uses C++ exceptions. See here in the doc for more.
try
{
... // call OpenCV
}
catch( cv::Exception& e )
{
const char* err_msg = e.what();
std::cout << "exception caught: " << err_msg << std::endl;
}
A CV_Assert in the OpenCV code is a macro which calls the OpenCV function error. That function can be seen here. It will always print the error text on stderr unless you don't have the customErrorCallback set. You do that via cvRedirectError, see here.

You have to check whether OpenCV function calls in your code is successfully executed or not. Then you can understand the exact problem. Here is the modified code.
int main (){
cv::Mat frame;
cv::VideoCapture cap(1); // I don't have a second videoinput device!
if ( !cap.isOpened() ) // if not success, exit program
{
cout << "Cannot open the video cam" << endl;
return -1;
}
int key = 0;
while(key !=27){
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read the frame from video cam" << endl;
break;
}
cv::imshow("frame",frame);
key = cv::waitKey(10);
}
cap.release();
return 0;
}

Related

opencv 3.2 C++ VS2015 imshow access violation executing

I am trying to use the sample code to get image via the embedded camera of my laptop. I can get the image. But the imshow statement throw Access violation executing
Anyone can help me to figure out the reason?
Deeply appreciate!
Environment: Visual Studio 2015, Opencv 3.2
c++ code:
using namespace cv;
using namespace std;
int main(int, char**)
{
Mat frame;
VideoCapture cap;
cap.open(0);
if (!cap.isOpened())
{
cerr << "ERROR, unable to open the camera\n";
return -1;
}
std::cout << "start grapping" << endl;
for (;;)
{
cap.read(frame);
if (frame.empty())
{
cerr << "ERROR, blank frame grabbed \n";
break;
}
// I added below statement to save the frame.
// check the frame from the camera.
imwrite("D:\\CapturedImage.jpg", frame);
// Below line throw Access violation executing location error
imshow("Live", frame);
}
}

Can we use pthread library for opencv C++ programming?

I have implemented a openCV program which can capture frames from video file and process it and create a new file. That is doing for single in file . Now I want for multiple files . then I have an idea about POSIX thread pthread library . Is that is a good or bad idea . Actually when I implement pthreads in opencv program I got some errors like following :
OpenCV Error: Assertion failed (_src.sameSize(_dst) && dcn == scn) in
accumulate, file
/home/satinder/opencv_installation/OpenCV/opencv/modules/imgproc/src/accum.cpp,
line 915
what():
/home/satinder/opencv_installation/OpenCV/opencv/modules/imgproc/src/accum.cpp:915:
error: (-215) _src.sameSize(_dst) && dcn == scn in function accumulate
Aborted (core dumped)
corrupted double-linked list: 0x00007fcd048f73d0 ***
Aborted (core dumped)
seg fault also some time .
Is there any possible way how I can implement multi-threading or equivalent my goal make a program which can get more that one input files for same processing.
FOllowing is my code snapshot :
#include "opencv2/highgui/highgui.hpp"
#include <sys/types.h>
#include <pthread.h>
#include <iostream>
using namespace cv;
using namespace std;
void * VideoCap(void *);
void * VideoCap(void *arg)
{
VideoCapture cap((char *)arg); // open the video file for reading
if ( !cap.isOpened() ) // if not success, exit program
{
cout << "Cannot open the video file" << endl;
exit(1);
}
//cap.set(CV_CAP_PROP_POS_MSEC, 300); //start the video at 300ms
double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video
cout << "Frame per seconds : " << fps << endl;
namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
while(1)
{
Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read the frame from video file" << endl;
break;
}
imshow("MyVideo", frame); //show the frame in "MyVideo" window
if(waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
}
int main(int argc, char* argv[])
{
int ret ;
pthread_t th[2];
ret = pthread_create(&th[0] , NULL , VideoCap , (void *)"cctv3.mp4");
if(0 == ret)
{
cout << "Thread 1 is created successfull" << endl;
}
ret = pthread_create(&th[1] , NULL , VideoCap , (void *)"cctv10.mp4");
if(0 == ret)
{
cout << "Thread 2 is created successfull" << endl;
}
pthread_join(th[0] , NULL);
pthread_join(th[1] , NULL);
return 0;
}
There are some problems with your code
namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
You are creating two threads, so the windows should have different identifiers.
namedWindow((char *)arg, CV_WINDOW_AUTOSIZE);
...
imshow((char *)arg, frame);
Since this question was posted with linux tag, I'm guessing that gtk is relevant. After inserting the directives
#include <gdk/gdk.h>
#include <gtk/gtkmain.h>
at the beginning of the file and then in the main()
pthread_t th[2];
gtk_disable_setlocale();
gtk_init(&argc, &argv);
gdk_threads_init();
New linker / compiler flags are probably needed,
g++ -O -Wall test.cpp -lopencv_highgui -lopencv_core `pkg-config --libs --cflags gdk-2.0 gtk+-2.0`
After these changes there was still an occasional crash, so I added gdk_threads_enter() and gdk_threads_leave(); calls to test if they would help:
gdk_threads_enter();
namedWindow ((char *) arg, CV_WINDOW_AUTOSIZE);
gdk_threads_leave();
Since the crashing was not reproducible, it is hard to tell if those lines have any effect.

opencv stereo camera error

i working in a stereo camera project i have two cameras 5megapixels in every one i connected it with my laptop and run my code but when i run it i get this error libv4l2: error turning on stream: No space left on device
im linux os that's my c++ opencv code there are any ideas how to fix it i tried others codes i found it in network but still give me the same error
#include <opencv2/opencv.hpp>
int main()
{
cv::VideoCapture cap1(1);
cv::VideoCapture cap2(2);
if(!cap1.isOpened())
{
std::cout << "Cannot open the video cam [1]" << std::endl;
return -1;
}
if(!cap2.isOpened())
{
std::cout << "Cannot open the video cam [2]" << std::endl;
return -1;
}
cap1.set(CV_CAP_PROP_FPS, 15);
cap2.set(CV_CAP_PROP_FPS, 15);
// Values taken from output of Version 1 and used to setup the exact same parameters with the exact same values!
cap1.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap1.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
cap2.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap2.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
cv::namedWindow("cam[1]",CV_WINDOW_AUTOSIZE);
cv::namedWindow("cam[2]",CV_WINDOW_AUTOSIZE);
while(1)
{
cv::Mat frame1, frame2;
bool bSuccess1 = cap1.read(frame1);
bool bSuccess2 = cap2.read(frame2);
if (!bSuccess1)
{
std::cout << "Cannot read a frame from video stream [1]" << std::endl;
break;
}
if (!bSuccess2)
{
std::cout << "Cannot read a frame from video stream [2]" << std::endl;
break;
}
cv::imshow("cam[1]", frame1);
cv::imshow("cam[2]", frame2);
if(cv::waitKey(30) == 27)
{
std::cout << "ESC key is pressed by user" << std::endl;
break;
}
}
return 0;
}

opencv_ffmpeg**.dll error in OpenCV 2.4.11 for IP Camera Access?

I try to get the live stream from my IP camera by using OpenCv 2.4.11 on Windows. However, it raises error and when I debug the an access occured as
Unhandled exception at 0x0000000066E5377F (opencv_ffmpeg2411_64.dll) in opencvAxisExp.exe: 0xC0000005: Access violation reading location 0x0000000000000000.
I use the code below to do so;
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <iostream>
int main(int, char**) {
cv::VideoCapture vcap;
cv::Mat image;
// This works on a D-Link CDS-932L
//const std::string videoStreamAddress = "http://user:pass#ip_address:port/axis-cgi/jpg/image.cgi?camera=1&resolution=320x240&compression=25 ";
const std::string videoStreamAddress = "http:/user:pass#ip_address:port/axis-cgi/mjpg/video.cgi";
//open the video stream and make sure it's opened
if (!vcap.open(videoStreamAddress)) {
std::cout << "Error opening video stream or file" << std::endl;
return -1;
}
for (;;) {
if (!vcap.read(image)) {
std::cout << "No frame" << std::endl;
cv::waitKey();
}
cv::imshow("Output Window", image);
if (cv::waitKey(1) >= 0) break;
}
}
Is this a dll error or what is missing here? Do you have any suggestion? Thanks for any comment.
I'd guess you might be missing the mjpg suffix. See this answer.

OpenCV 2.4.6.1: Error while grabbing frame from camera [duplicate]

This question already has answers here:
Getting opencv error in c++
(2 answers)
Closed 6 years ago.
I am using XCode (OS X Mountain Lion) with OpenCV. OpenCV is installed via homebrew (version 2.4.6.1)
My program should just access the camera.
Here is my code sofar:
using namespace cv;
int main(int argc, const char * argv[])
{
Mat frame;
VideoCapture cap(CV_CAP_ANY);
if (!cap.isOpened())
{
std::cerr << "Webcam error. Was not able to open webcam!\n";
exit(1);
}
namedWindow("webcam", CV_WINDOW_AUTOSIZE);
while (cap.isOpened())
{
cap >> frame;
if (frame.empty())
{
std::cerr << "Frame data error.\n";
}
imshow("webcam", frame);
if(waitKey(50) >= 0)
{
cap.release();
std::cout << "Webcam closed.\n";
}
}
std::cout << "The Program has finished.";
return 0;
}
But I am getting the output:
Frame data error.
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /tmp/default-mebu/opencv-2.4.6.1/modules/highgui/src/window.cpp, line 261
libc++abi.dylib: terminate called throwing an exception
(lldb)
I think that my program is not accessing the camera properly. It is somehow not able to get the data.
I know there have been some problems with Linux but I thought they have been fixed and I am not sure how they influenced OS X.
Does anybody know a solution to my problem?
Edit:
So I found a solution. I added a try {} catch {} for the imshow. Now my program does not exit when it hits the imshow. Instead it just throughs an error and keeps the while loop running. It misses a few frames but still gets enough to maintain a good videostream.
try
{
imshow("webcam", frame);
}
catch (Exception& e)
{
const char* err_msg = e.what();
std::cout << "exception caught: imshow:\n" << err_msg << std::endl;
}
The error thrown is still the same one:
frame data error.
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /tmp/default-mebu/opencv-2.4.6.1/modules/highgui/src/window.cpp, line 261
exception caught: imshow:
/tmp/default-mebu/opencv-2.4.6.1/modules/highgui/src/window.cpp:261: error: (-215) size.width>0 && size.height>0 in function imshow
VideoCapture cap(CV_CAP_ANY);
Sleep(1000); // Wait for response of camera, don't forget to #include <windows.h>
I had similar problem .You can add similar this code, maybe solve it. Because capture size cause this problem
VideoCapture cap;
cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
then
cap.read(image);
So I found a workaround. I added a try {} catch {} for the imshow. Now my program does not exit when it hits the imshow. Instead it just throughs an error and keeps the while loop running. It misses a few frames but still gets enough to maintain a good videostream.
try
{
imshow("webcam", frame);
}
catch (Exception& e)
{
const char* err_msg = e.what();
std::cout << "exception caught: imshow:\n" << err_msg << std::endl;
}