PageRenderTime 1462ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/samples/cpp/tutorial_code/calib3d/stereoBM/SBM_Sample.cpp

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