我一直在尝试执行以下代码,但无法编译它。我通过将一些行作为注释来屏蔽错误。最后,我通过将所有其他行从face_cascade.load
删除到rectangle(image,faces[i],Scalar(0,125,165),2,8,0);
来达到CascadeClassifier face_cascade;
但是我不明白错误是什么!我是否以错误的方式声明了CascadeClassiier?
提前谢谢你。
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include<opencv2/features2d/features2d.hpp>
#include<iostream>
#include<stdio.h>
using namespace std;
using namespace cv;
int main()
{
Mat image=imread("taylor_swift.jpg");
imshow( "input", image );
cvtColor(image,image,CV_BGR2GRAY);
equalizeHist( image,image );
CascadeClassifier face_cascade;
face_cascade.load( "haarcascade_frontalface_alt2.xml" );
// Detect faces
vector<Rect> faces;
face_cascade.detectMultiScale(image,faces,1.5,3,0 |CV_HAAR_SCALE_IMAGE);
for(size_t i=0;i<faces.size();i++ )
{
rectangle(image,faces[i],Scalar(0,125,165),2,8,0);
}
imshow( "Detected Face", image );
waitKey();
return 0;
}
编辑:
Error 1 error LNK2019: unresolved external symbol "public: virtual __thiscall cv::CascadeClassifier::~CascadeClassifier(void)" (??1CascadeClassifier@cv@@UAE@XZ) referenced in function _main C:Usersvamsidhar muthireddydocumentsvisual studio 2010ProjectsOPEN_CV_TESTOPEN_CV_TESTmain.obj
Error 2 error LNK2019: unresolved external symbol "public: __thiscall cv::CascadeClassifier::CascadeClassifier(void)" (??0CascadeClassifier@cv@@QAE@XZ) referenced in function _main C:Usersvamsidhar muthireddydocumentsvisual studio 2010ProjectsOPEN_CV_TESTOPEN_CV_TESTmain.obj
Error 3 error LNK1120: 2 unresolved externals C:Usersvamsidhar muthireddydocumentsvisual studio 2010ProjectsOPEN_CV_TESTDebugOPEN_CV_TEST.exe 1
你一定看到这样的编译器错误:
/tmp/cckqEgtd.o: In function `main':
face.cpp:(.text+0x50): undefined reference to `cv::imread(std::string const&, int)'
face.cpp:(.text+0x87): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
face.cpp:(.text+0xca): undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
face.cpp:(.text+0x101): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
face.cpp:(.text+0x11a): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
face.cpp:(.text+0x13a): undefined reference to `cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
face.cpp:(.text+0x153): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
face.cpp:(.text+0x16c): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
face.cpp:(.text+0x185): undefined reference to `cv::equalizeHist(cv::_InputArray const&, cv::_OutputArray const&)'
face.cpp:(.text+0x194): undefined reference to `cv::CascadeClassifier::CascadeClassifier()'
face.cpp:(.text+0x1d7): undefined reference to `cv::CascadeClassifier::load(std::string const&)'
face.cpp:(.text+0x275): undefined reference to `cv::CascadeClassifier::detectMultiScale(cv::Mat const&, std::vector<cv::Rect_<int>, std::allocator<cv::Rect_<int> > >&, double, int, int, cv::Size_<int>, cv::Size_<int>)'
face.cpp:(.text+0x32a): undefined reference to `cv::rectangle(cv::Mat&, cv::Rect_<int>, cv::Scalar_<double> const&, int, int, int)'
face.cpp:(.text+0x36c): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
face.cpp:(.text+0x3af): undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
face.cpp:(.text+0x3d7): undefined reference to `cv::waitKey(int)'
face.cpp:(.text+0x3fa): undefined reference to `cv::CascadeClassifier::~CascadeClassifier()'
face.cpp:(.text+0x4f3): undefined reference to `cv::CascadeClassifier::~CascadeClassifier()'
/tmp/cckqEgtd.o: In function `cv::Mat::~Mat()':
face.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/cckqEgtd.o: In function `cv::Mat::release()':
face.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x47): undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
当您未将可执行文件链接到所需的库时,会发生这些未定义的引用错误。
如果你在 linux 机器上,那么你可以使用它来编译你的代码。
g++ face.cpp `pkg-config opencv --libs`
编辑:
看看 http://docs.opencv.org/doc/tutorials/introduction/windows_visual_studio_Opencv/windows_visual_studio_Opencv.html.OpenCV 文档中的此链接解释了如何使用 OpenCV 配置 Visual Studio
下面是一个属性表,可以根据设置进行少量修改,以配置 Visual Studio https://raw.githubusercontent.com/OpenNI2-Book/openni2book/master/props/OpenCV.props
您可能需要根据安装目录更改某些路径。此外,您还必须根据您拥有的 opencv 版本更改库名称。例如,如果您有OpenCV 2.4.10,那么您将opencv_core246.lib
替换为opencv_core2410.lib
等等。