OpenCV (CvHaarClassifierCascade*) cvLoad doesn't load, unable to load xml file - c++

I'm trying face detection using OpenCv 2.3. My trying to load "haarcascade_frontalface_alt_tree.xml" on my project, I'm constantly unable to load the xml file.
CvHaarClassifierCascade * pCascade = 0; // the face detector
const char* file ="C:\OpenCV2.3\opencv\data\haarcascades\haarcascade_frontalface_alt_tree.xml" ;
pCascade = (CvHaarClassifierCascade*) cvLoad(file , NULL, NULL, NULL);
if (!pCascade) {
exit(-1); // unable to load xml
}
I believe that I'm experiencing the same problem as this problem.
I have tried to load an image before the cvLoad command, but it didn't help.
I'm using OpenCV 2.3, made my configuration just like in this tutorial.
I'm using those libraries (I presume my configurations are correct, the file exist and can be open using Notepad++).
#include <stdio.h>
#include "opencv2\opencv.hpp"
#include "cv.h"
#include "highgui.h"
//#include "cvaux.h"
using namespace cv;
#pragma comment(lib, "opencv_core230d.lib")
#pragma comment(lib, "opencv_highgui230d.lib")
//#pragma comment(lib, "opencv_contrib230d.lib")
//#pragma comment(lib, "opencv_calib3d230d.lib")
//#pragma comment(lib, "opencv_features2d230d.lib")
//#pragma comment(lib, "opencv_flann230d.lib")
//#pragma comment(lib, "opencv_gpu230d.lib")
#pragma comment(lib, "opencv_haartraining_engined.lib")
#pragma comment(lib, "opencv_imgproc230d.lib")
//#pragma comment(lib, "opencv_legacy230d.lib")
//#pragma comment(lib, "opencv_ml230d.lib")
//#pragma comment(lib, "opencv_objdetect230d.lib")
//#pragma comment(lib, "opencv_video230d.lib")

To narrow down the issue, before calling cvLoad you should check to see if the file exists. Here's one way:
struct stat buf;
int statResult = stat(file,&buf);
if (statResult || buf.st_ino < 0) {
cout << "File not found: " << file << endl;
exit(-2);
}
You'll need to #include <sys/stat.h>
On my system (OS X 10.6.8/OpenCV 2.3), when I attempt to load haarcascade_frontalface_alt_tree.xml or haarcascade_frontalface_alt.xml I get an exception:
OpenCV Error: Unspecified error (The node does not represent a user object (unknown type?)) in cvRead, file /Users/steve/Development/opencv2/opencv/modules/core/src/persistence.cpp, line 4857
I think you are using an outdated OpenCV 1 tutorial that doesn't work with the current version of haarcascade_frontalface_alt_tree.xml. Try this OpenCV 2 tutorial instead. This code from that tutorial works for me:
CascadeClassifier face_cascade;
if (!face_cascade.load( file) ) {
cout << "Couldn't load face_cascade" << endl;
exit(-1);
}
cout << "Loaded face_cascade" << endl;

It happens also to me but finally I think I found the problem.
OpenCV has two different libraries *d.lib and *.lib the d means debug.
The problem is you need to setup the proper libraries to your environment (vs in my case) in the proper mode.
d.lib when you are in debug and .lib when you are in release.
Also in my project I need to run it in Release mode to make it work :)
This setup in my vs2009 could be found under Properties, Linker, Input, Additional dependencies.
Best regards

Check that the string with the "haarcascade_frontalface_alt.xml" file name is correct.
I had this problem and the directory separater was not being recognised. I changed the '\' character to '/' and the tutorial worked.
For your info, I was using MacOS 10.8.3 running Parallels with Windows 7, Visual Studio 2012 and opencv 2.44 - I was using the version 2 of the tutorial

Related

How to install GDI+ SDK

I recently downloaded GDI+ SDK from the following link:
https://www.microsoft.com/en-us/download/confirmation.aspx?id=18909
I have it, but it simply extracts two folders, one of which contains no code at all, just the eula.
I am not sure what you call the files I was given here. I assumed they would be a library, and should just be #included. However, c++ seems a lot less straight forward as to importing things(Java seemed a lot clearer to me on how to use external code). Then again, maybe I just haven't found that essential tutorial yet.
Either way, I am at a loss on how to incorporate GDI+ into my code.
The following site (https://msdn.microsoft.com/en-us/library/system.drawing.image(v=vs.110).aspx) says plainly that System.Drawing is a namespace, which to my knowledge is in GDI+. I have the following code:
#include "C:\GDI+\asms\10\msft\windows\gdiplus\gdiplus.dll"
#include <stdafx.h>
#include <windows.h>
#include <objidl.h>
#include <gdiplus.h>
When trying to code the following:
using namespace System.Drawing;
I get the error: Must be a namespace name.
How should I install the SDK files so that I can use them in my projects?
Clearly, I am not doing something right.
#include is for header files only, cannot be used to add *.dll files.
using namespace System.Drawing; is for .Net, C++ doesn't know what it is
In Visual Studio use this to link to library:
#pragma comment( lib, "Gdiplus.lib" )
You also need Gdi startup and shut down.
//#pragma warning( push ) optional
//#pragma warning( disable : 4458 )
#include <gdiplus.h>
//#pragma warning( pop )
//link to library:
#pragma comment( lib, "Gdiplus.lib" )
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR, int)
{
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
//do stuff
Gdiplus::GdiplusShutdown(gdiplusToken);
return 0;
}

Visual Studio 2010 64bit Library Build

Dear StackOverFlow experts,
I have searched and searched for a solution to this problem but have not found an answer. I have found people with similar questions but not an answer. I humbly ask for your forgiveness if I have overlooked a solution. With this I ask you to please consider my submission.
I have a simple test code for OpenCV 2.4.11 in Visual Studio 2010 x64
#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <opencv2\opencv.hpp>
#include <opencv2\core\types_c.h>
using namespace cv;
int main(){
IplImage* img=cvLoadImage("C:\\Users\\Russ\\Pictures\\3-7-15\\_DSC8489.jpg"); //change the name
cvNamedWindow("Example1",CV_WINDOW_NORMAL );
cvShowImage("Example1",img);
cvWaitKey(0);
cvReleaseImage(&img);
cvDestroyWindow( "Example1");
return 0;
}
When I Build this I get an error
C:\opencv\build\include\opencv2/core/types_c.h(55): fatal error C1083: Cannot open include file: 'assert.h': No such file or directory
All of the opencv includes are included in my properties path as are my libraries for OpenCV. When I open the types_c.h file I see the following
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
//M*/
#ifndef __OPENCV_CORE_TYPES_H__
#define __OPENCV_CORE_TYPES_H_
#if !defined _CRT_SECURE_NO_DEPRECATE && defined _MSC_VER
# if _MSC_VER > 1300
# define _CRT_SECURE_NO_DEPRECATE /* to avoid multiple Visual Studio 2005 warnings */
# endif
#endif
#ifndef SKIP_INCLUDES
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#if !defined _MSC_VER && !defined __BORLANDC__
# include <stdint.h>
#endif
#...
Since assert.h is used for debugging, I decided to comment out the assert.h line. When I Build the code again I got this error.
C:\opencv\build\include\opencv2/core/types_c.h(56): fatal error C1083: Cannot open include file: 'stdlib.h': No such file or directory
Clearly something is not set properly for Visual Studio 2010 as it can not find these libraries. Can someone tell how to fix this problem in Visual Studio 2010 x64. Thanks for your help.
For your case, you only need to include opencv2/opencv.hpp to make it work.
Also, since you're using C++, it's strongly recommended to use OpenCV's C++ API over deprecated C API. The code will be like:
#include <opencv2/opencv.hpp>
int main()
{
cv::Mat img = cv::imread("C:\\Users\\Russ\\Pictures\\3-7-15\\_DSC8489.jpg");
cv::namedWindow("Example1", CV_WINDOW_NORMAL);
cv::imshow("Example1", img);
cv::waitKey(0);
cv::destroyWindow("Example1");
return 0;
}
Updated:
If you still encounter with problems, you should follow this post to setup OpenCV correctly with VS.

Unresolved external symbol _IID_IDXGIFactory

I create Console Application in VC++ 2010, and add the following code to it:
#include <d3d10.h>
#include <d3dx10.h>
#include <DxErr.h>
#pragma comment(lib, "d3d10.lib")
#pragma comment(lib, "d3dx10.lib")
#pragma comment(lib, "dxgi.lib")
#pragma comment(lib, "dxerr.lib")
int _tmain(int argc, _TCHAR* argv[])
{
IDXGIFactory* pDXGIFactory;
CreateDXGIFactory(IID_IDXGIFactory, ( void** )&pDXGIFactory);
return 0;
}
Building this project, I have linker error: error LNK2001: unresolved external symbol _IID_IDXGIFactory
Now I create Console Application with MFC support, and add the same code. The build is successful. What is wrong in the first case? Why MFC project is built successfully, and non-MFC project fails?
You need to reference another library, which holds this identifier:
#pragma comment(lib, "windowscodecs.lib")
MSDN says its DXGI.lib but effectively at least Windows SDK 7.0 has it in a different library.
I had the same issue and In my case referencing
#pragma comment(lib, "dxgi.lib") solved the issue.

C++ write and read from mysql database

I'm working on a project where we want to write to a mysql database, I've googled around and tried a few implementations but they all fail.
For example I tried this: http://markalexanderbain.suite101.com/using-a-mysql-databases-with-c-a70097
#include "StdAfx.h"
#include <iostream>
#include <my_global.h>
#include <mysql.h>
#pragma comment(lib, "libmysql")
#pragma comment(lib, "mysqlclient")
using namespace std;
MYSQL *connection, mysql;
MYSQL_RES *result;
MYSQL_ROW row;
int query_state;
int main()
{mysql_init(&mysql);
//connection = mysql_real_connect(&mysql,"host","user","password","database",0,0,0);
connection = mysql_real_connect(&mysql,"localhost","bainm","not_telling","cpp_data",0,0,0);
if (connection == NULL)
cout << mysql_error(&mysql) << endl;
return 1;
}
It compiles and generates an exe file, but it closes every time I try to run it, I've added a cin.get at the end so it wouldn't close.
But the program still closes and Visual Studio 2010 gives me the following error message: The program '[32856] mysql test.exe: Native' has exited with code -1073741515 (0xc0000135).
The only thing that is common in all these implementations is the Include files and the #pragma comment(lib, "libmysql") and #pragma comment(lib, "mysqlclient")
I'm using these files which are included with mysql ga 5.5.16 32 bit.
So I'm looking for a way to write to a mysql database, if any one knows how to fix this to make it work or knows another way with a tutorial please let me know.
Error code 0xc0000135 means "The Application Failed To Initialize Properly". This is almost certainly due to a missing DLL (or two); probably the ones relating to MySQL mentioned in your #pragma lib statements.
Either add them to your path or copy them to the same directory as the .exe.
EDIT: Follow the instructions from here, in order to compile with the correct options. The file mysqlclient.dll should be included with the MySQL installation (do a system file find if you cannot find it).

Setup OpenCV-2.3 for Visual Studio 2010

I'm trying to use opencv 2.3 with Visual Studio 2010 Express. My code is from example:
#include "stdafx.h"
#include <highgui.h>
int _tmain(int argc, _TCHAR* argv[])
{
int c;
// allocate memory for an image
IplImage *img;
// capture from video device #1
CvCapture* capture = cvCaptureFromCAM(1);
// create a window to display the images
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
// position the window
cvMoveWindow("mainWin", 5, 5);
while(1)
{
// retrieve the captured frame
img=cvQueryFrame(capture);
// show the image in the window
cvShowImage("mainWin", img );
// wait 10 ms for a key to be pressed
c=cvWaitKey(10);
// escape key terminates program
if(c == 27)
break;
}
return 0;
}
What have I done so far?
Added build\bin and one of build\{x86|x64}\{vc9\vc10\mingw}\bin to my system path (to use DLLs).
Added build\{x86|x64}\{vc9\vc10\mingw}\lib or build\{x86|x64}\{vc9\vc10\mingw}\staticlib as library directories to my linker settings.
Added build\include and build\include\opencv as include directories to my compiler settings.
And the result is:
1>LINK : fatal error LNK1104: cannot open file 'c:\OpenCV2.3\build\x86\vc10\lib.obj'
There's no lib.obj in OpenCV folders. I've only unziped OpenCV-2.3.0-win-superpack.exe, without using CMake software.
What am I doing wrong?
Well, the official guide is for installing OpenCV 2.1 on VS2010, so I wrote some instructions below that shows how to properly install and configure the x86 version of OpenCV 2.3 on Visual Studio 2010 (Express), since a lot of folks seem to have problems setting it up correctly.
Download OpenCV-2.3.0-win-superpack.exe and execute it to extract all files to a folder named OpenCV2.3. Inside this folder there are 2 directories: build and opencv. All the setup on VS2010 will refer to the build directory. For practical purposes I moved the folder OpenCV2.3 to my C:\ drive, so pay attention to the paths I suggest on this guide as yours might be different.
On Visual Studio, create a new Win32 Console Application project and name it whatever you like. After that, a new window will show up. Click on the tab Application Settings and make sure the option Empty Project gets selected:
Add a new file main.cpp to the folder Source Files, then add this code to main.cpp:
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
int main(int argc, char* argv[])
{
if (argc < 2)
{
printf("Usage: ./opencv_hello <file.png>\n");
return -1;
}
IplImage* img = cvLoadImage(argv[1], CV_LOAD_IMAGE_UNCHANGED);
if (!img)
{
return -1;
}
cvNamedWindow("display", CV_WINDOW_AUTOSIZE);
cvShowImage("display", img );
cvWaitKey(0);
return 0;
}
At this point, we need to configure the project so it can locate OpenCV headers and libraries. Go to the Project Properties (ALT+F7), and once the new window shows up do the following:
On the Configuration box, select All Configurations
Open Configuration Properties > C/C++ > General, and edit the field Additional Include Directories to add these 3 paths (for the headers):
C:\OpenCV2.3\build\include\opencv
C:\OpenCV2.3\build\include\opencv2
C:\OpenCV2.3\build\include
Note that include\opencv is for the C interface of OpenCV and include\opencv2 if for the C++ interface. We are also adding the folder include to prevent our build from being broken by some headers of the C interface that refer to C++ headers as opencv2\core.
Then, add the path of the libraries on Configuration Properties > Linker > General, and on the Additional Library Directories field, add this: C:\OpenCV2.3\build\x86\vc9\lib:
Finally, for this simple test we are going to add the libraries opencv_core230.lib and opencv_highgui230.lib. So go to Configuration Properties > Linker > Input and add them:
When writing more complex applications you'll probably need to add other OpenCV libs that I did not
mentioned on this little project of ours.
Press F7 to Build Solution and you should see:
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
To be able to execute the application you'll need to modify the PATH environment variable of your system to add the location of OpenCV's DLLs. Add this to end of PATH:
; C:\OpenCV2.3\build\x86\vc9\bin
If you are struggling with editing the PATH environment variables, you can also copy the required .dll files to your project folder:
The dll files are located in this folder ../OpenCV2.3/build.x86/vc9/bin
Then copy them to the folder where .exe file is created:
c:\Users\PIMMES\Documents\Visual Studio 2010\Projects\eigenfaces\Debug (Ofcourse you have to change the path to your Debug folder)
You only have to copy the .dll files which you are using in your project (#include for example) For example if you get an error message saying opencv_core231d.dll is not found then get this .dll file from the above location (bin folder) and copy to your project Debug folder.
Hope this helps..
Whenever I make a program that uses opencv 2.2 or greater I include everything, and then comment out the libraries I don't need. Try this, I'm sure you need more than highgui.h
#include "opencv2\opencv.hpp"
using namespace cv;
//#pragma comment(lib, "opencv/opencv_calib3d231.lib")
//#pragma comment(lib, "opencv/opencv_contrib231.lib")
#pragma comment(lib, "opencv/opencv_core231.lib")
//#pragma comment(lib, "opencv/opencv_features2d231.lib")
//#pragma comment(lib, "opencv/opencv_flann231.lib")
//#pragma comment(lib, "opencv/opencv_gpu231.lib")
//#pragma comment(lib, "opencv/opencv_haartraining_engine.lib")
#pragma comment(lib, "opencv/opencv_highgui231.lib")
//#pragma comment(lib, "opencv/opencv_imgproc231.lib")
//#pragma comment(lib, "opencv/opencv_legacy231.lib")
//#pragma comment(lib, "opencv/opencv_ml231.lib")
#pragma comment(lib, "opencv/opencv_objdetect231.lib")
//#pragma comment(lib, "opencv/opencv_ts231.lib")
//#pragma comment(lib, "opencv/opencv_video231.lib")