7 #include "opencv2/core/utility.hpp"
8 #include "opencv2/ocl/ocl.hpp"
9 #include "opencv2/highgui/highgui.hpp"
18 App(CommandLineParser& cmd);
20 void handleKey(char key);
21 void printParams() const;
25 work_begin = getTickCount();
29 int64 d = getTickCount() - work_begin;
30 double f = getTickFrequency();
33 string method_str() const
49 ss << "(" << method_str() << ") FPS: " << setiosflags(ios::left)
50 << setprecision(4) << work_fps;
54 bool running, write_once;
56 Mat left_src, right_src;
58 oclMat d_left, d_right;
61 StereoBeliefPropagation bp;
62 StereoConstantSpaceBP csbp;
69 enum {BM, BP, CSBP} method;
70 int ndisp; // Max disparity + 1
74 int main(int argc, char** argv)
77 "{ h | help | false | print help message }"
78 "{ l | left | | specify left image }"
79 "{ r | right | | specify right image }"
80 "{ m | method | BM | specify match method(BM/BP/CSBP) }"
81 "{ n | ndisp | 64 | specify number of disparity levels }"
82 "{ o | output | stereo_match_output.jpg | specify output path when input is images}";
84 CommandLineParser cmd(argc, argv, keys);
85 if (cmd.get<bool>("help"))
87 cout << "Available options:" << endl;
95 cout << "Device name:" << cv::ocl::Context::getContext()->getDeviceInfo().deviceName << endl;
99 catch (const exception& e)
101 cout << "error: " << e.what() << endl;
107 App::App(CommandLineParser& cmd)
108 : running(false),method(BM)
110 cout << "stereo_match_ocl sample\n";
111 cout << "\nControls:\n"
113 << "\to - save output image once\n"
114 << "\tp - print current parameters\n"
115 << "\tg - convert source images into gray\n"
116 << "\tm - change stereo match method\n"
117 << "\ts - change Sobel prefiltering flag (for BM only)\n"
118 << "\t1/q - increase/decrease maximum disparity\n"
119 << "\t2/w - increase/decrease window size (for BM only)\n"
120 << "\t3/e - increase/decrease iteration count (for BP and CSBP only)\n"
121 << "\t4/r - increase/decrease level count (for BP and CSBP only)\n";
123 l_img = cmd.get<string>("l");
124 r_img = cmd.get<string>("r");
125 string mstr = cmd.get<string>("m");
126 if(mstr == "BM") method = BM;
127 else if(mstr == "BP") method = BP;
128 else if(mstr == "CSBP") method = CSBP;
129 else cout << "unknown method!\n";
130 ndisp = cmd.get<int>("n");
131 out_img = cmd.get<string>("o");
139 left_src = imread(l_img);
140 right_src = imread(r_img);
141 if (left_src.empty()) throw runtime_error("can't open file \"" + l_img + "\"");
142 if (right_src.empty()) throw runtime_error("can't open file \"" + r_img + "\"");
144 cvtColor(left_src, left, COLOR_BGR2GRAY);
145 cvtColor(right_src, right, COLOR_BGR2GRAY);
148 d_right.upload(right);
150 imshow("left", left);
151 imshow("right", right);
153 // Set common parameters
164 // Prepare disparity map of specified type
171 if (d_left.channels() > 1 || d_right.channels() > 1)
173 cout << "BM doesn't support color images\n";
174 cvtColor(left_src, left, COLOR_BGR2GRAY);
175 cvtColor(right_src, right, COLOR_BGR2GRAY);
176 cout << "image_channels: " << left.channels() << endl;
178 d_right.upload(right);
179 imshow("left", left);
180 imshow("right", right);
182 bm(d_left, d_right, d_disp);
185 bp(d_left, d_right, d_disp);
188 csbp(d_left, d_right, d_disp);
193 d_disp.download(disp);
198 disp.convertTo(disp, 0);
200 putText(disp, text(), Point(5, 25), FONT_HERSHEY_SIMPLEX, 1.0, Scalar::all(255));
201 imshow("disparity", disp);
204 imwrite(out_img, disp);
207 handleKey((char)waitKey(3));
212 void App::printParams() const
214 cout << "--- Parameters ---\n";
215 cout << "image_size: (" << left.cols << ", " << left.rows << ")\n";
216 cout << "image_channels: " << left.channels() << endl;
217 cout << "method: " << method_str() << endl
218 << "ndisp: " << ndisp << endl;
222 cout << "win_size: " << bm.winSize << endl;
223 cout << "prefilter_sobel: " << bm.preset << endl;
226 cout << "iter_count: " << bp.iters << endl;
227 cout << "level_count: " << bp.levels << endl;
230 cout << "iter_count: " << csbp.iters << endl;
231 cout << "level_count: " << csbp.levels << endl;
238 void App::handleKey(char key)
251 if (left.channels() == 1 && method != BM)
258 cvtColor(left_src, left, COLOR_BGR2GRAY);
259 cvtColor(right_src, right, COLOR_BGR2GRAY);
262 d_right.upload(right);
263 cout << "image_channels: " << left.channels() << endl;
264 imshow("left", left);
265 imshow("right", right);
281 cout << "method: " << method_str() << endl;
289 case StereoBM_OCL::BASIC_PRESET:
290 bm.preset = StereoBM_OCL::PREFILTER_XSOBEL;
292 case StereoBM_OCL::PREFILTER_XSOBEL:
293 bm.preset = StereoBM_OCL::BASIC_PRESET;
296 cout << "prefilter_sobel: " << bm.preset << endl;
300 ndisp == 1 ? ndisp = 8 : ndisp += 8;
301 cout << "ndisp: " << ndisp << endl;
308 ndisp = max(ndisp - 8, 1);
309 cout << "ndisp: " << ndisp << endl;
317 bm.winSize = min(bm.winSize + 1, 51);
318 cout << "win_size: " << bm.winSize << endl;
325 bm.winSize = max(bm.winSize - 1, 2);
326 cout << "win_size: " << bm.winSize << endl;
333 cout << "iter_count: " << bp.iters << endl;
335 else if (method == CSBP)
338 cout << "iter_count: " << csbp.iters << endl;
345 bp.iters = max(bp.iters - 1, 1);
346 cout << "iter_count: " << bp.iters << endl;
348 else if (method == CSBP)
350 csbp.iters = max(csbp.iters - 1, 1);
351 cout << "iter_count: " << csbp.iters << endl;
358 cout << "level_count: " << bp.levels << endl;
360 else if (method == CSBP)
363 cout << "level_count: " << csbp.levels << endl;
370 bp.levels = max(bp.levels - 1, 1);
371 cout << "level_count: " << bp.levels << endl;
373 else if (method == CSBP)
375 csbp.levels = max(csbp.levels - 1, 1);
376 cout << "level_count: " << csbp.levels << endl;