win7+Matlab2011b+VS2005环境下运行Deformable Part Models(voc-release4.01)目标检测matlab源码

配置方法如下:

1.打开voc-release4.01文件夹,在命令窗口输入:

mex -setup
2.回车后会出现:

Would you like mex to locate installed compilers [y]/n? n

输入n

3.之后会出现如下内容:

Select a compiler: 
[1] Intel C++ 12.0 (with Microsoft Visual C++ 2008 SP1 linker) 
[2] Intel C++ 12.0 (with Microsoft Visual C++ 2010 linker) 
[3] Intel C++ 11.1 (with Microsoft Visual C++ 2008 SP1 linker) 
[4] Intel Visual Fortran 12.0 (with Microsoft Visual C++ 2008 SP1 linker) 
[5] Intel Visual Fortran 12.0 (with Microsoft Visual C++ 2008 Shell linker) 
[6] Intel Visual Fortran 12.0 (with Microsoft Visual C++ 2010 linker) 
[7] Intel Visual Fortran 11.1 (with Microsoft Visual C++ 2008 SP1 linker) 
[8] Intel Visual Fortran 11.1 (with Microsoft Visual C++ 2008 Shell linker) 
[9] Lcc-win32 C 2.4.1 
[10] Microsoft Software Development Kit (SDK) 7.1 
[11] Microsoft Visual C++ 6.0 
[12] Microsoft Visual C++ 2005 SP1 
[13] Microsoft Visual C++ 2008 SP1 
[14] Microsoft Visual C++ 2010 
[15] Microsoft Visual C++ 2010 Express 
[16] Open WATCOM C++ 
[0] None 
我的机器是使用vs2005,选择12

4.回车后会出现如下提示:

C:\Program Files\Microsoft Visual Studio 8. Do you want to use this compiler [y]/n? y
选择y,回车,继续确认,直到出现Done . . . 

5.配置好后,在命令窗中输入compile,回车运行,会出现如下错误:

resize.cc 
resize.cc(36) : error C2057: expected constant expression 
resize.cc(36) : error C2466: cannot allocate an array of constant size 0 
resize.cc(36) : error C2133: 'ofs' : unknown size 
resize.cc(70) : error C3861: 'bzero': identifier not found 
resize.cc(95) : error C3861: 'round': identifier not found 
resize.cc(96) : error C3861: 'round': identifier not found 
 
  E:\MATLAB~1\BIN\MEX.PL: Error: Compile of 'resize.cc' failed. 
 
Error using mex (line 206)
Unable to complete successfully.

Error in compile (line 1)
mex -O resize.cc

6.打开compile文件, 发现首先会编译resize.cc,dt.cc,features.cc,getdetections.cc 这四个文件,既然不识别.cc文件,就将这四个.cc文件的扩展名都改为.cpp,同时也修改compile.m文件,将前四句改为:

mex -O resize.cpp
mex -O dt.cpp
mex -O features.cpp
mex -O getdetections.cpp

% use one of the following depending on your setup
% 0 is fastest, 3 is slowest 

% 0) multithreaded convolution using SSE
% mex -O fconvsse.cc -o fconv

% 1) multithreaded convolution using blas
%    WARNING: the blas version does not work with matlab >= 2010b 
%    and Intel CPUs
% mex -O fconvblasMT.cc -lmwblas -o fconv

% 2) mulththreaded convolution without blas
% mex -O fconvMT.cc -o fconv

% 3) convolution using blas
% mex -O fconvblas.cc -lmwblas -o fconv

% 4) basic convolution, very compatible
% mex -O fconv.cc -o fconv
mex -O fconv.cpp

7.保存后继续在命令窗compile,会出现如下错误:
resize.cpp 
resize.cpp(36) : error C2057: expected constant expression 
resize.cpp(36) : error C2466: cannot allocate an array of constant size 0 
resize.cpp(36) : error C2133: 'ofs' : unknown size 
resize.cpp(70) : error C3861: 'bzero': identifier not found 
resize.cpp(95) : error C3861: 'round': identifier not found 
resize.cpp(96) : error C3861: 'round': identifier not found 
 
  E:\MATLAB~1\BIN\MEX.PL: Error: Compile of 'resize.cpp' failed. 
 
Error using mex (line 206)
Unable to complete successfully.

Error in compile (line 1)
mex -O resize.cpp
8.在resize.cpp文件中的开始部分 加上bzero和round的定义

#define bzero(a,b) memset(a,0,b)
int round(float a){float tmp=a-(int)a;if(tmp>=0.5) return(int)a+1;else return (int)a;}

并并修改ofs数组的定义,将
alphainfo ofs[len]; 

改为

alphainfo *ofs = new alphainfo[len];
当然在同一作用域后面加上:
delete [] ofs; 

最后resize.cpp的文件内容为:

#include <math.h>
#include <assert.h>
#include <string.h>
#include "mex.h"
#define bzero(a,b) memset(a,0,b)
int round(float a){float tmp=a-(int)a;if(tmp>=0.5) return(int)a+1;else return (int)a;}

/*
 * Fast image subsampling.
 * This is used to construct the feature pyramid.
 */

// struct used for caching interpolation values
struct alphainfo {
  int si, di;
  double alpha;
};

// copy src into dst using pre-computed interpolation values
void alphacopy(double *src, double *dst, struct alphainfo *ofs, int n) {
  struct alphainfo *end = ofs + n;
  while (ofs != end) {
    dst[ofs->di] += ofs->alpha * src[ofs->si];
    ofs++;
  }
}

// resize along each column
// result is transposed, so we can apply it twice for a complete resize
void resize1dtran(double *src, int sheight, double *dst, int dheight, 
		  int width, int chan) {
  double scale = (double)dheight/(double)sheight;
  double invscale = (double)sheight/(double)dheight;
  
  // we cache the interpolation values since they can be 
  // shared among different columns
  int len = (int)ceil(dheight*invscale) + 2*dheight;
  //alphainfo ofs[len];
  alphainfo *ofs = new alphainfo[len];
  int k = 0;
  for (int dy = 0; dy < dheight; dy++) {
    double fsy1 = dy * invscale;
    double fsy2 = fsy1 + invscale;
    int sy1 = (int)ceil(fsy1);
    int sy2 = (int)floor(fsy2);       

    if (sy1 - fsy1 > 1e-3) {
      assert(k < len);
      assert(sy-1 >= 0);
      ofs[k].di = dy*width;
      ofs[k].si = sy1-1;
      ofs[k++].alpha = (sy1 - fsy1) * scale;
    }

    for (int sy = sy1; sy < sy2; sy++) {
      assert(k < len);
      assert(sy < sheight);
      ofs[k].di = dy*width;
      ofs[k].si = sy;
      ofs[k++].alpha = scale;
    }

    if (fsy2 - sy2 > 1e-3) {
      assert(k < len);
      assert(sy2 < sheight);
      ofs[k].di = dy*width;
      ofs[k].si = sy2;
      ofs[k++].alpha = (fsy2 - sy2) * scale;
    }
  }

  // resize each column of each color channel
  bzero(dst, chan*width*dheight*sizeof(double));
  for (int c = 0; c < chan; c++) {
    for (int x = 0; x < width; x++) {
      double *s = src + c*width*sheight + x*sheight;
      double *d = dst + c*width*dheight + x;
      alphacopy(s, d, ofs, k);
    }
  }
   delete []ofs;
}

// main function
// takes a double color image and a scaling factor
// returns resized image
mxArray *resize(const mxArray *mxsrc, const mxArray *mxscale) {
  double *src = (double *)mxGetPr(mxsrc);
  const int *sdims = mxGetDimensions(mxsrc);
  if (mxGetNumberOfDimensions(mxsrc) != 3 || 
      mxGetClassID(mxsrc) != mxDOUBLE_CLASS)
    mexErrMsgTxt("Invalid input");  

  double scale = mxGetScalar(mxscale);
  if (scale > 1)
    mexErrMsgTxt("Invalid scaling factor");   

  int ddims[3];
  ddims[0] = (int)round(sdims[0]*scale);
  ddims[1] = (int)round(sdims[1]*scale);
  ddims[2] = sdims[2];
  mxArray *mxdst = mxCreateNumericArray(3, ddims, mxDOUBLE_CLASS, mxREAL);
  double *dst = (double *)mxGetPr(mxdst);

  double *tmp = (double *)mxCalloc(ddims[0]*sdims[1]*sdims[2], sizeof(double));
  resize1dtran(src, sdims[0], tmp, ddims[0], sdims[1], sdims[2]);
  resize1dtran(tmp, sdims[1], dst, ddims[1], ddims[0], sdims[2]);
  mxFree(tmp);

  return mxdst;
}

// matlab entry point
// dst = resize(src, scale)
// image should be color with double values
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { 
  if (nrhs != 2)
    mexErrMsgTxt("Wrong number of inputs"); 
  if (nlhs != 1)
    mexErrMsgTxt("Wrong number of outputs");
  plhs[0] = resize(prhs[0], prhs[1]);
}



8.保存后继续compile,运行后结果:

dt.cpp 
dt.cpp(61) : error C2065: 'int32_t' : undeclared identifier 
dt.cpp(61) : error C2065: 'Ix' : undeclared identifier 
dt.cpp(61) : error C2059: syntax error : ')' 
dt.cpp(62) : error C2065: 'Iy' : undeclared identifier 
dt.cpp(62) : error C2059: syntax error : ')' 
dt.cpp(65) : error C2065: 'tmpIx' : undeclared identifier 
dt.cpp(65) : error C2059: syntax error : ')' 
dt.cpp(66) : error C2065: 'tmpIy' : undeclared identifier 
dt.cpp(66) : error C2059: syntax error : ')' 
 
  E:\MATLAB~1\BIN\MEX.PL: Error: Compile of 'dt.cpp' failed. 

解决办法是在dt.cpp文件中加入定义:

#define int32_t  int

保存后再compile,出现的错误如下:

 compile
features.cpp 
features.cpp(48) : error C3861: 'round': identifier not found 
features.cpp(49) : error C3861: 'round': identifier not found 
 
  E:\MATLAB~1\BIN\MEX.PL: Error: Compile of 'features.cpp' failed. 

解决方法:

在features.cpp文件中加入round函数的定义:

int round(float a){float tmp = a-(int)a; if(tmp>=0.5) return (int)a+1;else return (int)a;} 
9.保存后compile后的结果为:

 compile
fconv.cpp 
d:\智能车实验室\dpm资料\voc-release4.01\fconv.cpp(75) : error C4716: 'process' : must return a value 
 
  E:\MATLAB~1\BIN\MEX.PL: Error: Compile of 'fconv.cpp' failed. 

10. 修改fconv.cpp文件,将

void*process(void *thread_arg) 

修改为

void process(void *thread_arg) 

即去掉指针符号。

然后再次compile,没错误提示了,编译成功了。

11.在命令窗输入demo运行,可以看到DPM检测结果



参考资料:http://blog.csdn.net/masibuaa/article/details/17577195

                    http://blog.csdn.net/fuhpi/article/details/8767557



你可能感兴趣的:(DPM)