码迷,mamicode.com
首页 > 其他好文 > 详细

立体匹配:关于Middlebury提供的源码的简化使用

时间:2016-03-12 01:24:13      阅读:506      评论:0      收藏:0      [点我收藏+]

标签:

Middlebury提供的源码,虽然花了不到一个小时就运行起来啦。但说实话,它那循环读取脚本命令来执行算法真是让我费了不少头脑,花了近三天时间,我才弄明白了它的运行机制。你说,我就想提取一下算法,你给我整这么多圈子干啥。于是,在我明白了它的运行机制以后,就将无关的代码通通咔嚓了。剩下了最重要的两个类。

以下是我的操作过程。

1.1  

新建解决方案SolutionXYZ、工程ExecutionXYZ及TestData目录。

为工程配置OpenCV库、pnglib库及jpeglib库

对VS2013还需要在C/C++->Preprocessor->Preprocessor Definitions中添加指令_CRT_SECURE_NO_WARNINGS

添加本人的CCC目录到工程内,没有则不用添加。

1.2  

将<MiddEval3-SDK-1.6.zip>中的MiddEval3\code路径下的imageLib目录解压到工程内

将imageLib中Error.h文件中的snprintf改为_snprintf方可

1.3  

将<StereoMatch_1_0.exe>中的StereoMatch目录解压到工程内。

删除StereoMatch目录下的StereoMatch.dsp、StereoMatch.dsw、License.txt、Makefile、README-StereoMatch.txt。

删除StereoMatch目录下的Convert.cpp、Convert.h、Convolve.h、Convolve.cpp、Copyright.h、Error.h、Image.cpp、Image.h、ImageIO.cpp、ImageIO.h、RefCntMem.cpp、RefCntMem.h、main.cpp、StereoIO.h、StereoIO.cpp、Verbose.h、ParameterIO.cpp、ParameterIO.h

1.4  

将<Stereo-patch.zip>内的StcEvaluate.cpp解压到StereoMatch目录内覆盖原有文件

将<LASW.zip>内除Makefile文件外的所有文件解压到StereoMatch目录内覆盖原有文件

将<bp-extensions.tgz>内的StcOptBPSync.cpp, StcOptBP.cpp, node.h, node.cpp, bpregions.h, bpregions.cpp解压到StereoMatch目录内覆盖原有文件

将StereoMatch目录内的所有文件都打开并保存一遍。

在StereoMatcher.h中添加

void OptBP();

void OptBPSync();

在StereoParameters.h的eSymmetric =6,后添加

    eBPSync      = 7,

    eBPAccel     = 8,

在StcOptimize.cpp的opt_fn == eSimulAnnl的添加

||opt_fn == eBPAccel||opt_fn == eBPSync

在StcOptimize.cpp的OptSymmetric();break;的添加

    case eBPAccel:

OptBP();  // run the optimization

break;

    case eBPSync:

OptBPSync();  // run the optimization

break;

1.5  

将所有StereoMatch目录中的所有文件中的"Image.h"改为"./../imageLib/Image.h"

将所有StereoMatch目录中的所有文件中的"Error.h"改为"./../imageLib/Error.h"

将所有StereoMatch目录中的所有文件中的"Convert.h"改为"./../imageLib/Convert.h"

将所有StereoMatch目录中的所有文件中的"ImageIO.h"改为"./../imageLib/ImageIO.h"

将所有StereoMatch目录中的所有文件中的"Convolve.h"改为"./../imageLib/Convolve.h"

将所有StereoMatch目录中的所有文件中的所有#include "ParameterIO.h"及#include "Verbose.h"都删除

将所有类的protected成员改为public成员

暂且注释掉parser_flg.c中的所有代码

由vs2010和2013仍不支持const常量作为数组维数,所以BP算法还没法使用。为编译成功,将bpregions.cpp和node.cpp全部注释,将StcOptBP.cpp和StcOptBPSync.cpp中的相应函数的函数体注释。

1.6  

在StereoParameters.h中添加如下代码:

enum EVerbosityLevel

{

    eVerboseQuiet           = 0,   // no output except for errors

    eVerboseWarning         = 1,   // include warning messages

    eVerboseSummary         = 2,   // print brief summary of algorithm and result

    eVerboseProgress        = 3,   // report progress through steps of algorithm

    eVerboseFileIO          = 4,   // report reading and writing of files

    eVerboseTiming          = 5,   // print timing information

    eVerbosePredictionError = 6,   // print prediction error for all frames

    eVerboseScriptFile      = 10,  // echo commands from script file

    eVerboseInnerLoops      = 15,  // show inner iterations

    eVerboseDumpFiles       = 25,  // dump intermediate results as image files

    eVerboseAllMessages     = 99   // include all information

};

enum EStereoMatchStatus

{                  // Match status computed by symmetric matcher (values in m_status)

    eUnknownMatch = 0,

    eCertainMatch = 1,

    eAmbiguousMatch = 2,

    eOccludedMatch = 3,

};

struct CStereoFrame

{                  // An input image and its associated meta-data (depth map, ground truth, ...):

    CByteImage input_image;             // input image (gray or color)

    CByteImage depth_image;             // depth image (input/output)

    CByteImage truth_image;             // ground truth depth image

    CByteImage resampled_image;         // resampled image (for reprojection error)

 

    float predict_err;                  // prediction error (visible pixels)

    float predict_visible;              // fraction of pixels visible

};

将StereoMatcher.h中的enum EstereoMatchStatus{…}和struct CstereoFrame{…}删除

将StereoParameters.h中代码void PIOInitialize(CParameterIO& prw)删除或注释

将StereoParameters.cpp中代码void PIOInitialize(CParameterIO& prw){…}删除或注释

将StereoParameters.cpp代码都加到StereoParameters.h中并删除StereoParameters.cpp

1.7  

可选操作。

将所有Stc**.cpp中的”./../imageLib/***.h”,并补加这些头文件到”StereoMatcher.h”文件中

将StereoMatcher.cpp中代码全部转移到StereoMatcher.h中后将其删除

对文件编号:

1.1StcPreProcess.cpp

1.2StcRawCosts.cpp

1.3StcAggregate.cpp

1.4StcOptimize.cpp

1.5StcRefine.cpp

1.6StcEvaluate.cpp

2.1StcDiffusion.cpp

2.2LASW.cpp

2.2LASW.h

3.1StcOptDP.cpp

3.2StcOptSO.cpp

3.3StcSimulAnn.cpp

3.4StcGraphCut.cpp

编译出错时再对头文件名进行更改。

1.8  

在TestData目录内新建目录:map, sawtooth, venus。然后将< imagedirs.zip>中对应目录内的imx.pgm, imy.pgm, dispx.pgm, dispy.pgm文件复制过来,并都统一命名为:im0.pgm, im1.pgm, disp0.pgm, disp1.pgm

1.9  

接下就是实现<Scripts.zip>、<LASWscripts.zip>及<bp-extensions.zip>中的exp7_bps.txt和exp8_bpa.txt的功能。

添加scripts.h和main.cpp

  1 #include "CCC/COMCV.h"
  2 #include "StereoMatch/StereoMatcher.h"
  3 #include <fstream>
  4 #include <iomanip>
  5 
  6 //输出参数
  7 void writeParams(CStereoMatcher &s, string &path)
  8 {
  9     ofstream out(path);
 10     out << "preproc_addnoise_stddev " << s.preproc_addnoise_stddev;//0.000000
 11     out << endl << "preproc_blur_iter "<< s.preproc_blur_iter;//0
 12     out << endl << "frame_ref " << s.frame_ref;//0
 13     out << endl << "frame_match " << s.frame_match;//1
 14     out << endl << "disp_min " << s.disp_min;//0
 15     out << endl << "disp_max " << s.disp_max;//29
 16     out << endl << "disp_step " << s.disp_step;//1.000000
 17     out << endl << "disp_n " << s.disp_n;//30
 18     out << endl << "disp_scale " << s.disp_scale;//8.000000
 19     out << endl << "match_fn " << s.match_fn;//1
 20     out << endl << "match_interp " << s.match_interp;//3
 21     out << endl << "match_max " << s.match_max;//1000
 22     out << endl << "match_interval " << s.match_interval;//0
 23     out << endl << "match_interpolated " << s.match_interpolated;//0
 24     out << endl << "aggr_fn " << s.aggr_fn;//1
 25     out << endl << "aggr_window_size " << s.aggr_window_size;//9
 26     out << endl << "aggr_iter " << s.aggr_iter;//1
 27     out << endl << "aggr_minfilter " << s.aggr_minfilter;//0
 28     out << endl << "aggr_subpixel " << s.aggr_subpixel;//0
 29     out << endl << "aggr_collapse " << s.aggr_collapse;//0
 30     out << endl << "diff_lambda " << s.diff_lambda;//0.150000
 31     out << endl << "diff_beta " << s.diff_beta;//0.500000
 32     out << endl << "diff_scale_cost " << s.diff_scale_cost;//0.010000
 33     out << endl << "diff_mu " << s.diff_mu;//0.500000
 34     out << endl << "diff_sigmaP " << s.diff_sigmaP;//0.400000
 35     out << endl << "diff_epsP " << s.diff_epsP;//0.010000
 36     out << endl << "opt_fn " << s.opt_fn;//1
 37     out << endl << "opt_smoothness " << s.opt_smoothness;//1.000000
 38     out << endl << "opt_grad_thresh " << s.opt_grad_thresh;//5.000000
 39     out << endl << "opt_grad_penalty " << s.opt_grad_penalty;//1.000000
 40     out << endl << "opt_occlusion_cost " << s.opt_occlusion_cost;//20
 41     out << endl << "opt_max_iter " << s.opt_max_iter;//100
 42     out << endl << "opt_random " << s.opt_random;//1
 43     out << endl << "opt_sa_var " << s.opt_sa_var;//3
 44     out << endl << "opt_sa_start_T " << s.opt_sa_start_T;//10.000000
 45     out << endl << "opt_sa_end_T " << s.opt_sa_end_T;//0.010000
 46     out << endl << "opt_sa_schedule " << s.opt_sa_schedule;//1
 47     out << endl << "opt_min_margin " << s.opt_min_margin;//0.700000
 48     out << endl << "opt_sym_passes " << s.opt_sym_passes;//1
 49     out << endl << "refine_subpix " << s.refine_subpix;//0
 50     out << endl << "eval_ignore_border " << s.eval_ignore_border;//10
 51     out << endl << "eval_bad_thresh " << s.eval_bad_thresh;//1.000000
 52     out << endl << "eval_error_scale " << s.eval_error_scale;//0.000000
 53     out << endl << "eval_lin_interp " << s.eval_lin_interp;//1
 54     out << endl << "eval_disp_gap " << s.eval_disp_gap;//2.000000
 55     out << endl << "eval_predict_type " << s.eval_predict_type;//0
 56     out << endl << "eval_textureless_width " << s.eval_textureless_width;//3
 57     out << endl << "eval_textureless_thresh " << s.eval_textureless_thresh;//4.000000
 58     out << endl << "eval_discont_width " << s.eval_discont_width;//9
 59     out << endl << "eval_predict_diff " << s.eval_predict_diff;//0
 60     out << endl << "eval_empty_color " << setbase(16)<< s.eval_empty_color;//0x00ffc0ff
 61     out << endl << "eval_partial_shuffle " << s.eval_partial_shuffle;//0.000000
 62     out << endl << "eval_match_quality " << s.eval_match_quality;//0
 63     out << endl << "eval_certain_matches_only " << s.eval_certain_matches_only;//0
 64     out << endl << "rms_error_all" << s.rms_error_all;
 65     out << endl << "rms_error_nonocc " << s.rms_error_nonocc;//1.898849
 66     out << endl << "rms_error_occ " << s.rms_error_occ;//17.536930
 67     out << endl << "rms_error_textured " << s.rms_error_textured;//1.895457
 68     out << endl << "rms_error_textureless " << s.rms_error_textureless;//2.254724
 69     out << endl << "rms_error_discont " << s.rms_error_discont;//6.221545
 70     out << endl << "bad_pixels_all " << s.bad_pixels_all;//0.074057
 71     out << endl << "bad_pixels_nonocc " << s.bad_pixels_nonocc;//0.017140
 72     out << endl << "bad_pixels_occ " << s.bad_pixels_occ;//0.971484
 73     out << endl << "bad_pixels_textured " << s.bad_pixels_textured;//0.016978
 74     out << endl << "bad_pixels_textureless " << s.bad_pixels_textureless;//0.035714
 75     out << endl << "bad_pixels_discont " << s.bad_pixels_discont;//0.114071
 76     out << endl << "fraction_matched " << s.fraction_matched;//0.100000
 77     out << endl << "predict_err_near " << s.predict_err_near;//-;//1.000000
 78     out << endl << "predict_err_middle " << s.predict_err_middle;//-;//1.000000
 79     out << endl << "predict_err_match " << s.predict_err_match;//-;//1.000000
 80     out << endl << "predict_err_far " << s.predict_err_far;//-;//1.000000
 81     out << endl << "final_energy " << s.final_energy;//611911.437500
 82     out << endl << "total_time " << s.total_time;//1.681000
 83     out << endl << "verbose " << s.verbose;//2
 84     out << endl << "evaluate_only " << s.evaluate_only;//0
 85     out.close();
 86 }
 87 
 88 //相同参数
 89 void comParam(CStereoMatcher &s)
 90 {
 91     s.verbose = eVerboseSummary;
 92     s.evaluate_only = s.evaluate_only;
 93     s.frame_ref = 0;
 94     s.frame_match = 1;
 95     s.eval_predict_type = ePredictNone;
 96     s.eval_error_scale = 0;
 97     s.disp_min = 0;
 98     s.disp_scale = 8;
 99     s.eval_disp_gap = 2;
100     s.eval_ignore_border = 10;
101 }
102 
103 //与目录相关的参数
104 void dirParam(CStereoMatcher &s, string &dir)
105 {
106     string im0path, im1path, trupath;
107     cout << endl << dir.substr(dir.find_last_of("/") + 1);
108     if (dir.substr(dir.find_last_of("/") + 1) == "map")
109     {
110         s.disp_max = 29;
111         im0path = dir + "/im0.pgm";
112         im1path = dir + "/im1.pgm";
113         trupath = dir + "/disp0.pgm";
114     }
115     else if (dir.substr(dir.find_last_of("/") + 1) == "sawtooth")
116     {
117         s.disp_max = 19;
118         im0path = dir + "/im0.ppm";
119         im1path = dir + "/im1.ppm";
120         trupath = dir + "/disp0.pgm";
121     }
122     else if (dir.substr(dir.find_last_of("/") + 1) == "venus")
123     {
124         s.disp_max = 19;
125         im0path = dir + "/im0.ppm";
126         im1path = dir + "/im1.ppm";
127         trupath = dir + "/disp0.pgm";
128     }
129     CStereoFrame fr0, fr1;
130     ReadImage(fr0.input_image, im0path.c_str());
131     ReadImage(fr1.input_image, im1path.c_str());
132     ReadImage(fr0.truth_image, trupath.c_str());
133     s.m_frame.push_back(fr0);
134     s.m_frame.push_back(fr1);
135 }
136 
137 //测试方案一
138 void costSSD9(CStereoMatcher &s, string &dir)
139 {
140     //0.此函数内的公共设置
141     s.aggr_window_size = 9;
142     s.aggr_minfilter = 0;
143     int match_max[7] = { 1000, 50, 20, 10, 5, 2, 1 };
144 
145     //1.测试一
146     s.match_fn = eAD;
147     s.match_interval = 0;
148     string out1[7] = { dir + "/SAD09.pgm", dir + "/SAD09t50.pgm", dir + "/SAD09t20.pgm", dir + "/SAD09t10.pgm", dir + "/SAD09t05.pgm", dir + "/SAD09t02.pgm", dir + "/SAD09t01.pgm" };
149     for (int i = 0; i < 7; i++)
150     {
151         s.match_max = match_max[i];
152         s.ComputeCorrespondence(); s.Evaluate();
153         WriteImage(s.m_frame[0].depth_image, out1[i].c_str());
154         writeParams(s,out1[i].substr(0, out1[i].find_last_of(".")) + ".txt");
155         int x = 0;
156     }
157     //2.测试二
158     s.match_fn = eSD;
159     s.match_interval = 0;
160     string out2[7] = { dir + "/SSD09.pgm", dir + "/SSD09t50.pgm", dir + "/SSD09t20.pgm", dir + "/SSD09t10.pgm", dir + "/SSD09t05.pgm", dir + "/SSD09t02.pgm", dir + "/SSD09t01.pgm" };
161     for (int i = 0; i < 7; i++)
162     {
163         s.match_max = match_max[i];
164         s.ComputeCorrespondence(); s.Evaluate();
165         WriteImage(s.m_frame[0].depth_image, out2[i].c_str());
166         writeParams(s, out2[i].substr(0, out2[i].find_last_of(".")) + ".txt");
167     }
168     //3.测试三
169     s.match_fn = eAD;
170     s.match_interval = 1;
171     string out3[7] = { dir + "/SAD09b.pgm", dir + "/SAD09bt50.pgm", dir + "/SAD09bt20.pgm", dir + "/SAD09bt10.pgm", dir + "/SAD09bt05.pgm", dir + "/SAD09bt02.pgm", dir + "/SAD09bt01.pgm" };
172     for (int i = 0; i < 7; i++)
173     {
174         s.match_max = match_max[i];
175         s.ComputeCorrespondence(); s.Evaluate();
176         WriteImage(s.m_frame[0].depth_image, out3[i].c_str());
177         writeParams(s, out3[i].substr(0, out3[i].find_last_of(".")) + ".txt");
178     }
179     //4.测试四
180     s.match_fn = eSD;//1
181     s.match_interval = 1;
182     string out4[7] = { dir + "/SSD09b.pgm", dir + "/SSD09bt50.pgm", dir + "/SSD09bt20.pgm", dir + "/SSD09bt10.pgm", dir + "/SSD09bt05.pgm", dir + "/SSD09bt02.pgm", dir + "/SSD09bt01.pgm" };
183     for (int i = 0; i < 7; i++)
184     {
185         s.match_max = match_max[i];
186         s.ComputeCorrespondence(); s.Evaluate();
187         WriteImage(s.m_frame[0].depth_image, out4[i].c_str());
188         writeParams(s, out4[i].substr(0, out4[i].find_last_of(".")) + ".txt");
189     }
190 }
191 
192 //测试方案二
193 void costSSDMF9(CStereoMatcher &s, string &dir)
194 {
195     //0.此函数内的公共设置
196     s.aggr_window_size = 9;
197     s.aggr_minfilter = 9;
198     int match_max[7] = { 1000, 50, 20, 10, 5, 2, 1 };
199 
200     //1.测试一
201     s.match_fn = eAD;
202     s.match_interval = 0;
203     string out1[7] = { dir + "/SADmf09.pgm", dir + "/SADmf09t50.pgm", dir + "/SADmf09t20.pgm", dir + "/SADmf09t10.pgm", dir + "/SADmf09t05.pgm", dir + "/SADmf09t02.pgm", dir + "/SADmf09t01.pgm" };
204     for (int i = 0; i < 7; i++)
205     {
206         s.match_max = match_max[i];
207         s.ComputeCorrespondence(); s.Evaluate();
208         WriteImage(s.m_frame[0].depth_image, out1[i].c_str());
209         writeParams(s, out1[i].substr(0, out1[i].find_last_of(".")) + ".txt");
210         int x = 0;
211     }
212     //2.测试二
213     s.match_fn = eSD;
214     s.match_interval = 0;
215     string out2[7] = { dir + "/SSDmf09.pgm", dir + "/SSDmf09t50.pgm", dir + "/SSDmf09t20.pgm", dir + "/SSDmf09t10.pgm", dir + "/SSDmf09t05.pgm", dir + "/SSDmf09t02.pgm", dir + "/SSDmf09t01.pgm" };
216     for (int i = 0; i < 7; i++)
217     {
218         s.match_max = match_max[i];
219         s.ComputeCorrespondence(); s.Evaluate();
220         WriteImage(s.m_frame[0].depth_image, out2[i].c_str());
221         writeParams(s, out2[i].substr(0, out2[i].find_last_of(".")) + ".txt");
222     }
223     //3.测试三
224     s.match_fn = eAD;
225     s.match_interval = 1;
226     string out3[7] = { dir + "/SADmf09b.pgm", dir + "/SADmf09bt50.pgm", dir + "/SADmf09bt20.pgm", dir + "/SADmf09bt10.pgm", dir + "/SADmf09bt05.pgm", dir + "/SADmf09bt02.pgm", dir + "/SADmf09bt01.pgm" };
227     for (int i = 0; i < 7; i++)
228     {
229         s.match_max = match_max[i];
230         s.ComputeCorrespondence(); s.Evaluate();
231         WriteImage(s.m_frame[0].depth_image, out3[i].c_str());
232         writeParams(s, out3[i].substr(0, out3[i].find_last_of(".")) + ".txt");
233     }
234     //4.测试四
235     s.match_fn = eSD;//1
236     s.match_interval = 1;
237     string out4[7] = { dir + "/SSDmf09b.pgm", dir + "/SSDmf09bt50.pgm", dir + "/SSDmf09bt20.pgm", dir + "/SSDmf09bt10.pgm", dir + "/SSDmf09bt05.pgm", dir + "/SSDmf09bt02.pgm", dir + "/SSDmf09bt01.pgm" };
238     for (int i = 0; i < 7; i++)
239     {
240         s.match_max = match_max[i];
241         s.ComputeCorrespondence(); s.Evaluate();
242         WriteImage(s.m_frame[0].depth_image, out4[i].c_str());
243         writeParams(s, out4[i].substr(0, out4[i].find_last_of(".")) + ".txt");
244     }
245 }
246 
247 //测试方案三
248 
249 
250 //执行
251 void execute()
252 {
253     vector<string> dirs = cv_GetListFolders("./../TestData", "*", true);
254     for (int i = 0; i < dirs.size(); i++)
255     {
256         CStereoMatcher s;
257         comParam(s);
258         dirParam(s, dirs[i]);
259         costSSD9(s, dirs[i]);
260         //costSSDMF9(s, dirs[i]);
261     }
262 }

 

立体匹配:关于Middlebury提供的源码的简化使用

标签:

原文地址:http://www.cnblogs.com/dzyBK/p/5267747.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!