文章转自:http://blog.csdn.net/masibuaa/article/details/20651005
可变形部件模型Deformable Part Models(DPM)是非常经典的目标检测算法,由Felzenszwalb提出,本文介绍如何在windows下运行Felzenszwalb给出的DPM算法的star-cascade版本voc-release4.01-star-cascade,相比于基本版本voc-release4.01,star-cascade版本增加了PCA降维,检测速度可提高十几倍。
有关Deformable Part Model参见论文
A Discriminatively Trained, Multiscale,Deformable Part Model[CVPR 2008]的中文翻译
Object Detection with Discriminatively Trained Part Based Models[PAMI 2010]的中文翻译
及 有关可变形部件模型(Deformable Part Model)的一些说明
Deformable Part Model 相关网页(其中有源码下载):http://www.cs.berkeley.edu/~rbg/latent/index.html
star-cascade版本相关网页(其中有源码下载):http://people.cs.uchicago.edu/~rbg/star-cascade/
Pedro Felzenszwalb的个人主页:http://cs.brown.edu/~pff/
PASCAL VOC 目标检测挑战:http://pascallin.ecs.soton.ac.uk/challenges/VOC/
同样,Felzenszwalb给出的star-cascade DPM也只能在Linux或Mac系统上运行,我们可以对其进行一些修改,使之可以在Windows的Matlab上跑起来。
我的环境:Win7 + Matlab R2012b(其中配置VS2010中的c++编译器)
首先,在作者的网站上下载源码,由于star-cascade必须在DPM的基本版本之上运行,所以还需要下载对应的DPM源码,我这里用的都是第4版,即voc-release4.01 和 voc-release4.01-star-cascade。下载完成后首先要在Windows上把voc-release4.01跑起来,详见这篇文章:
在Windows下运行Felzenszwalb的Deformable Part Models(voc-release4.01)目标检测matlab源码
下载完voc-release4.01-star-cascade后,将star-cascade文件夹解压到voc-release4.01文件夹中(因为要在DPM之上运行)。发现其中有很多“._”开头的文件,这些是Linux或Mac系统上使用的(具体我也不是很懂),全部删掉,windows下用不到。其子文件夹data中的._开头的文件也可以删掉。
然后用notepad++或其他编辑器打开README,看看说明。README中说先进入star-cascade文件夹,编译源码,然后退回到voc-release4目录运行。在Linux下可以直接运行作者写好的Makefile文件进行编译,在windows上我们需要手动编译。打开Makefile文件,大体上看一看,发现需要编译cascade.cc,model.cc和fconv_var_dim.cc三个文件,接下来尝试用mex进行编译,看看会遇到什么错误。
步骤1 编译cascade.cpp和model.cpp
首先将cascade.cc,model.cc,fconv_var_dim.cc这三个文件的扩展名都改为.cpp,否则windows中无法识别cc文件。然后将matlab的工作目录定位到star-cascade文件夹。cascade.cpp和model.cpp需要同时编译,在matlab命令行中输入:
- >> mex cascade.cpp model.cpp
- \voc-release4.01-star-cascade\star-cascade\timer.h(6) : fatal error C1083: Cannot open include file: 'sys/time.h': No such file or directory
- D:\PROGRA~1\MATLAB\R2012B\BIN\MEX.PL: Error: Compile of 'cascade.cpp' failed.
- Error using mex (line 206)
- Unable to complete successfully.
- voc-release4.01-star-cascade\star-cascade\timer.h(21) : error C2079: 'tv' uses undefined struct 'timer::tic::timeval'
- voc-release4.01-star-cascade\star-cascade\timer.h(22) : error C3861: 'gettimeofday': identifier not found
- voc-release4.01-star-cascade\star-cascade\timer.h(23) : error C2228: left of '.tv_sec' must have class/struct/union type is 'int'
- voc-release4.01-star-cascade\star-cascade\timer.h(23) : error C2228: left of '.tv_usec' must have class/struct/union type is 'int'
- voc-release4.01-star-cascade\star-cascade\timer.h(28) : error C2079: 'tv' uses undefined struct 'timer::toc::timeval'
- voc-release4.01-star-cascade\star-cascade\timer.h(29) : error C3861: 'gettimeofday': identifier not found
- voc-release4.01-star-cascade\star-cascade\timer.h(30) : error C2228: left of '.tv_sec' must have class/struct/union type is 'int'
- voc-release4.01-star-cascade\star-cascade\timer.h(30) : error C2228: left of '.tv_usec' must have class/struct/union type is 'int'
- #ifndef _TIMER_H_
- #define _TIMER_H_
- #include <string>
- #include <sstream>
- //#include <sys/time.h> //windows下的time.h中没有timeval定义
- #include<windows.h> //windows下代替<sys/time.h>
- #include<time.h> //windows下代替<sys/time.h>
- using namespace std;
- //windows下没有gettimeofday函数,从网上找的一个替代函数
- int gettimeofday(struct timeval *tp, void *tzp)
- {
- time_t clock;
- struct tm tm;
- SYSTEMTIME wtm;
- GetLocalTime(&wtm);
- tm.tm_year = wtm.wYear - 1900;
- tm.tm_mon = wtm.wMonth - 1;
- tm.tm_mday = wtm.wDay;
- tm.tm_hour = wtm.wHour;
- tm.tm_min = wtm.wMinute;
- tm.tm_sec = wtm.wSecond;
- tm. tm_isdst = -1;
- clock = mktime(&tm);
- tp->tv_sec = clock;
- tp->tv_usec = wtm.wMilliseconds * 1000;
- return (0);
- }
- class timer {
- public:
- timer(string timer_name) {
- name = timer_name;
- total_time = 0;
- calls = 0;
- };
- ~timer() {};
- void tic() {
- struct timeval tv;
- gettimeofday(&tv, NULL);
- last_time = (double)tv.tv_sec + 1e-6*(double)tv.tv_usec;
- calls++;
- };
- void toc() {
- struct timeval tv;
- gettimeofday(&tv, NULL);
- double cur_time = (double)tv.tv_sec + 1e-6*(double)tv.tv_usec;
- total_time += cur_time - last_time;
- };
- const char *msg() {
- ostringstream oss;
- oss << "timer '" << name
- << "' = " << total_time << " sec in "
- << calls << " call(s)";
- return oss.str().c_str();
- };
- void mexPrintTimer() {
- mexPrintf("timer '%s' = %f sec in %d call(s)\n", name.c_str(), total_time, calls);
- };
- double getTotalTime() {
- return total_time;
- };
- private:
- string name;
- int calls;
- double last_time;
- double total_time;
- };
- #endif
- cascade.cpp(116) : error C2065: 'INFINITY' : undeclared identifier
- cascade.cpp(130) : error C2065: 'INFINITY' : undeclared identifier
- cascade.cpp(170) : error C2065: 'INFINITY' : undeclared identifier
- cascade.cpp(216) : error C2065: 'INFINITY' : undeclared identifier
- cascade.cpp(217) : error C2065: 'INFINITY' : undeclared identifier
- cascade.cpp(218) : error C2065: 'INFINITY' : undeclared identifier
- cascade.cpp(219) : error C2065: 'INFINITY' : undeclared identifier
- //INFINITY是Linux下的无穷大标志,windows下没有,自己定义一个
- #define INFINITY 0xFFFFFFFFF
步骤2 编译fconv_var_dim.cpp
在matlab命令行中输入:
- >> mex fconv_var_dim.cpp
- fconv_var_dim.cpp(2) : fatal error C1083: Cannot open include file: 'pthread.h': No such file or directory
- D:\PROGRA~1\MATLAB\R2012B\BIN\MEX.PL: Error: Compile of 'fconv_var_dim.cpp' failed.
- Error using mex (line 206)
- Unable to complete successfully.
将pthread.h的目录添加到环境变量,嫌麻烦的话直接把pthread.h放到VS2010安装目录的include文件夹中。再次编译,提示:
- C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\pthread.h(299) : fatal error C1083: Cannot open include file: 'sched.h': No such file or directory
- D:\PROGRA~1\MATLAB\R2012B\BIN\MEX.PL: Error: Compile of 'fconv_var_dim.cpp' failed.
- Error using mex (line 206)
- Unable to complete successfully.
- \voc-release4.01-star-cascade\star-cascade\fconv_var_dim.cpp(77) : error C4716: 'process' : must return a value
- D:\PROGRA~1\MATLAB\R2012B\BIN\MEX.PL: Error: Compile of 'fconv_var_dim.cpp' failed.
- Error using mex (line 206)
- Unable to complete successfully.
- fconv_var_dim.cpp(128) : error C2664: 'pthread_create' : cannot convert parameter 3 from 'void (__cdecl *)(void *)' to 'void *(__cdecl *)(void *)'
- None of the functions with this name in scope match the target type
- D:\PROGRA~1\MATLAB\R2012B\BIN\MEX.PL: Error: Compile of 'fconv_var_dim.cpp' failed.
- Error using mex (line 206)
- Unable to complete successfully.
- if (pthread_create(&ts[i], NULL, process, (void *)&td[i]))
怎么办呢,到这里我也没辙了。
翻墙去google上查,偶然发现一个大牛的个人主页,他做了star-cascade DPM算法的改进,网页上有源码下载,使用说明里有句话:
The distribution comes with precomputed mex files for 64-bit windows, linux and mac systems.也就是说他为64位的windows、linux和mac系统编译好了文件,那么他的源码会不会是win32上的呢,赶紧下载下来一看,果真,他提供了win32版本的和MacOSX版本的源码,而且也是基于voc-release4进行改进的。所以,我们可以下载 http://vision.mas.ecp.fr/Personnel/iasonas/dpms.html 上的dtbb_1.zip源码,解压后,进入star-cascade目录,可以看到几个cc文件都为64位系统mex编译好了,真是良心啊。
.mexa64是给linux-64编译好的,.mexmaci64是给Mac-64编译好的,.mexw64是为win-x64编译好的。所以,如果你是64位系统,就不用编译了,人家已经给弄好啦。我们编译完成后,会生成一个后缀为.mexw32的文件,这个文件可以看做c++和matlab之间的一个接口,可以在matlab中调用,实现matlab和c++混合编程。
再仔细看看,发现他写了个makefile_windows.m文件,方便windows用户快速编译,其实里面也就两句话:
- mex cascade.cc model.cpp
- mex fconv_var_dim.cpp
- #ifdef MThread
- thread_exit(NULL);
- #else
- eturn NULL;
- #endif
- #ifdef MThread
- (pthread_create(&ts[i], NULL, process, (void *)&td[i]))
- mexErrMsgTxt("Error creating thread");
- #else
- process((void *)&td[i]);
- mxSetCell(plhs[0], i, td[i].mxC);
- #endif
步骤3 运行star-cascade
将matlab的工作目录返回到voc-release4.01,先输入命令:
- addpath star-cascade
将star-cascade加入搜索目录,然后调用cascade_demo,就可以看到检测结果啦。
最后再提一下,这个网站 http://vision.mas.ecp.fr/Personnel/iasonas/dpms.html 上的DPM算法改进应该很不错,以前没接触到,以后要好好看看。最想说的是,此网站提供的源码真心不错。