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}";
83 CommandLineParser cmd(argc, argv, keys);
84 if (cmd.get<bool>("help"))
86 cout << "Available options:" << endl;
93 cout << "Device name:" << cv::ocl::Context::getContext()->getDeviceInfo().deviceName << endl;
97 catch (const exception& e)
99 cout << "error: " << e.what() << endl;
104 App::App(CommandLineParser& cmd)
105 : running(false),method(BM)
107 cout << "stereo_match_ocl sample\n";
108 cout << "\nControls:\n"
110 << "\to - save output image once\n"
111 << "\tp - print current parameters\n"
112 << "\tg - convert source images into gray\n"
113 << "\tm - change stereo match method\n"
114 << "\ts - change Sobel prefiltering flag (for BM only)\n"
115 << "\t1/q - increase/decrease maximum disparity\n"
116 << "\t2/w - increase/decrease window size (for BM only)\n"
117 << "\t3/e - increase/decrease iteration count (for BP and CSBP only)\n"
118 << "\t4/r - increase/decrease level count (for BP and CSBP only)\n";
119 l_img = cmd.get<string>("l");
120 r_img = cmd.get<string>("r");
121 string mstr = cmd.get<string>("m");
122 if(mstr == "BM") method = BM;
123 else if(mstr == "BP") method = BP;
124 else if(mstr == "CSBP") method = CSBP;
125 else cout << "unknown method!\n";
126 ndisp = cmd.get<int>("n");
127 out_img = cmd.get<string>("o");
135 left_src = imread(l_img);
136 right_src = imread(r_img);
137 if (left_src.empty()) throw runtime_error("can't open file \"" + l_img + "\"");
138 if (right_src.empty()) throw runtime_error("can't open file \"" + r_img + "\"");
140 cvtColor(left_src, left, COLOR_BGR2GRAY);
141 cvtColor(right_src, right, COLOR_BGR2GRAY);
144 d_right.upload(right);
146 imshow("left", left);
147 imshow("right", right);
149 // Set common parameters
160 // Prepare disparity map of specified type
167 if (d_left.channels() > 1 || d_right.channels() > 1)
169 cout << "BM doesn't support color images\n";
170 cvtColor(left_src, left, COLOR_BGR2GRAY);
171 cvtColor(right_src, right, COLOR_BGR2GRAY);
172 cout << "image_channels: " << left.channels() << endl;
174 d_right.upload(right);
175 imshow("left", left);
176 imshow("right", right);
178 bm(d_left, d_right, d_disp);
181 bp(d_left, d_right, d_disp);
184 csbp(d_left, d_right, d_disp);
189 d_disp.download(disp);
194 disp.convertTo(disp, 0);
196 putText(disp, text(), Point(5, 25), FONT_HERSHEY_SIMPLEX, 1.0, Scalar::all(255));
197 imshow("disparity", disp);
200 imwrite(out_img, disp);
203 handleKey((char)waitKey(3));
208 void App::printParams() const
210 cout << "--- Parameters ---\n";
211 cout << "image_size: (" << left.cols << ", " << left.rows << ")\n";
212 cout << "image_channels: " << left.channels() << endl;
213 cout << "method: " << method_str() << endl
214 << "ndisp: " << ndisp << endl;
218 cout << "win_size: " << bm.winSize << endl;
219 cout << "prefilter_sobel: " << bm.preset << endl;
222 cout << "iter_count: " << bp.iters << endl;
223 cout << "level_count: " << bp.levels << endl;
226 cout << "iter_count: " << csbp.iters << endl;
227 cout << "level_count: " << csbp.levels << endl;
234 void App::handleKey(char key)
247 if (left.channels() == 1 && method != BM)
254 cvtColor(left_src, left, COLOR_BGR2GRAY);
255 cvtColor(right_src, right, COLOR_BGR2GRAY);
258 d_right.upload(right);
259 cout << "image_channels: " << left.channels() << endl;
260 imshow("left", left);
261 imshow("right", right);
277 cout << "method: " << method_str() << endl;
285 case StereoBM_OCL::BASIC_PRESET:
286 bm.preset = StereoBM_OCL::PREFILTER_XSOBEL;
288 case StereoBM_OCL::PREFILTER_XSOBEL:
289 bm.preset = StereoBM_OCL::BASIC_PRESET;
292 cout << "prefilter_sobel: " << bm.preset << endl;
296 ndisp == 1 ? ndisp = 8 : ndisp += 8;
297 cout << "ndisp: " << ndisp << endl;
304 ndisp = max(ndisp - 8, 1);
305 cout << "ndisp: " << ndisp << endl;
313 bm.winSize = min(bm.winSize + 1, 51);
314 cout << "win_size: " << bm.winSize << endl;
321 bm.winSize = max(bm.winSize - 1, 2);
322 cout << "win_size: " << bm.winSize << endl;
329 cout << "iter_count: " << bp.iters << endl;
331 else if (method == CSBP)
334 cout << "iter_count: " << csbp.iters << endl;
341 bp.iters = max(bp.iters - 1, 1);
342 cout << "iter_count: " << bp.iters << endl;
344 else if (method == CSBP)
346 csbp.iters = max(csbp.iters - 1, 1);
347 cout << "iter_count: " << csbp.iters << endl;
354 cout << "level_count: " << bp.levels << endl;
356 else if (method == CSBP)
359 cout << "level_count: " << csbp.levels << endl;
366 bp.levels = max(bp.levels - 1, 1);
367 cout << "level_count: " << bp.levels << endl;
369 else if (method == CSBP)
371 csbp.levels = max(csbp.levels - 1, 1);
372 cout << "level_count: " << csbp.levels << endl;