Skip to content

Commit 14a9103

Browse files
committed
Merge pull request opencv#18129 from pemmanuelviel:pev--update-stereo-sample
2 parents 2c1f348 + 6d1f7c2 commit 14a9103

File tree

1 file changed

+46
-19
lines changed

1 file changed

+46
-19
lines changed

samples/cpp/stereo_match.cpp

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@
1414
#include "opencv2/core/utility.hpp"
1515

1616
#include <stdio.h>
17+
#include <sstream>
1718

1819
using namespace cv;
1920

2021
static void print_help(char** argv)
2122
{
2223
printf("\nDemo stereo matching converting L and R images into disparity and point clouds\n");
23-
printf("\nUsage: %s <left_image> <right_image> [--algorithm=bm|sgbm|hh|sgbm3way] [--blocksize=<block_size>]\n"
24+
printf("\nUsage: %s <left_image> <right_image> [--algorithm=bm|sgbm|hh|hh4|sgbm3way] [--blocksize=<block_size>]\n"
2425
"[--max-disparity=<max_disparity>] [--scale=scale_factor>] [-i=<intrinsic_filename>] [-e=<extrinsic_filename>]\n"
25-
"[--no-display] [-o=<disparity_image>] [-p=<point_cloud_file>]\n", argv[0]);
26+
"[--no-display] [--color] [-o=<disparity_image>] [-p=<point_cloud_file>]\n", argv[0]);
2627
}
2728

2829
static void saveXYZ(const char* filename, const Mat& mat)
@@ -50,16 +51,17 @@ int main(int argc, char** argv)
5051
std::string disparity_filename = "";
5152
std::string point_cloud_filename = "";
5253

53-
enum { STEREO_BM=0, STEREO_SGBM=1, STEREO_HH=2, STEREO_VAR=3, STEREO_3WAY=4 };
54+
enum { STEREO_BM=0, STEREO_SGBM=1, STEREO_HH=2, STEREO_VAR=3, STEREO_3WAY=4, STEREO_HH4=5 };
5455
int alg = STEREO_SGBM;
5556
int SADWindowSize, numberOfDisparities;
5657
bool no_display;
58+
bool color_display;
5759
float scale;
5860

5961
Ptr<StereoBM> bm = StereoBM::create(16,9);
6062
Ptr<StereoSGBM> sgbm = StereoSGBM::create(0,16,3);
6163
cv::CommandLineParser parser(argc, argv,
62-
"{@arg1||}{@arg2||}{help h||}{algorithm||}{max-disparity|0|}{blocksize|0|}{no-display||}{scale|1|}{i||}{e||}{o||}{p||}");
64+
"{@arg1||}{@arg2||}{help h||}{algorithm||}{max-disparity|0|}{blocksize|0|}{no-display||}{color||}{scale|1|}{i||}{e||}{o||}{p||}");
6365
if(parser.has("help"))
6466
{
6567
print_help(argv);
@@ -74,12 +76,14 @@ int main(int argc, char** argv)
7476
_alg == "sgbm" ? STEREO_SGBM :
7577
_alg == "hh" ? STEREO_HH :
7678
_alg == "var" ? STEREO_VAR :
79+
_alg == "hh4" ? STEREO_HH4 :
7780
_alg == "sgbm3way" ? STEREO_3WAY : -1;
7881
}
7982
numberOfDisparities = parser.get<int>("max-disparity");
8083
SADWindowSize = parser.get<int>("blocksize");
8184
scale = parser.get<float>("scale");
8285
no_display = parser.has("no-display");
86+
color_display = parser.has("color");
8387
if( parser.has("i") )
8488
intrinsic_filename = parser.get<std::string>("i");
8589
if( parser.has("e") )
@@ -238,6 +242,8 @@ int main(int argc, char** argv)
238242
sgbm->setMode(StereoSGBM::MODE_HH);
239243
else if(alg==STEREO_SGBM)
240244
sgbm->setMode(StereoSGBM::MODE_SGBM);
245+
else if(alg==STEREO_HH4)
246+
sgbm->setMode(StereoSGBM::MODE_HH4);
241247
else if(alg==STEREO_3WAY)
242248
sgbm->setMode(StereoSGBM::MODE_SGBM_3WAY);
243249

@@ -254,7 +260,7 @@ int main(int argc, char** argv)
254260
if (disp.type() == CV_16S)
255261
disparity_multiplier = 16.0f;
256262
}
257-
else if( alg == STEREO_SGBM || alg == STEREO_HH || alg == STEREO_3WAY )
263+
else if( alg == STEREO_SGBM || alg == STEREO_HH || alg == STEREO_HH4 || alg == STEREO_3WAY )
258264
{
259265
sgbm->compute(img1, img2, disp);
260266
if (disp.type() == CV_16S)
@@ -268,22 +274,13 @@ int main(int argc, char** argv)
268274
disp.convertTo(disp8, CV_8U, 255/(numberOfDisparities*16.));
269275
else
270276
disp.convertTo(disp8, CV_8U);
271-
if( !no_display )
272-
{
273-
namedWindow("left", 1);
274-
imshow("left", img1);
275-
namedWindow("right", 1);
276-
imshow("right", img2);
277-
namedWindow("disparity", 0);
278-
imshow("disparity", disp8);
279-
printf("press any key to continue...");
280-
fflush(stdout);
281-
waitKey();
282-
printf("\n");
283-
}
277+
278+
Mat disp8_3c;
279+
if (color_display)
280+
cv::applyColorMap(disp8, disp8_3c, COLORMAP_TURBO);
284281

285282
if(!disparity_filename.empty())
286-
imwrite(disparity_filename, disp8);
283+
imwrite(disparity_filename, color_display ? disp8_3c : disp8);
287284

288285
if(!point_cloud_filename.empty())
289286
{
@@ -297,5 +294,35 @@ int main(int argc, char** argv)
297294
printf("\n");
298295
}
299296

297+
if( !no_display )
298+
{
299+
std::ostringstream oss;
300+
oss << "disparity " << (alg==STEREO_BM ? "bm" :
301+
alg==STEREO_SGBM ? "sgbm" :
302+
alg==STEREO_HH ? "hh" :
303+
alg==STEREO_VAR ? "var" :
304+
alg==STEREO_HH4 ? "hh4" :
305+
alg==STEREO_3WAY ? "sgbm3way" : "");
306+
oss << " blocksize:" << (alg==STEREO_BM ? SADWindowSize : sgbmWinSize);
307+
oss << " max-disparity:" << numberOfDisparities;
308+
std::string disp_name = oss.str();
309+
310+
namedWindow("left", cv::WINDOW_NORMAL);
311+
imshow("left", img1);
312+
namedWindow("right", cv::WINDOW_NORMAL);
313+
imshow("right", img2);
314+
namedWindow(disp_name, cv::WINDOW_AUTOSIZE);
315+
imshow(disp_name, color_display ? disp8_3c : disp8);
316+
317+
printf("press ESC key or CTRL+C to close...");
318+
fflush(stdout);
319+
printf("\n");
320+
while(1)
321+
{
322+
if(waitKey() == 27) //ESC (prevents closing on actions like taking screenshots)
323+
break;
324+
}
325+
}
326+
300327
return 0;
301328
}

0 commit comments

Comments
 (0)