opencv/samples/ocl/stereo_match.cpp

385 lines
9.7 KiB
C++
Raw Normal View History

2013-05-23 18:10:38 +08:00
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <stdexcept>
Merge remote-tracking branch 'origin/2.4' Pull requests: #943 from jet47:cuda-5.5-support #944 from jet47:cmake-2.8.11-cuda-fix #912 from SpecLad:contributing #934 from SpecLad:parallel-for #931 from jet47:gpu-test-fixes #932 from bitwangyaoyao:2.4_fixBFM #918 from bitwangyaoyao:2.4_samples #924 from pengx17:2.4_arithm_fix #925 from pengx17:2.4_canny_tmp_fix #927 from bitwangyaoyao:2.4_perf #930 from pengx17:2.4_haar_ext #928 from apavlenko:bugfix_3027 #920 from asmorkalov:android_move #910 from pengx17:2.4_oclgfft #913 from janm399:2.4 #916 from bitwangyaoyao:2.4_fixPyrLK #919 from abidrahmank:2.4 #923 from pengx17:2.4_macfix Conflicts: modules/calib3d/src/stereobm.cpp modules/features2d/src/detectors.cpp modules/gpu/src/error.cpp modules/gpu/src/precomp.hpp modules/imgproc/src/distransform.cpp modules/imgproc/src/morph.cpp modules/ocl/include/opencv2/ocl/ocl.hpp modules/ocl/perf/perf_color.cpp modules/ocl/perf/perf_imgproc.cpp modules/ocl/perf/perf_match_template.cpp modules/ocl/perf/precomp.cpp modules/ocl/perf/precomp.hpp modules/ocl/src/arithm.cpp modules/ocl/src/canny.cpp modules/ocl/src/filtering.cpp modules/ocl/src/haar.cpp modules/ocl/src/hog.cpp modules/ocl/src/imgproc.cpp modules/ocl/src/opencl/haarobjectdetect.cl modules/ocl/src/pyrlk.cpp modules/video/src/bgfg_gaussmix2.cpp modules/video/src/lkpyramid.cpp platforms/linux/scripts/cmake_arm_gnueabi_hardfp.sh platforms/linux/scripts/cmake_arm_gnueabi_softfp.sh platforms/scripts/ABI_compat_generator.py samples/ocl/facedetect.cpp
2013-06-04 22:31:51 +08:00
#include "opencv2/core/utility.hpp"
2013-05-23 18:10:38 +08:00
#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) }"
2013-10-22 00:47:55 +08:00
"{ n | ndisp | 64 | specify number of disparity levels }"
2013-06-19 16:36:23 +08:00
"{ o | output | stereo_match_output.jpg | specify output path when input is images}";
2013-10-22 00:47:55 +08:00
2013-06-19 16:36:23 +08:00
CommandLineParser cmd(argc, argv, keys);
if (cmd.get<bool>("help"))
{
2013-10-03 23:35:01 +08:00
cout << "Available options:" << endl;
cmd.printMessage();
2013-06-19 16:36:23 +08:00
return 0;
}
2013-10-22 00:47:55 +08:00
2013-05-23 18:10:38 +08:00
try
{
2013-06-19 16:36:23 +08:00
App app(cmd);
2013-10-03 23:35:01 +08:00
cout << "Device name:" << cv::ocl::Context::getContext()->getDeviceInfo().deviceName << endl;
2013-05-23 18:10:38 +08:00
app.run();
}
catch (const exception& e)
{
cout << "error: " << e.what() << endl;
}
2013-10-22 00:47:55 +08:00
return EXIT_SUCCESS;
2013-05-23 18:10:38 +08:00
}
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";
2013-10-22 00:47:55 +08:00
2013-06-19 16:36:23 +08:00
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
Merge remote-tracking branch 'origin/2.4' Pull requests: #943 from jet47:cuda-5.5-support #944 from jet47:cmake-2.8.11-cuda-fix #912 from SpecLad:contributing #934 from SpecLad:parallel-for #931 from jet47:gpu-test-fixes #932 from bitwangyaoyao:2.4_fixBFM #918 from bitwangyaoyao:2.4_samples #924 from pengx17:2.4_arithm_fix #925 from pengx17:2.4_canny_tmp_fix #927 from bitwangyaoyao:2.4_perf #930 from pengx17:2.4_haar_ext #928 from apavlenko:bugfix_3027 #920 from asmorkalov:android_move #910 from pengx17:2.4_oclgfft #913 from janm399:2.4 #916 from bitwangyaoyao:2.4_fixPyrLK #919 from abidrahmank:2.4 #923 from pengx17:2.4_macfix Conflicts: modules/calib3d/src/stereobm.cpp modules/features2d/src/detectors.cpp modules/gpu/src/error.cpp modules/gpu/src/precomp.hpp modules/imgproc/src/distransform.cpp modules/imgproc/src/morph.cpp modules/ocl/include/opencv2/ocl/ocl.hpp modules/ocl/perf/perf_color.cpp modules/ocl/perf/perf_imgproc.cpp modules/ocl/perf/perf_match_template.cpp modules/ocl/perf/precomp.cpp modules/ocl/perf/precomp.hpp modules/ocl/src/arithm.cpp modules/ocl/src/canny.cpp modules/ocl/src/filtering.cpp modules/ocl/src/haar.cpp modules/ocl/src/hog.cpp modules/ocl/src/imgproc.cpp modules/ocl/src/opencl/haarobjectdetect.cl modules/ocl/src/pyrlk.cpp modules/video/src/bgfg_gaussmix2.cpp modules/video/src/lkpyramid.cpp platforms/linux/scripts/cmake_arm_gnueabi_hardfp.sh platforms/linux/scripts/cmake_arm_gnueabi_softfp.sh platforms/scripts/ABI_compat_generator.py samples/ocl/facedetect.cpp
2013-06-04 22:31:51 +08:00
cvtColor(left_src, left, COLOR_BGR2GRAY);
cvtColor(right_src, right, COLOR_BGR2GRAY);
2013-05-23 18:10:38 +08:00
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";
Merge remote-tracking branch 'origin/2.4' Pull requests: #943 from jet47:cuda-5.5-support #944 from jet47:cmake-2.8.11-cuda-fix #912 from SpecLad:contributing #934 from SpecLad:parallel-for #931 from jet47:gpu-test-fixes #932 from bitwangyaoyao:2.4_fixBFM #918 from bitwangyaoyao:2.4_samples #924 from pengx17:2.4_arithm_fix #925 from pengx17:2.4_canny_tmp_fix #927 from bitwangyaoyao:2.4_perf #930 from pengx17:2.4_haar_ext #928 from apavlenko:bugfix_3027 #920 from asmorkalov:android_move #910 from pengx17:2.4_oclgfft #913 from janm399:2.4 #916 from bitwangyaoyao:2.4_fixPyrLK #919 from abidrahmank:2.4 #923 from pengx17:2.4_macfix Conflicts: modules/calib3d/src/stereobm.cpp modules/features2d/src/detectors.cpp modules/gpu/src/error.cpp modules/gpu/src/precomp.hpp modules/imgproc/src/distransform.cpp modules/imgproc/src/morph.cpp modules/ocl/include/opencv2/ocl/ocl.hpp modules/ocl/perf/perf_color.cpp modules/ocl/perf/perf_imgproc.cpp modules/ocl/perf/perf_match_template.cpp modules/ocl/perf/precomp.cpp modules/ocl/perf/precomp.hpp modules/ocl/src/arithm.cpp modules/ocl/src/canny.cpp modules/ocl/src/filtering.cpp modules/ocl/src/haar.cpp modules/ocl/src/hog.cpp modules/ocl/src/imgproc.cpp modules/ocl/src/opencl/haarobjectdetect.cl modules/ocl/src/pyrlk.cpp modules/video/src/bgfg_gaussmix2.cpp modules/video/src/lkpyramid.cpp platforms/linux/scripts/cmake_arm_gnueabi_hardfp.sh platforms/linux/scripts/cmake_arm_gnueabi_softfp.sh platforms/scripts/ABI_compat_generator.py samples/ocl/facedetect.cpp
2013-06-04 22:31:51 +08:00
cvtColor(left_src, left, COLOR_BGR2GRAY);
cvtColor(right_src, right, COLOR_BGR2GRAY);
2013-05-23 18:10:38 +08:00
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
{
Merge remote-tracking branch 'origin/2.4' Pull requests: #943 from jet47:cuda-5.5-support #944 from jet47:cmake-2.8.11-cuda-fix #912 from SpecLad:contributing #934 from SpecLad:parallel-for #931 from jet47:gpu-test-fixes #932 from bitwangyaoyao:2.4_fixBFM #918 from bitwangyaoyao:2.4_samples #924 from pengx17:2.4_arithm_fix #925 from pengx17:2.4_canny_tmp_fix #927 from bitwangyaoyao:2.4_perf #930 from pengx17:2.4_haar_ext #928 from apavlenko:bugfix_3027 #920 from asmorkalov:android_move #910 from pengx17:2.4_oclgfft #913 from janm399:2.4 #916 from bitwangyaoyao:2.4_fixPyrLK #919 from abidrahmank:2.4 #923 from pengx17:2.4_macfix Conflicts: modules/calib3d/src/stereobm.cpp modules/features2d/src/detectors.cpp modules/gpu/src/error.cpp modules/gpu/src/precomp.hpp modules/imgproc/src/distransform.cpp modules/imgproc/src/morph.cpp modules/ocl/include/opencv2/ocl/ocl.hpp modules/ocl/perf/perf_color.cpp modules/ocl/perf/perf_imgproc.cpp modules/ocl/perf/perf_match_template.cpp modules/ocl/perf/precomp.cpp modules/ocl/perf/precomp.hpp modules/ocl/src/arithm.cpp modules/ocl/src/canny.cpp modules/ocl/src/filtering.cpp modules/ocl/src/haar.cpp modules/ocl/src/hog.cpp modules/ocl/src/imgproc.cpp modules/ocl/src/opencl/haarobjectdetect.cl modules/ocl/src/pyrlk.cpp modules/video/src/bgfg_gaussmix2.cpp modules/video/src/lkpyramid.cpp platforms/linux/scripts/cmake_arm_gnueabi_hardfp.sh platforms/linux/scripts/cmake_arm_gnueabi_softfp.sh platforms/scripts/ABI_compat_generator.py samples/ocl/facedetect.cpp
2013-06-04 22:31:51 +08:00
cvtColor(left_src, left, COLOR_BGR2GRAY);
cvtColor(right_src, right, COLOR_BGR2GRAY);
2013-05-23 18:10:38 +08:00
}
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
}
}