windows下的DPM经验

原文:http://blog.sciencenet.cn/blog-261330-663182.html
Discriminatively Trained Deformable Part Models的官网: http://www.cs.brown.edu/~pff/latent/

目前做目标检测最好的一个算法。

搞不懂为什么外国人老喜欢在linux下编代码,也许是因为版权的问题吧。

装了虚拟机在ubuntu下跑通了程序,但........你懂得(虚拟机能让你机子卡死)。

于是着手移植到windows下。

参考了pozen同学的博客:http://blog.csdn.net/pozen/article/details/7023742

启发很大,下面说说自己的调试过程:

1、把用到的文件dt.cc resize.cc fconv.cc features.cc的后缀都修改为cpp

2、dt.cpp中加:#define int32_t int

3、features.cpp、resize.cpp、fconv.cpp中加入

[html]  view plain copy
  1. #define bzero(a, b) memset(a, 0, b)   
  2. int round(float a) { float tmp = a - (int)a; if( tmp >= 0.5 ) return (int)a + 1; else return (int)a; }  

4、resize.cpp中的alphainfo ofs[len] 换成:alphainfo *ofs = new alphainfo[len] 当然要记得释放这个内存,但要注意放得位置。

之后就可以在matlab中运行compile进行编译了,这时会出现一些重定义的错误,应该是vc6.0对c++的一些不兼容,修改下就可以了。

5、输入demo()。查看结果。

调试过程中如何调试c程序参见前一篇文章。

说下自己的编译环境:vs2008&matlab2008b

贴出resize.cpp源代码(调试中因为内存错误折腾了很久)

[html]  view plain copy
  1. #include <math.h>  
  2. #include <assert.h>  
  3. #include <string.h>  
  4. #include "mex.h"  
  5.   
  6. #define bzero(a, b) memset(a, 0, b)   
  7. int round(float a) { float tmp = a - (int)a; if( tmp >= 0.5 ) return (int)a + 1; else return (int)a; }  
  8.   
  9. // struct used for caching interpolation values  
  10. struct alphainfo {  
  11.   int si, di;  
  12.   double alpha;  
  13. };  
  14.   
  15. // copy src into dst using interpolation values  
  16. void alphacopy(double *src, double *dst, struct alphainfo *ofs, int n) {  
  17.   struct alphainfo *end = ofs + n;  
  18.   while (ofs != end) {  
  19.     dst[ofs->di] += ofs->alpha * src[ofs->si];  
  20.     ofs++;  
  21.   }  
  22. }  
  23.   
  24. // resize along each column  
  25. // result is transposed, so we can apply it twice for a complete resize  
  26. void resize1dtran(double *src, int sheight, double *dst, int dheight,   
  27.           int width, int chan) {  
  28.   double scale = (double)dheight/(double)sheight;  
  29.   double invscale = (double)sheight/(double)dheight;  
  30.     
  31.   // we cache the interpolation values since they can be   
  32.   // shared among different columns  
  33.   int len = (int)ceil(dheight*invscale) + 2*dheight;  
  34.   //alphainfo ofs[len];  
  35.   alphainfo *ofs = new alphainfo[len];  
  36.   int k = 0;  
  37.   for (int dy = 0; dy < dheight; dy++) {  
  38.     double fsy1 = dy * invscale;  
  39.     double fsy2 = fsy1 + invscale;  
  40.     int sy1 = (int)ceil(fsy1);  
  41.     int sy2 = (int)floor(fsy2);         
  42.   
  43.     if (sy1 - fsy1 > 1e-3) {  
  44.       assert(k < len);  
  45.       assert(sy1-1 >= 0);  
  46.       ofs[k].di = dy*width;  
  47.       ofs[k].si = sy1-1;  
  48.       ofs[k++].alpha = (sy1 - fsy1) * scale;  
  49.     }  
  50.   
  51.     for (int sy = sy1; sy < sy2; sy++) {  
  52.       assert(k < len);  
  53.       assert(sy < sheight);  
  54.       ofs[k].di = dy*width;  
  55.       ofs[k].si = sy;  
  56.       ofs[k++].alpha = scale;  
  57.     }  
  58.   
  59.     if (fsy2 - sy2 > 1e-3) {  
  60.       assert(k < len);  
  61.       assert(sy2 < sheight);  
  62.       ofs[k].di = dy*width;  
  63.       ofs[k].si = sy2;  
  64.       ofs[k++].alpha = (fsy2 - sy2) * scale;  
  65.     }  
  66.   }  
  67.   
  68.  // delete [] ofs;  
  69.   // resize each column of each color channel  
  70.   bzero(dst, chan*width*dheight*sizeof(double));  
  71.   for (int c = 0; c < chan; c++) {  
  72.     for (int x = 0; x < width; x++) {  
  73.       double *s = src + c*width*sheight + x*sheight;  
  74.       double *d = dst + c*width*dheight + x;  
  75.       alphacopy(s, d, ofs, k);  
  76.     }  
  77.   }  
  78.    delete [] ofs;  
  79. }  
  80.   
  81. // main function  
  82. // takes a double color image and a scaling factor  
  83. // returns resized image  
  84. mxArray *resize(const mxArray *mxsrc, const mxArray *mxscale) {  
  85.   double *src = (double *)mxGetPr(mxsrc);  
  86.   const int *sdims = (int*)mxGetDimensions(mxsrc);  
  87.   if (mxGetNumberOfDimensions(mxsrc) != 3 ||   
  88.       mxGetClassID(mxsrc) != mxDOUBLE_CLASS)  
  89.     mexErrMsgTxt("Invalid input");    
  90.   
  91.   double scale = mxGetScalar(mxscale);  
  92.   if (scale > 1)  
  93.     mexErrMsgTxt("Invalid scaling factor");     
  94.   
  95.   int ddims[3];  
  96.   ddims[0] = (int)round(sdims[0]*scale);  
  97.   ddims[1] = (int)round(sdims[1]*scale);  
  98.   ddims[2] = sdims[2];  
  99.   mxArray *mxdst = mxCreateNumericArray(3, (mwSize*)ddims, mxDOUBLE_CLASS, mxREAL);  
  100.   double *dst = (double *)mxGetPr(mxdst);  
  101.   
  102.   double *tmp = (double *)mxCalloc(ddims[0]*sdims[1]*sdims[2], sizeof(double));  
  103.   resize1dtran(src, sdims[0], tmp, ddims[0], sdims[1], sdims[2]);  
  104.   resize1dtran(tmp, sdims[1], dst, ddims[1], ddims[0], sdims[2]);  
  105.   mxFree(tmp);  
  106.   
  107.   return mxdst;  
  108. }  
  109.   
  110. // matlab entry point  
  111. // dst = resize(src, scale)  
  112. // image should be color with double values  
  113. void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {   
  114.   if (nrhs != 2)  
  115.     mexErrMsgTxt("Wrong number of inputs");   
  116.   if (nlhs != 1)  
  117.     mexErrMsgTxt("Wrong number of outputs");  
  118.   plhs[0] = resize(prhs[0], prhs[1]);  
  119. }  

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值