近期下了一个matlab的 人体上半身探测的工具包calvin_upperbody_detector_v1.04,由于其本身是在linux下直接使用的,搬到win7上出现了一点问题。
在使用之前,要将其中的“me_HaarDetectOpenCV.cpp”文件编译为mexw64文件,编译时出现了link问题如下:
me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvCreateImage referenced in function mexFunction
me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvReleaseImage referenced in function mexFunction
me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvCreateMemStorage referenced in function mexFunction
me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvReleaseMemStorage referenced in function mexFunction
me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvGetSeqElem referenced in function mexFunction
me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvLoad referenced in function mexFunction
me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvEqualizeHist referenced in function mexFunction
me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvReleaseHaarClassifierCascade referenced in function mexFunction
me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvHaarDetectObjects referenced in function mexFunction
me_HaarDetectOpenCV.mexw64 : fatal error LNK1120: 9 unresolved externals
首先,由于该cpp使用的是opencv1.0,所以我先将电脑里的opencv2.3.1替换掉,将环境变量和VS2010里的配置也全部换成了opencv1.0,重新mex后还是一样的问题。
这里要提一下的是,我的系统和matlab均为64位版本,不存在不匹配的问题。
然后,我决定放弃opencv1.0,毕竟版本太老,并且与电脑上其他已经写过的程序又不兼容了。换回opencv2.3.1,改回环境变量和VS配置,重启电脑。
最重要的是,将“me_HaarDetectOpenCV.cpp”文件替换掉。替换的代码如下:
#include "mex.h"
#include <string>
#include "opencv2/opencv.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/imgproc/imgproc.hpp"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
// validate arguments
if (nrhs < 2) {
mexErrMsgTxt("Wrong number of input arguments.");
}
if (nlhs > 1) {
mexErrMsgTxt("Too many output arguments.");
}
if (!mxIsChar(prhs[0]) || mxGetM(prhs[0])!=1) {
mexErrMsgTxt("First argument must be a string.");
}
if (!mxIsUint8(prhs[1]) || mxGetNumberOfDimensions(prhs[0])!=2) {
mexErrMsgTxt("Second argument must be a uint8 grayscale image.");
}
// get XML cascade file name
char *xmlfile = mxArrayToString(prhs[0]);
cv::CascadeClassifier cascade;
if (!cascade.load(std::string(xmlfile))) {
mexErrMsgTxt("Failed to load cascade classifier.");
}
mxFree(xmlfile);
// get grayscale image
mwSize nrows = mxGetM(prhs[1]);
mwSize ncols = mxGetN(prhs[1]);
uint8_T *data = reinterpret_cast<uint8_T*>(mxGetData(prhs[1]));
// copy into an OpenCV mat (there are better ways to do this step!)
cv::Mat img(nrows, ncols, CV_8UC1, cv::Scalar::all(0));
for(mwIndex c=0; c<ncols; c++) {
for(mwIndex r=0; r<nrows; r++) {
img.at<char>(r,c) = data[r + nrows*c];
}
}
// process image before detection
cv::equalizeHist(img, img);
// detect faces
std::vector<cv::Rect> faces;
cascade.detectMultiScale(img, faces, 1.1, 4, 0, cv::Size(30,30));
// return rectangles found to MATLAB
plhs[0] = mxCreateDoubleMatrix(4, faces.size(), mxREAL);
double *out = mxGetPr(plhs[0]);
for(mwIndex i=0; i<faces.size(); i++) {
out[i+0] = static_cast<double>(faces[i].x);
out[i+1] = static_cast<double>(faces[i].y);
out[i+2] = static_cast<double>(faces[i].width);
out[i+3] = static_cast<double>(faces[i].height);
}
}
完成了最重要的一步后,紧接着,找到 ”D:\Program Files\MATLAB\R2013b\bin\win64\mexopts\msvc100opts.bat“文件,打开该文件作以下修改:
1.在 set PATH 处添加 D:\Program Files (x86)\opencv\build\x64\vc10\bin;D:\Program Files (x86)\opencv\build\common\tbb\intel64\vc10;
2.在 set INCLUDE 处添加 D:\Program Files (x86)\opencv\build\include;D:\Program Files (x86)\opencv\build\include\opencv;D:\Program Files (x86)\opencv\build\include\opencv2
3.在 set LIB 处添加 D:\Program Files (x86)\opencv\build\x64\vc10\lib;
4.在 set LINKFLAGS 处添加 opencv_calib3d231.lib opencv_contrib231.lib opencv_core231.lib opencv_features2d231.lib opencv_flann231.lib opencv_gpu231.lib opencv_highgui231.lib opencv_imgproc231.lib opencv_legacy231.lib opencv_ml231.lib opencv_objdetect231.lib opencv_ts231.lib opencv_video231.lib
修改完毕后,保存,重新在matlab中 mex -setup,再次编译需要mex的文件,顺利通过!