I'm using VideoCapture capture(filename); to load a video from a file. When I run the program in visual studio (release mode), it works just fine, loading the video like I would expect. When I run outside of visual studio (by double-clicking the icon in the explorer directory) the video cannot be found and the capture device returns null, even though it's the same file and paths are hardcoded and absolute.
Any ideas?
Update: Also tried using the old CvCapture* and same error.
Update 6/19:
Here's some example code.
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char** args)
{
const char* filename = "c:/testvideo.wmv";
//Check to see if we can see the file
FILE* myFile = fopen(filename, "r");
if (myFile)
cout<<"0: Found file"<<endl;
else
cout<<"0: File not found"<<endl;
//First use the openCV new way of doing it
VideoCapture capture1(filename);
if (capture1.isOpened())
cout<<"1: Opened the video successfully"<<endl;
else
cout<<"1: Could not open the video"<<endl;
//Second, try the old way
CvCapture* capture2 = cvCaptureFromFile(filename);
if (capture2)
cout<<"2: Opened the video successfully"<<endl;
else
cout<<"2: Could not open the video"<<endl;
//Pause
char c;
cin>>c;
return 0;
}
In Visual Studio running in release mode I get:
0: File Found
1: Opened the video successfully
2: Opened the video successfully
Running from the exe from the file system (double-clicking) I get:
0: File Found
1: Could not open the video
2: Could not open the video
I only compiled once, so there's only one exe in the directory...
I've also tried displaying the frames in Visual Studio, so I know it's actually really reading the video when it thinks it's open.
Check if all the DLL's that are required are in the same folder as your exe (or in PATH)
Make sure that you use absolute video path (if not, Try to copy video into exe path) and if you are using release mode, all dlls must be in the release mode. Maybe I will solve this problem if you send me a small project.
Related
As the title says. I've tried to load an image with argv and with absolute path and it worked, but with relative path it doesn't. The image is in the same directory of the executable. I'm using visual studio 2013 and opencv 2.4.10 on windows 7 64 bit. How can I resolve?
EDIT
Here's my code:
include <iostream>
#include <fstream>
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\stitching\stitcher.hpp"
#include "opencv2\core\core.hpp"
using namespace cv;
using namespace std;
void main()
{
vector< Mat > vImg;
Mat rImg;
vImg.push_back(imread("1.png"));
vImg.push_back(imread("2.png"));
vImg.push_back(imread("3.png"));
Stitcher stitcher = Stitcher::createDefault(true);
unsigned long AAtime = 0, BBtime = 0; //check processing time
AAtime = getTickCount(); //check processing time
stitcher.stitch(vImg, rImg);
BBtime = getTickCount(); //check processing time
printf("%.2lf sec \n", (BBtime - AAtime)
getTickFrequency()); //check processing time
namedWindow("Stitching Result");
imshow("Stitching Result", rImg);
waitKey(0);
}
I tried also to use "./" "/" "//" "\" and "\",but it still not working!
As CTZStef said, the images must not be in the sln folder but in the folder with the VC++ project file!
I could offer you the following way to deal with your problem:
In your IDE change the working directory in which your application is executed when run or debug it. Change it to the root of your data directory.
Start a command prompt and cd to the root of your data directory. Then start your application using the absolute path to it.
Add a compile definition which stores the absolute path of your data directory (add something like this /DDATA_DIR=C:\where\your\data\is to your compiler flags and in your code use vImg.push_back(imread("DATA_DIR/1.png"));)
Generally, if possible, I would keep your source code separate from your data and not copy it into the folder of your solution file.
Here is my code:
#include<opencv\cv.h>
#include<opencv\highgui.h>
using namespace cv;
using namespace std;
int main()
{
Mat src;
//src.create(200,500,CV_8UC3);
src = imread( "a.bmp", 1 );
namedWindow( "Display window", WINDOW_AUTOSIZE );
if(!src.data)
cout<<"Could not open or find the image" << std::endl ;
else
imshow( "Display window", src);
waitKey(0);
return 0;
}
It is always executing the if part
when I am using src.create instead of imread() it shows an empty image.
To debug your issue you should try to confirm that the image path is correct.
As suggested in the comments, try specifying the full absolute path of the file. Remember to to use escape slashes if you are on windows (e.g. c:\a.bmp will need to be "c:\a.bmp")
OR
If you are executing your application from Visual Studio then you can configure the working directory to be that of the bitmap too! (OpenCV cvLoadImage() does not load images in visual studio debugger?)
You can also try using cvLoadImage instead of imread. If cvLoadImage can open the file then it is possible that you have a mix of release and debug libraries causing you an issue as per:
OpenCV imread(filename) fails in debug mode when using release libraries
The OpenCV documentation has mentioned imread() would return an empty matrix ( Mat::data==NULL ) if the image file cannot be read.
http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#imread
You should check if the "a.bmp" file is in your current working directory. The IDE (visual studio) may set executable's working directory (Debug/... Release/... ) on purpose. Using an absolute path to "a.bmp", or starting executable in an expected directory from command line would also help, provided that "a.bmp" is a valid BMP file and you have the right file system permission to read it.
I was having the same issue on visual studion and imread returning null data.
img = cv::imread("c:\\data\\a.bmp",1);
Note the \\ delimiters to enable windows to correctly parse the path.
Your opencv libs should match the configi.e., Debug or Release of your application in Visual Studio or whatever build you are using (If you are using pre-built binaries) for WINDOWS.
I've written this in C++ (VS2012) using the OpenCV library (2.4.6).
#include <opencv2\opencv.hpp>
#include <opencv2\highgui\highgui.hpp>
int main(){
Mat image;
VideoCapture cap;
cap.open("test.avi");
if(!cap.isOpened()){
cout<< "Capture not open \n";
cin.get();
}
cvNamedWindow("Video Output");
while(1){
cap >> image;
imshow("Video Output",image);
waitKey(30);
}
}
Running it, the video capture fails to open.
test.avi is located in the same directory as the executable, and running it ind Debug/Release/outside the IDE makes no difference.
The OpenCv DLLs and the video file are here: https://www.dropbox.com/sh/16c04d97iw90gtk/88fQ4BLbfl#/
What could I be doing wrong?
EDIT: As seen in questions on the OpenCV Q&A site, I've copied the opencv_ffmpeg DLL to the folder with my executable. Now it only works outside the IDE (VS2012)
You should copy the opencv_ffmpeg DLL in your targetDir, in this case the Debug executable folder, other than the Release one
I try bulid face detect applicatyion on Visual Studio 2010 using C++ and OpenCV 2.3.1 library
So, I declaration String type and initialize:
String face_cascade_name = "haarcascade_frontalface_alt.xml";
Next, I create a object of class CascadeClassifier:
CascadeClassifier face_cascade;
And I load cascade:
if( !face_cascade.load(face_cascade_name) ){ printf("--(!)Error loading\n"); return -1; };
The project bulid and debugging without problem, but when I run application and application try load cascade program crash!
And I see this communication:
The program '[1288] OpenCV2.0.exe: Native' has exited with code -1 (0xffffffff).
I had the same problem with CascadeClassifier and FileStorage.
For example if you try this:
FileStorage fs(xml_fname, FileStorage::READ);
if (!fs.isOpened())
{
cout<<"can not read xml"<<endl;
}
Probably, it won't work.
In my case, I passed in VC++ from Debugging mode to Release mode, specified .lib files without d at the end (e.g. opencv_core231.lib) and it works now.
In my case(OSX 10.9), I input the whole path instead of simply "haarcascade_frontalface_alt.xml", like "/Users/xxx/Desktop/opencv-2.4.7/data/haarcascades/haarcascade_frontalface_alt.xml". Good Luck
In my case (win7 64, VS 10 express), changing file permissions for xml files solved the problem,
I added Everyone with full controll, and it worked.
I am trying to work out a simple hello world for OpenCV but am running out of ideas as to why it is not working.
When I compile and run this code:
#include <cv.h>
#include <highgui.h>
int main(int argc, char* argv[])
{
IplImage* img = cvLoadImage( "myjpeg.jpg" );
cvNamedWindow( "MyJPG", CV_WINDOW_AUTOSIZE );
cvShowImage("MyJPG", img);
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "MyJPG" );
return 0;
}
I get a grey box about 200x200 instead of the indicated .jpg file. If I use a different jpg I get the same kind of window, and if I put an invalid filename in, I get a very tiny window (expected).
I am using Visual Studio 2008 under Windows 7 Professional.
Most of the sample programs seem to work fine, so I am doubly confused how that code loads the sample jpgs just fine but in the code above it does not work (even tried the sample jpeg).
Update
The executables produced by compiling work fine, however the Visual Studio 2008 debugger loads a null pointer into img every time I try to run the debugger - regardless if the file location is implicit or explicit.
It really seems like there's a problem with the path to myjpeg.jpg since the current directory could be different when you're running under the debugger.
By default, the current directory that the Visual Studio debugger uses is the directory containing the .vcproj file, but you can change it in the project properties (Debugging -> Working Directory).
Are you 100% sure that you pass the absolute path correctly? Try to pass the same path to fopen and see if it also returns NULL. If so, then the path is incorrect.
If you want to see exactly what file is the library trying to open you can use Project Monitor with a filter on myjpeg.jpg.
Which version of OpenCV are you using? I've tried your code on the latest (OpenCV2.0) and it works fine. You can download OpenCV2.0 from here.
If you want the latest build, you can get check it out with SVN from here.
Try to add HAVE_JPEG into preprocessor definitions.
I encountered the same problem. The Debug version does not load the image, but when I compile and link it as a Release it works. I hope this helps