14
14
#include " opencv2/core/utility.hpp"
15
15
16
16
#include < stdio.h>
17
+ #include < sstream>
17
18
18
19
using namespace cv ;
19
20
20
21
static void print_help (char ** argv)
21
22
{
22
23
printf (" \n Demo stereo matching converting L and R images into disparity and point clouds\n " );
23
- printf (" \n Usage: %s <left_image> <right_image> [--algorithm=bm|sgbm|hh|sgbm3way] [--blocksize=<block_size>]\n "
24
+ printf (" \n Usage: %s <left_image> <right_image> [--algorithm=bm|sgbm|hh|hh4| sgbm3way] [--blocksize=<block_size>]\n "
24
25
" [--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 ]);
26
27
}
27
28
28
29
static void saveXYZ (const char * filename, const Mat& mat)
@@ -50,16 +51,17 @@ int main(int argc, char** argv)
50
51
std::string disparity_filename = " " ;
51
52
std::string point_cloud_filename = " " ;
52
53
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 };
54
55
int alg = STEREO_SGBM;
55
56
int SADWindowSize, numberOfDisparities;
56
57
bool no_display;
58
+ bool color_display;
57
59
float scale;
58
60
59
61
Ptr<StereoBM> bm = StereoBM::create (16 ,9 );
60
62
Ptr<StereoSGBM> sgbm = StereoSGBM::create (0 ,16 ,3 );
61
63
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||}" );
63
65
if (parser.has (" help" ))
64
66
{
65
67
print_help (argv);
@@ -74,12 +76,14 @@ int main(int argc, char** argv)
74
76
_alg == " sgbm" ? STEREO_SGBM :
75
77
_alg == " hh" ? STEREO_HH :
76
78
_alg == " var" ? STEREO_VAR :
79
+ _alg == " hh4" ? STEREO_HH4 :
77
80
_alg == " sgbm3way" ? STEREO_3WAY : -1 ;
78
81
}
79
82
numberOfDisparities = parser.get <int >(" max-disparity" );
80
83
SADWindowSize = parser.get <int >(" blocksize" );
81
84
scale = parser.get <float >(" scale" );
82
85
no_display = parser.has (" no-display" );
86
+ color_display = parser.has (" color" );
83
87
if ( parser.has (" i" ) )
84
88
intrinsic_filename = parser.get <std::string>(" i" );
85
89
if ( parser.has (" e" ) )
@@ -238,6 +242,8 @@ int main(int argc, char** argv)
238
242
sgbm->setMode (StereoSGBM::MODE_HH);
239
243
else if (alg==STEREO_SGBM)
240
244
sgbm->setMode (StereoSGBM::MODE_SGBM);
245
+ else if (alg==STEREO_HH4)
246
+ sgbm->setMode (StereoSGBM::MODE_HH4);
241
247
else if (alg==STEREO_3WAY)
242
248
sgbm->setMode (StereoSGBM::MODE_SGBM_3WAY);
243
249
@@ -254,7 +260,7 @@ int main(int argc, char** argv)
254
260
if (disp.type () == CV_16S)
255
261
disparity_multiplier = 16 .0f ;
256
262
}
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 )
258
264
{
259
265
sgbm->compute (img1, img2, disp);
260
266
if (disp.type () == CV_16S)
@@ -268,22 +274,13 @@ int main(int argc, char** argv)
268
274
disp.convertTo (disp8, CV_8U, 255 /(numberOfDisparities*16 .));
269
275
else
270
276
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);
284
281
285
282
if (!disparity_filename.empty ())
286
- imwrite (disparity_filename, disp8);
283
+ imwrite (disparity_filename, color_display ? disp8_3c : disp8);
287
284
288
285
if (!point_cloud_filename.empty ())
289
286
{
@@ -297,5 +294,35 @@ int main(int argc, char** argv)
297
294
printf (" \n " );
298
295
}
299
296
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
+
300
327
return 0 ;
301
328
}
0 commit comments