Can't open video by OpenCv ErrorUnhandled exception at 0x7695c41f in moving movie.exe: - opencv

I write a very simple program to test to open a video by openCV, but I fail.
Here is the code.
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv ;
void main( void) {
namedWindow( "Example3", WINDOW_AUTOSIZE );
VideoCapture cap;
cap.open( "D:\a.mp4" );
cv::Mat frame;
while( 1 ) {
cap >> frame;
//cap.read(frame);
if( !frame.data ) break;
cv::imshow( "Example3", frame );
waitKey(33);
}
}
It just flash the windows, However, if I comment the if( !frame.data ) break;
I will get this:
Unhandled exception at 0x7695c41f in moving movie.exe: Microsoft C++ exception: cv::Exception at memory location 0x0045f968..
Any idea?

cap.open( "D:\a.mp4" ); should be:
cap.open( "D:\\a.mp4" );

Try to put your video in the "current directory" so that you can give the path simply as cap.open( "a.mp4" );.
So, your program should look like following:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv ;
void main( void)
{
namedWindow( "Example3", WINDOW_AUTOSIZE );
VideoCapture cap;
cap.open( "a.mp4" );
cv::Mat frame;
while( 1 )
{
cap >> frame;
//cap.read(frame);
if( !frame.data ) break;
cv::imshow( "Example3", frame );
waitKey(33);
}
}
PS: I think the way you are giving path for video is also wrong, it should be forward slash / not backward which you are using i.e. \

Related

Error In OpenCv

I installed opencv and configure it from tutorials, but when I am trying a code implementing it, all what is related to opencv is shown as an error as shown in the figure. Can you please help me? I really need it
#include "stdafx.h"
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int Main( void)
{
CvCapture* capture = 0;
// Start Capture From WebCam
capture = cvCaptureFromCAM( CV_CAP_ANY );
if( !capture )
{
cout << "No camera Detected" << endl;
}
// Create New Window
cvNamedWindow( "My OpenCV WebCam", CV_WINDOW_AUTOSIZE );
if( capture )
{
cout << "WebCam Is In capture" << endl;
for(;;)
{
// Get Captured Image And Show It In The New Window
// You Can Do Save It Or Filter It
IplImage* iplImg = cvQueryFrame( capture );
// Use This To Filter Image
//cvNot(iplImg, iplImg);
cvShowImage( "My OpenCV WebCam", iplImg );
if( waitKey( 10 ) >= 0 )
break;
}
}
cvReleaseCapture( &capture );
cvDestroyWindow( "My OpenCV WebCam" );
return 0;
}
From the comments to the question it turned out that the OP was linking to OpenCV 2.4.8, instead of OpenCV 3.0.0.
Correcting the linked libraries name, as well as the library path, solved the issue.

opencv c++ HSV image channel separation exception

I know this question has been asked a number of times and I'm trying to implement their answers but its causing an exception in my code.
OS: Windows 7
OpenCV: 2.4.9
Here is my code:
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
VideoCapture cap(0); //capture the video from webcam
if ( !cap.isOpened() ) // if not success, exit program
{
cout << "Cannot open the web cam" << endl;
return -1;
}
Mat imgHSV;
_sleep(100); //give the camera a chance to wake up
while (true)
{
Mat imgOriginal;
bool success = cap.read(imgOriginal);
if (!success) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV
vector<Mat> hsv_planes;
split( imgHSV, hsv_planes );
//hsv_planes[0] // H channel
//hsv_planes[1] // S channel
//hsv_planes[2] // V channel
imshow(thresholded_window, hsg_planes[2]); //show the S channel
if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
Exception is thrown on the split line: "Unhandled exception at 0x62B978C9 (opencv_core249.dll) in TrackColour.exe: 0xC0000005: Access violation writing location 0xFEEEFEEE."
I figured out my problem, incase this happens to anyone else.
In VS I was doing a debug build but I was linking to the non debug versions of opencv. Notice in my error message opencv_core249, it should have been opencv_core249d (Note the 'd'). I updated my CV linking to use the debug libraries and it works.
The other opencv calls performed fine using the wrong libraries, something must be unique with split.

cv.h: file not found opencv

I'm running this code:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv ) {
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0; }
But it isn't working for me because it shows this error on my screen when I write cmake . in terminal:
cv.h: file not found.
Could anyone help me? It was working one month ago.
you have linking problem, try this out;
g++ `pkg-config --cflags --libs opencv` filename.cpp -o filename
You can change the linker properties in case of Visual studio.
For Makefile you need to make sure that the opencv_dir is set correctly.

OPEN CV Program execution error?

I am running my below code in eclipse, i have include the path and libraries successfuly, but when run the code it shows an error.
#include <cv.h>
#include<stdio.h>
#include <highgui.h>
//using namespace cv;
int main()
{
Mat image;
image = imread( argv[1], 1 );
if( argc != 2 || !image.data )
{
printf( "No image data \n" );
return -1;
}
namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
imshow( "Display Image", image );
waitKey(0);
printf("this is open cv programming");
return 0;
}
Your main() signature is incomplete
try
int main(int argc, char* argv[])
these parameters represent:
argc // an int indicating the number of arguments passed in to the function
argv[] // an array of character strings, the actual arguments.
The first argument argv[0] is the program name ... so argc is always a minimum of 1.
The second argument, argv[1] will be the first argument your user passes in, bringing argc up to 2. That is what your program is expecting, a single argument from the user, argc == 2.
Try to use the latest version of OpenCV i.e. 2.4.3....however right now you can try to link the debug libraries e.g. opencv_core2.4.xd and run the program to get the Mat image format working.
what is the version of opencv you are using?
try the following code and test...get some picture and run it....
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main()
{
Mat im = imread("C:\\some_picture.jpg");
if(im.empty())
return -1;
imshow("TEST",im);
waitKey();
return 0;
}

OpenCV VideoWriter won't write anything, although cvWriteToAVI does

I'm been trying to capture video from a cam and write it into an AVI file. I'm using Qt 4.8.2 with MSVC 2010 (x86) on Windows 7. I have 2 versions of the code: one using cv::Mat and the other using IplImage*. However, only the IplImage* version is working. Here's my code using cv::Mat:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main() {
VideoCapture* capture2 = new VideoCapture( CV_CAP_DSHOW );
Size size2 = Size(640,480);
int codec = CV_FOURCC('M', 'J', 'P', 'G');
VideoWriter* writer2 = new VideoWriter("video.avi",codec,15,size2);
int a = 100;
Mat frame2;
while ( a > 0 ) {
capture2->read(frame2);
writer2->write(frame2);
a--;
}
writer2->release();
capture2->release();
return 0;
}
And here's the code using IplImage*:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main() {
CvCapture* capture = cvCaptureFromCAM( CV_CAP_DSHOW );
CvSize size = cvSize(640,480);
int codec = CV_FOURCC('M', 'J', 'P', 'G');
CvVideoWriter* writer = cvCreateVideoWriter("video.avi",codec,15,size);
int a = 100;
while ( a > 0 ) {
IplImage* frame = cvQueryFrame( capture );
cvWriteToAVI(writer,frame);
a--;
}
cvReleaseVideoWriter(&writer);
cvReleaseCapture( &capture );
return 0;
}
It's basically the same, or at least it looks like the same thing to me. It reads 100 frames and should write them into "video.avi". It compiles and runs without errors, but the cv::Mat version doesn't write anything, and the IplImage* version works perfectly.
Does someone have any idea on what's going on?
The syntax in Opencv C++ reference is bit different, and here is a working code in C++.
I Just added imshow and waitkey, for checking you can remove them if you want.
int main()
{
VideoCapture* capture2 = new VideoCapture(CV_CAP_DSHOW);
Size size2 = Size(640, 480);
int codec = CV_FOURCC('M', 'J', 'P', 'G');
// Unlike in C, here we use an object of the class VideoWriter//
VideoWriter writer2("video_.avi", codec, 15.0, size2, true);
writer2.open("video_.avi", codec, 15.0, size2, true);
if (writer2.isOpened())
{
int a = 100;
Mat frame2;
while (a > 0)
{
capture2->read(frame2);
imshow("live", frame2);
waitKey(100);
writer2.write(frame2);
a--;
}
}
else
{
cout << "ERROR while opening" << endl;
}
// No Need to release the Writer as the distructor will called automatically
capture2->release();
return 0;
}
I had the same problem over and over again, and none of the solutions I found online helped.
Strange enough, the problem (identified purely with a trial and error method) was with the write permission. Everything worked after I sudo chmod u+rwx the python script.
I have the same problem and after a few time i realize that the input video isn't the same size with the output. Resize the input video may help u.
capture2->read(frame2);
cv::resize(frame2,frame2,cv::Size(640,480);
writer2->write(frame2);

Resources