matlab x64 mex,Matlab 生成mexwin64文件时遇到的问题

近期下了一个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

#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(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

for(mwIndex r=0; r

img.at(r,c) = data[r + nrows*c];

}

}

// process image before detection

cv::equalizeHist(img, img);

// detect faces

std::vector<: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

out[i+0] = static_cast(faces[i].x);

out[i+1] = static_cast(faces[i].y);

out[i+2] = static_cast(faces[i].width);

out[i+3] = static_cast(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的文件,顺利通过!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值