This simple program is crashing at VideoCapture.
My configuration is OpenCV 2.0, MinGW, Windows7
int main()
{
cout<<"reached here"<<endl;
VideoCapture cap;
cap.open("Z:\\snapshot\\streaminput\\video1.avi");
if (!cap.isOpened())
{
cout<<"capture failed"<<endl;
// print error msg
return -1;
}
cout<<"capture successful"<<endl;
Mat frame;
namedWindow("gray",1);
do
{
cap >> frame;
cout<<"got a frame"<<endl;
imshow("gray", frame);
cvWaitKey(300);
}while(1);
//cvDestroyAllWindows();
cout << "Hello world!" << endl;
return 0;
}
CALLSTACK:
#0 002FB4B8 cv::VideoCapture::VideoCapture(int) ()(C:\OpenCV2.0\bin\libhighgui200.dll:??)
#1 77CCE115 ntdll!RtlAllocateMemoryZone() (C:\Windows\system32\ntdll.dll:??)
#2 00000000 0x0022fe78 in ??() (??:??)
PROGRAMOUTPUT(BEFORE CRASH POINT):
capture name is:Z:\snapshot\streaminput\video1.avi
Process returned 255 (0xFF) execution time : 9.874
Press any key to continue.
Any idea what is wrong in this program or in configuration??
Related
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);
}
}
First and foremost, I should say that I'm a beginner to OpenCV. I'm trying to convert a live video stream from my webcam from RGB to Grayscale.
I have the following code in my function:
VideoCapture cap(0);
while (true)
{
Mat frame;
Mat grayscale;
cvtColor(frame, grayscale, CV_RGB2GRAY);
imshow("Debug Window", grayscale);
if (waitKey(30) >=0)
{
cout << "End of Stream";
break;
}
}
I know it isn't complete. I'm trying to find a way to take a frame of the video and send it to frame, manipulate it using cvtColor, then output it back to grayscale so I can display it on my screen.
If anyone could help, it would be much appreciated.
Please see this example, here the complete code exists, hope this will work for you:
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
VideoCapture cap(0); // open the video camera no. 0
if (!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the video cam" << endl;
return -1;
}
namedWindow("MyVideo",CV_WINDOW_AUTOSIZE);
while (1)
{
Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess)
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
Mat grayscale;
cvtColor(frame, grayscale, CV_RGB2GRAY);
imshow("MyVideo", grayscale);
if (waitKey(30) == 27)
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
You just initialized the variable "frame" and forgot to assign an image to it. Since the variable "frame" is empty you won't get output. Grab a image and copy to frame from the video sequence "cap". This piece of code will do the job for you.
Mat frame;
bool bSuccess = cap.read(frame); // read a frame from the video
if (!bSuccess)
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
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.
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;
}
I'm trying to write my own app with OpenCV but i have problem. cvCaptureFromCAM() not loading any frame on my Mac. I tried this way:
CvCapture* capture = NULL;
if ((capture = cvCaptureFromCAM(-1)) == NULL) {
std::cerr << "!!! ERROR: vCaptureFromCAM No camera found\n";
return;
}
cvNamedWindow("webcam", CV_WINDOW_AUTOSIZE);
cvMoveWindow("webcam", 50, 50);
IplImage* src = NULL; for (;;) {
if ((src = cvQueryFrame(capture)) == NULL) {
std::cerr << "!!! ERROR: vQueryFrame\n";
break;
}
cvShowImage("webcam", &src);
}
cvReleaseCapture(&capture);
and this one:
cv::VideoCapture cap;
cap.open(0);
if( !cap.isOpened() )
{
std::cerr << "***Could not initialize capturing...***\n";
std::cerr << "Current parameter's value: \n";
return;
}
cv::Mat frame;
while(1){
cap >> frame;
if(frame.empty()){
std::cerr<<"frame is empty"<<std::endl;
break;
}
cv::imshow("", frame);
cv::waitKey(10);
}
Those two ways work good on linux, but not on my mac. My built-in camera works in skype and Photo Booth. What i do wrong? Have any ideas?