How to load xml cascade file in openCV 2.3.1 - c++

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.

Related

Error in opencv function in .exe file but not in debug nor release

One more time, I want to ask for your help after long and unfruitful researches.
I have developped an app using OpenCV which works very well is Visual Studio in debug and release mode.
I tried to deploy it, but i get a fatal error when I start the .exe. By using a log file and doing tests, I figured out the problem was in function cv::resize in the following code :
Mat logo = imread("lena.png");
cv::resize(logo, logo, Size(), 0.55, 0.55, INTER_CUBIC);
I found very weird that i can call cv::imread (and cv::Mat) without throwing exception but not resize.
If I try to run the .exe created by the compilation in release mode, i get the following errod code : 0xc000007b.
I read it could be a problem of compatibility between 32/64-bits dll, without being able to solve it. I try to build a 64-bits appplication. The path for opencv lib is define like this : C:\opencv\build\x64\vc14\lib
Thank you for your help,
Valentin B

Failed to use imwrite() in Qt Creator in Debug mode

I used imwrite() function in Qt Creator. I failed to run the code in Debug mode,while successfully in Release mode.
I wrote a test program.The followings is what I have done:
First to use OpenCV, I added the following code in .pro:
INCLUDEPATH+=D:\Work_Software\OpenCV3.1\opencv\build\include
LIBS+=D:\Work_Software\OpenCV3.1\opencv\build\x64\vc12\lib\*.lib
Then I added a Push Button. The slot function is:
void MainWindow::on_pushButton_clicked()
{
Mat img;
img=imread("F:\\My_Desktop\\foot1.jpg",0);
imwrite("F:\\My_Desktop\\result.jpg",img);
namedWindow("test");
imshow("test",img);
waitKey(0);
}
Finally, in Release mode, I can successfully read and write the image. However, failed to write in Debug mode. The error information is:
UPDATE
imread works in debug mode, for example, I change the slot function into:
void MainWindow::on_pushButton_clicked()
{
Mat img;
img=imread("F:\\My_Desktop\\foot1.jpg",0);
namedWindow("test");
imshow("test",img);
waitKey(0);
imwrite("F:\\My_Desktop\\result.jpg",img);
}
I can successfully load and imshow the image in Debug mode, but when I closed the windows, the same error happened.
The content of lib folder:
I have just seen a problem like mine similar problem , but it could not fix mine.
It happens because you included all library using *.lib command. In debug mode if you links with release libraries it fails. It works in release mode cause it links with release libs as it comes first due to string sort. See the image
Here 2411d.lib stands for debug library and 2411.lib stands for release library. I faced this issue and fixed separate linking in debug & release mode. You can either make 2 folders of debug and release libraries or you can mention the library names instead of *.lib.
[change the version for you]
Debug link: LIBS+=D:\Work_Software\OpenCV2.411\opencv\build\x64\vc12\lib\*‌​d.lib
Release link: LIBS+=D:\Work_Software\OpenCV2.411\opencv\build\x64\vc12\lib\*‌​2411.lib
OR
To separate folder See the images:
Folder stucture:
Debug Library folder:
Release Library folder:
UPDATE
If opencv is not built with qt properly please follow link
OpenCV dll and lib files are differ in terms of CPU architecture (32-64 bit) and Debug-Release mode. if you switch to Debug mode you must use dll and lib files for Debug mode (depends on CPU architecture).

Why is imread() always returning null data?

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.

OpenCV VideoCapture not working outside Visual Studio

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.

OpenCV cvLoadImage() does not load images in visual studio debugger?

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