1/** 2 * @file SBM_Sample 3 * @brief Get a disparity map of two images 4 * @author A. Huaman 5 */ 6 7#include <stdio.h> 8#include <iostream> 9#include "opencv2/calib3d/calib3d.hpp" 10#include "opencv2/core/core.hpp" 11#include "opencv2/imgcodecs.hpp" 12#include "opencv2/highgui/highgui.hpp" 13 14using namespace cv; 15 16const char *windowDisparity = "Disparity"; 17 18void readme(); 19 20/** 21 * @function main 22 * @brief Main function 23 */ 24int main( int argc, char** argv ) 25{ 26 if( argc != 3 ) 27 { readme(); return -1; } 28 29 //-- 1. Read the images 30 Mat imgLeft = imread( argv[1], IMREAD_GRAYSCALE ); 31 Mat imgRight = imread( argv[2], IMREAD_GRAYSCALE ); 32 //-- And create the image in which we will save our disparities 33 Mat imgDisparity16S = Mat( imgLeft.rows, imgLeft.cols, CV_16S ); 34 Mat imgDisparity8U = Mat( imgLeft.rows, imgLeft.cols, CV_8UC1 ); 35 36 if( imgLeft.empty() || imgRight.empty() ) 37 { std::cout<< " --(!) Error reading images " << std::endl; return -1; } 38 39 //-- 2. Call the constructor for StereoBM 40 int ndisparities = 16*5; /**< Range of disparity */ 41 int SADWindowSize = 21; /**< Size of the block window. Must be odd */ 42 43 Ptr<StereoBM> sbm = StereoBM::create( ndisparities, SADWindowSize ); 44 45 //-- 3. Calculate the disparity image 46 sbm->compute( imgLeft, imgRight, imgDisparity16S ); 47 48 //-- Check its extreme values 49 double minVal; double maxVal; 50 51 minMaxLoc( imgDisparity16S, &minVal, &maxVal ); 52 53 printf("Min disp: %f Max value: %f \n", minVal, maxVal); 54 55 //-- 4. Display it as a CV_8UC1 image 56 imgDisparity16S.convertTo( imgDisparity8U, CV_8UC1, 255/(maxVal - minVal)); 57 58 namedWindow( windowDisparity, WINDOW_NORMAL ); 59 imshow( windowDisparity, imgDisparity8U ); 60 61 //-- 5. Save the image 62 imwrite("SBM_sample.png", imgDisparity16S); 63 64 waitKey(0); 65 66 return 0; 67} 68 69/** 70 * @function readme 71 */ 72void readme() 73{ std::cout << " Usage: ./SBMSample <imgLeft> <imgRight>" << std::endl; } 74