From: Ana Huaman Date: Thu, 3 Nov 2011 05:52:35 +0000 (+0000) Subject: Added one sample in tutorial_code to use Stereo Block matching X-Git-Tag: accepted/2.0/20130307.220821~1657 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8c04ae8c30b13dc55d864883e1514a143dd2e043;p=profile%2Fivi%2Fopencv.git Added one sample in tutorial_code to use Stereo Block matching --- diff --git a/samples/cpp/tutorial_code/calib3d/stereoBM/SBM_Sample.cpp b/samples/cpp/tutorial_code/calib3d/stereoBM/SBM_Sample.cpp new file mode 100644 index 0000000..1ff4301 --- /dev/null +++ b/samples/cpp/tutorial_code/calib3d/stereoBM/SBM_Sample.cpp @@ -0,0 +1,74 @@ +/** + * @file SBM_Sample + * @brief Get a disparity map of two images + * @author A. Huaman + */ + +#include +#include +#include "opencv2/calib3d/calib3d.hpp" +#include "opencv2/core/core.hpp" +#include "opencv2/highgui/highgui.hpp" + +using namespace cv; + +char *windowDisparity = "Disparity"; + +void readme(); + +/** + * @function main + * @brief Main function + */ +int main( int argc, char** argv ) +{ + if( argc != 3 ) + { readme(); return -1; } + + //-- 1. Read the images + Mat imgLeft = imread( argv[1], CV_LOAD_IMAGE_GRAYSCALE ); + Mat imgRight = imread( argv[2], CV_LOAD_IMAGE_GRAYSCALE ); + //-- And create the image in which we will save our disparities + Mat imgDisparity16S = Mat( imgLeft.rows, imgLeft.cols, CV_16S ); + Mat imgDisparity8U = Mat( imgLeft.rows, imgLeft.cols, CV_8UC1 ); + + if( !imgLeft.data || !imgRight.data ) + { std::cout<< " --(!) Error reading images " << std::endl; return -1; } + + //-- 2. Call the constructor for StereoBM + int ndisparities = 16*5; /**< Range of disparity */ + int SADWindowSize = 21; /**< Size of the block window. Must be odd */ + + StereoBM sbm( StereoBM::BASIC_PRESET, + ndisparities, + SADWindowSize ); + + //-- 3. Calculate the disparity image + sbm( imgLeft, imgRight, imgDisparity16S, CV_16S ); + + //-- Check its extreme values + double minVal; double maxVal; + + minMaxLoc( imgDisparity16S, &minVal, &maxVal ); + + printf("Min disp: %f Max value: %f \n", minVal, maxVal); + + //-- 4. Display it as a CV_8UC1 image + imgDisparity16S.convertTo( imgDisparity8U, CV_8UC1, 255/(maxVal - minVal)); + + namedWindow( windowDisparity, CV_WINDOW_NORMAL ); + imshow( windowDisparity, imgDisparity8U ); + + //-- 5. Save the image + imwrite("SBM_sample.png", imgDisparity16S); + + waitKey(0); + + return 0; +} + +/** + * @function readme + */ +void readme() +{ std::cout << " Usage: ./SBMSample " << std::endl; } diff --git a/samples/cpp/tutorial_code/images/stereoLeft.png b/samples/cpp/tutorial_code/images/stereoLeft.png new file mode 100644 index 0000000..ec7f219 Binary files /dev/null and b/samples/cpp/tutorial_code/images/stereoLeft.png differ diff --git a/samples/cpp/tutorial_code/images/stereoRight.png b/samples/cpp/tutorial_code/images/stereoRight.png new file mode 100644 index 0000000..b37cfdc Binary files /dev/null and b/samples/cpp/tutorial_code/images/stereoRight.png differ