opencv/samples/ocl/stereo_match.cpp

389 lines
9.9 KiB
C++
Raw Normal View History

2013-05-23 18:10:38 +08:00
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <stdexcept>
#include "opencv2/ocl/ocl.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
using namespace std;
using namespace ocl;
struct App
{
2013-06-19 16:36:23 +08:00
App(CommandLineParser& cmd);
2013-05-23 18:10:38 +08:00
void run();
void handleKey(char key);
void printParams() const;
2013-06-19 16:36:23 +08:00
void workBegin()
{
work_begin = getTickCount();
}
2013-05-23 18:10:38 +08:00
void workEnd()
{
int64 d = getTickCount() - work_begin;
double f = getTickFrequency();
work_fps = f / d;
}
2013-06-19 16:36:23 +08:00
string method_str() const
{
switch (method)
{
case BM:
return "BM";
case BP:
return "BP";
case CSBP:
return "CSBP";
}
return "";
}
2013-05-23 18:10:38 +08:00
string text() const
{
stringstream ss;
2013-06-19 16:36:23 +08:00
ss << "(" << method_str() << ") FPS: " << setiosflags(ios::left)
<< setprecision(4) << work_fps;
2013-05-23 18:10:38 +08:00
return ss.str();
}
private:
2013-06-28 15:08:39 +08:00
bool running, write_once;
2013-05-23 18:10:38 +08:00
Mat left_src, right_src;
Mat left, right;
oclMat d_left, d_right;
StereoBM_OCL bm;
StereoBeliefPropagation bp;
StereoConstantSpaceBP csbp;
int64 work_begin;
double work_fps;
2013-06-19 16:36:23 +08:00
string l_img, r_img;
string out_img;
enum {BM, BP, CSBP} method;
int ndisp; // Max disparity + 1
enum {GPU, CPU} type;
};
2013-05-23 18:10:38 +08:00
int main(int argc, char** argv)
{
2013-06-19 16:36:23 +08:00
const char* keys =
"{ h | help | false | print help message }"
"{ l | left | | specify left image }"
"{ r | right | | specify right image }"
"{ m | method | BM | specify match method(BM/BP/CSBP) }"
"{ n | ndisp | 64 | specify number of disparity levels }"
"{ s | cpu_ocl | false | use cpu or gpu as ocl device to process the image }"
"{ o | output | stereo_match_output.jpg | specify output path when input is images}";
CommandLineParser cmd(argc, argv, keys);
if (cmd.get<bool>("help"))
{
cout << "Avaible options:" << endl;
cmd.printParams();
return 0;
}
2013-05-23 18:10:38 +08:00
try
{
2013-06-19 16:36:23 +08:00
App app(cmd);
int flag = CVCL_DEVICE_TYPE_GPU;
if(cmd.get<bool>("s") == true)
flag = CVCL_DEVICE_TYPE_CPU;
2013-05-23 18:10:38 +08:00
vector<Info> info;
2013-06-19 16:36:23 +08:00
if(getDevice(info, flag) == 0)
2013-05-23 18:10:38 +08:00
{
throw runtime_error("Error: Did not find a valid OpenCL device!");
}
cout << "Device name:" << info[0].DeviceName[0] << endl;
app.run();
}
catch (const exception& e)
{
cout << "error: " << e.what() << endl;
}
return 0;
}
2013-06-19 16:36:23 +08:00
App::App(CommandLineParser& cmd)
: running(false),method(BM)
2013-05-23 18:10:38 +08:00
{
cout << "stereo_match_ocl sample\n";
cout << "\nControls:\n"
2013-06-19 16:36:23 +08:00
<< "\tesc - exit\n"
2013-06-28 15:08:39 +08:00
<< "\to - save output image once\n"
2013-06-19 16:36:23 +08:00
<< "\tp - print current parameters\n"
<< "\tg - convert source images into gray\n"
<< "\tm - change stereo match method\n"
<< "\ts - change Sobel prefiltering flag (for BM only)\n"
<< "\t1/q - increase/decrease maximum disparity\n"
<< "\t2/w - increase/decrease window size (for BM only)\n"
<< "\t3/e - increase/decrease iteration count (for BP and CSBP only)\n"
<< "\t4/r - increase/decrease level count (for BP and CSBP only)\n";
l_img = cmd.get<string>("l");
r_img = cmd.get<string>("r");
string mstr = cmd.get<string>("m");
if(mstr == "BM") method = BM;
else if(mstr == "BP") method = BP;
else if(mstr == "CSBP") method = CSBP;
else cout << "unknown method!\n";
ndisp = cmd.get<int>("n");
out_img = cmd.get<string>("o");
2013-06-28 15:08:39 +08:00
write_once = false;
2013-05-23 18:10:38 +08:00
}
void App::run()
{
// Load images
2013-06-19 16:36:23 +08:00
left_src = imread(l_img);
right_src = imread(r_img);
if (left_src.empty()) throw runtime_error("can't open file \"" + l_img + "\"");
if (right_src.empty()) throw runtime_error("can't open file \"" + r_img + "\"");
2013-05-23 18:10:38 +08:00
cvtColor(left_src, left, CV_BGR2GRAY);
cvtColor(right_src, right, CV_BGR2GRAY);
d_left.upload(left);
d_right.upload(right);
imshow("left", left);
imshow("right", right);
// Set common parameters
2013-06-19 16:36:23 +08:00
bm.ndisp = ndisp;
bp.ndisp = ndisp;
csbp.ndisp = ndisp;
2013-05-23 18:10:38 +08:00
cout << endl;
printParams();
running = true;
while (running)
{
// Prepare disparity map of specified type
Mat disp;
oclMat d_disp;
workBegin();
2013-06-19 16:36:23 +08:00
switch (method)
2013-05-23 18:10:38 +08:00
{
2013-06-19 16:36:23 +08:00
case BM:
2013-05-23 18:10:38 +08:00
if (d_left.channels() > 1 || d_right.channels() > 1)
{
cout << "BM doesn't support color images\n";
cvtColor(left_src, left, CV_BGR2GRAY);
cvtColor(right_src, right, CV_BGR2GRAY);
cout << "image_channels: " << left.channels() << endl;
d_left.upload(left);
d_right.upload(right);
imshow("left", left);
imshow("right", right);
}
bm(d_left, d_right, d_disp);
break;
2013-06-19 16:36:23 +08:00
case BP:
2013-05-23 18:10:38 +08:00
bp(d_left, d_right, d_disp);
break;
2013-06-19 16:36:23 +08:00
case CSBP:
2013-05-23 18:10:38 +08:00
csbp(d_left, d_right, d_disp);
break;
}
2013-06-28 15:08:39 +08:00
2013-05-23 18:10:38 +08:00
// Show results
d_disp.download(disp);
2013-06-21 14:50:08 +08:00
workEnd();
2013-06-28 15:08:39 +08:00
2013-06-19 16:36:23 +08:00
if (method != BM)
2013-05-23 18:10:38 +08:00
{
disp.convertTo(disp, 0);
}
putText(disp, text(), Point(5, 25), FONT_HERSHEY_SIMPLEX, 1.0, Scalar::all(255));
imshow("disparity", disp);
2013-06-28 15:08:39 +08:00
if(write_once)
2013-06-19 16:36:23 +08:00
{
imwrite(out_img, disp);
2013-06-28 15:08:39 +08:00
write_once = false;
2013-06-19 16:36:23 +08:00
}
2013-05-23 18:10:38 +08:00
handleKey((char)waitKey(3));
}
}
void App::printParams() const
{
cout << "--- Parameters ---\n";
cout << "image_size: (" << left.cols << ", " << left.rows << ")\n";
cout << "image_channels: " << left.channels() << endl;
2013-06-19 16:36:23 +08:00
cout << "method: " << method_str() << endl
<< "ndisp: " << ndisp << endl;
switch (method)
2013-05-23 18:10:38 +08:00
{
2013-06-19 16:36:23 +08:00
case BM:
2013-05-23 18:10:38 +08:00
cout << "win_size: " << bm.winSize << endl;
cout << "prefilter_sobel: " << bm.preset << endl;
break;
2013-06-19 16:36:23 +08:00
case BP:
2013-05-23 18:10:38 +08:00
cout << "iter_count: " << bp.iters << endl;
cout << "level_count: " << bp.levels << endl;
break;
2013-06-19 16:36:23 +08:00
case CSBP:
2013-05-23 18:10:38 +08:00
cout << "iter_count: " << csbp.iters << endl;
cout << "level_count: " << csbp.levels << endl;
break;
}
cout << endl;
}
void App::handleKey(char key)
{
switch (key)
{
case 27:
running = false;
break;
2013-06-19 16:36:23 +08:00
case 'p':
case 'P':
2013-05-23 18:10:38 +08:00
printParams();
break;
2013-06-19 16:36:23 +08:00
case 'g':
case 'G':
if (left.channels() == 1 && method != BM)
2013-05-23 18:10:38 +08:00
{
left = left_src;
right = right_src;
}
else
{
cvtColor(left_src, left, CV_BGR2GRAY);
cvtColor(right_src, right, CV_BGR2GRAY);
}
d_left.upload(left);
d_right.upload(right);
cout << "image_channels: " << left.channels() << endl;
imshow("left", left);
imshow("right", right);
break;
2013-06-19 16:36:23 +08:00
case 'm':
case 'M':
switch (method)
2013-05-23 18:10:38 +08:00
{
2013-06-19 16:36:23 +08:00
case BM:
method = BP;
2013-05-23 18:10:38 +08:00
break;
2013-06-19 16:36:23 +08:00
case BP:
method = CSBP;
2013-05-23 18:10:38 +08:00
break;
2013-06-19 16:36:23 +08:00
case CSBP:
method = BM;
2013-05-23 18:10:38 +08:00
break;
}
2013-06-19 16:36:23 +08:00
cout << "method: " << method_str() << endl;
2013-05-23 18:10:38 +08:00
break;
2013-06-19 16:36:23 +08:00
case 's':
case 'S':
if (method == BM)
2013-05-23 18:10:38 +08:00
{
switch (bm.preset)
{
case StereoBM_OCL::BASIC_PRESET:
bm.preset = StereoBM_OCL::PREFILTER_XSOBEL;
break;
case StereoBM_OCL::PREFILTER_XSOBEL:
bm.preset = StereoBM_OCL::BASIC_PRESET;
break;
}
cout << "prefilter_sobel: " << bm.preset << endl;
}
break;
case '1':
2013-06-19 16:36:23 +08:00
ndisp == 1 ? ndisp = 8 : ndisp += 8;
cout << "ndisp: " << ndisp << endl;
bm.ndisp = ndisp;
bp.ndisp = ndisp;
csbp.ndisp = ndisp;
2013-05-23 18:10:38 +08:00
break;
2013-06-19 16:36:23 +08:00
case 'q':
case 'Q':
ndisp = max(ndisp - 8, 1);
cout << "ndisp: " << ndisp << endl;
bm.ndisp = ndisp;
bp.ndisp = ndisp;
csbp.ndisp = ndisp;
2013-05-23 18:10:38 +08:00
break;
case '2':
2013-06-19 16:36:23 +08:00
if (method == BM)
2013-05-23 18:10:38 +08:00
{
bm.winSize = min(bm.winSize + 1, 51);
cout << "win_size: " << bm.winSize << endl;
}
break;
2013-06-19 16:36:23 +08:00
case 'w':
case 'W':
if (method == BM)
2013-05-23 18:10:38 +08:00
{
bm.winSize = max(bm.winSize - 1, 2);
cout << "win_size: " << bm.winSize << endl;
}
break;
case '3':
2013-06-19 16:36:23 +08:00
if (method == BP)
2013-05-23 18:10:38 +08:00
{
bp.iters += 1;
cout << "iter_count: " << bp.iters << endl;
}
2013-06-19 16:36:23 +08:00
else if (method == CSBP)
2013-05-23 18:10:38 +08:00
{
csbp.iters += 1;
cout << "iter_count: " << csbp.iters << endl;
}
break;
2013-06-19 16:36:23 +08:00
case 'e':
case 'E':
if (method == BP)
2013-05-23 18:10:38 +08:00
{
bp.iters = max(bp.iters - 1, 1);
cout << "iter_count: " << bp.iters << endl;
}
2013-06-19 16:36:23 +08:00
else if (method == CSBP)
2013-05-23 18:10:38 +08:00
{
csbp.iters = max(csbp.iters - 1, 1);
cout << "iter_count: " << csbp.iters << endl;
}
break;
case '4':
2013-06-19 16:36:23 +08:00
if (method == BP)
2013-05-23 18:10:38 +08:00
{
bp.levels += 1;
cout << "level_count: " << bp.levels << endl;
}
2013-06-19 16:36:23 +08:00
else if (method == CSBP)
2013-05-23 18:10:38 +08:00
{
csbp.levels += 1;
cout << "level_count: " << csbp.levels << endl;
}
break;
2013-06-19 16:36:23 +08:00
case 'r':
case 'R':
if (method == BP)
2013-05-23 18:10:38 +08:00
{
bp.levels = max(bp.levels - 1, 1);
cout << "level_count: " << bp.levels << endl;
}
2013-06-19 16:36:23 +08:00
else if (method == CSBP)
2013-05-23 18:10:38 +08:00
{
csbp.levels = max(csbp.levels - 1, 1);
cout << "level_count: " << csbp.levels << endl;
}
break;
2013-06-28 15:08:39 +08:00
case 'o':
case 'O':
write_once = true;
break;
2013-05-23 18:10:38 +08:00
}
}