sample.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include "opencv2/stereo.hpp"
  2. #include "opencv2/imgproc.hpp"
  3. #include "opencv2/highgui.hpp"
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <iostream>
  7. using namespace std;
  8. using namespace cv;
  9. using namespace cv::stereo;
  10. enum { STEREO_BINARY_BM, STEREO_BINARY_SGM };
  11. static bool parse_argument_values(int argc, char **argv, string &left, string &right, int &kernel_size, int &number_of_disparities,
  12. int &aggregation_window, int &P1, int &P2, float &scale, int &algo, int &binary_descriptor_type, int &success);
  13. int main(int argc, char** argv)
  14. {
  15. string left, right;
  16. int kernel_size = 0, number_of_disparities = 0, aggregation_window = 0, P1 = 0, P2 = 0;
  17. float scale = 4;
  18. int algo = STEREO_BINARY_BM;
  19. int binary_descriptor_type = 0;
  20. int success;
  21. // here we extract the values that were added as arguments
  22. // we also test to see if they are provided correcly
  23. if (!parse_argument_values(argc, argv, left, right,
  24. kernel_size,
  25. number_of_disparities,
  26. aggregation_window,
  27. P1, P2,
  28. scale,
  29. algo, binary_descriptor_type,success))
  30. {
  31. return 1;
  32. }
  33. // verify if the user inputs the correct number of parameters
  34. Mat image1, image2;
  35. // we read a pair of images from the disk
  36. image1 = imread(left, CV_8UC1);
  37. image2 = imread(right, CV_8UC1);
  38. // verify if they are loaded correctly
  39. if (image1.empty() || image2.empty())
  40. {
  41. cout << " --(!) Error reading images \n";
  42. return 1;
  43. }
  44. // we display the parsed parameters
  45. const char *b[7] = { "CV_DENSE_CENSUS", "CV_SPARSE_CENSUS", "CV_CS_CENSUS", "CV_MODIFIED_CS_CENSUS",
  46. "CV_MODIFIED_CENSUS_TRANSFORM", "CV_MEAN_VARIATION", "CV_STAR_KERNEL" };
  47. cout << "Program Name: " << argv[0];
  48. cout << "\nPath to left image " << left << " \n" << "Path to right image " << right << "\n";
  49. cout << "\nkernel size " << kernel_size << "\n"
  50. << "numberOfDisparities " << number_of_disparities << "\n"
  51. << "aggregationWindow " << aggregation_window << "\n"
  52. << "scallingFactor " << scale << "\n" << "Descriptor name : " << b[binary_descriptor_type] << "\n";
  53. Mat imgDisparity16S2 = Mat(image1.rows, image1.cols, CV_16S);
  54. Mat imgDisparity8U2 = Mat(image1.rows, image1.cols, CV_8UC1);
  55. imshow("Original Left image", image1);
  56. if (algo == STEREO_BINARY_BM)
  57. {
  58. Ptr<StereoBinaryBM> sbm = StereoBinaryBM::create(number_of_disparities, kernel_size);
  59. // we set the corresponding parameters
  60. sbm->setPreFilterCap(31);
  61. sbm->setMinDisparity(0);
  62. sbm->setTextureThreshold(10);
  63. sbm->setUniquenessRatio(0);
  64. sbm->setSpeckleWindowSize(400); // speckle size
  65. sbm->setSpeckleRange(200);
  66. sbm->setDisp12MaxDiff(0);
  67. sbm->setScalleFactor((int)scale); // the scaling factor
  68. sbm->setBinaryKernelType(binary_descriptor_type); // binary descriptor kernel
  69. sbm->setAgregationWindowSize(aggregation_window);
  70. // the user can choose between the average speckle removal algorithm or
  71. // the classical version that was implemented in OpenCV
  72. sbm->setSpekleRemovalTechnique(CV_SPECKLE_REMOVAL_AVG_ALGORITHM);
  73. sbm->setUsePrefilter(false);
  74. //-- calculate the disparity image
  75. sbm->compute(image1, image2, imgDisparity8U2);
  76. imshow("Disparity", imgDisparity8U2);
  77. }
  78. else if (algo == STEREO_BINARY_SGM)
  79. {
  80. // we set the corresponding parameters
  81. Ptr<StereoBinarySGBM> sgbm = StereoBinarySGBM::create(0, number_of_disparities, kernel_size);
  82. // setting the penalties for sgbm
  83. sgbm->setP1(P1);
  84. sgbm->setP2(P2);
  85. sgbm->setMinDisparity(0);
  86. sgbm->setUniquenessRatio(5);
  87. sgbm->setSpeckleWindowSize(400);
  88. sgbm->setSpeckleRange(0);
  89. sgbm->setDisp12MaxDiff(1);
  90. sgbm->setBinaryKernelType(binary_descriptor_type);
  91. sgbm->setSpekleRemovalTechnique(CV_SPECKLE_REMOVAL_AVG_ALGORITHM);
  92. sgbm->setSubPixelInterpolationMethod(CV_SIMETRICV_INTERPOLATION);
  93. sgbm->compute(image1, image2, imgDisparity16S2);
  94. /*Alternative for scalling
  95. imgDisparity16S2.convertTo(imgDisparity8U2, CV_8UC1, scale);
  96. */
  97. double minVal; double maxVal;
  98. minMaxLoc(imgDisparity16S2, &minVal, &maxVal);
  99. imgDisparity16S2.convertTo(imgDisparity8U2, CV_8UC1, 255 / (maxVal - minVal));
  100. //show the disparity image
  101. imshow("Windowsgm", imgDisparity8U2);
  102. }
  103. waitKey(0);
  104. return 0;
  105. }
  106. static bool parse_argument_values(int argc, char **argv, string &left, string &right, int &kernel_size, int &number_of_disparities,
  107. int &aggregation_window, int &P1, int &P2, float &scale, int &algo, int &binary_descriptor_type, int &success)
  108. {
  109. static const char* keys =
  110. "{ @left | | }"
  111. "{ @right | | }"
  112. "{ k kernel_size | 9 | }"
  113. "{ d disparity | 128 | }"
  114. "{ w aggregation_window | 9 | }"
  115. "{ P1 | 100 | }"
  116. "{ P2 | 1000 | }"
  117. "{ b binary_descriptor | 4 | Index of the descriptor type:\n 0 - CV_DENSE_CENSUS,\n 1 - CV_SPARSE_CENSUS,\n 2 - CV_CS_CENSUS,\n 3 - CV_MODIFIED_CS_CENSUS,\n 4 - CV_MODIFIED_CENSUS_TRANSFORM,\n 5 - CV_MEAN_VARIATION,\n 6 - CV_STAR_KERNEL}"
  118. "{ s scale | 1.01593 | }"
  119. "{ a algorithm | sgm | }"
  120. ;
  121. cv::CommandLineParser parser( argc, argv, keys );
  122. left = parser.get<string>(0);
  123. right = parser.get<string>(1);
  124. kernel_size = parser.get<int>("kernel_size");
  125. number_of_disparities = parser.get<int>("disparity");
  126. aggregation_window = parser.get<int>("aggregation_window");
  127. P1 = parser.get<int>("P1");
  128. P2 = parser.get<int>("P2");
  129. binary_descriptor_type = parser.get<int>("binary_descriptor");
  130. scale = parser.get<float>("scale");
  131. algo = parser.get<string>("algorithm") == "sgm" ? STEREO_BINARY_SGM : STEREO_BINARY_BM;
  132. parser.about("\nDemo stereo matching converting L and R images into disparity images using BM and SGBM\n");
  133. success = 1;
  134. //TEST if the provided parameters are correct
  135. if(binary_descriptor_type == CV_DENSE_CENSUS && kernel_size > 5)
  136. {
  137. cout << "For the dense census transform the maximum kernel size should be 5\n";
  138. success = 0;
  139. }
  140. if((binary_descriptor_type == CV_MEAN_VARIATION || binary_descriptor_type == CV_MODIFIED_CENSUS_TRANSFORM || binary_descriptor_type == CV_STAR_KERNEL) && kernel_size != 9)
  141. {
  142. cout <<" For Mean variation and the modified census transform the kernel size should be equal to 9\n";
  143. success = 0;
  144. }
  145. if((binary_descriptor_type == CV_CS_CENSUS || binary_descriptor_type == CV_MODIFIED_CS_CENSUS) && kernel_size > 7)
  146. {
  147. cout << " The kernel size should be smaller or equal to 7 for the CS census and modified center symetric census\n";
  148. success = 0;
  149. }
  150. if(binary_descriptor_type == CV_SPARSE_CENSUS && kernel_size > 11)
  151. {
  152. cout << "The kernel size for the sparse census must be smaller or equal to 11\n";
  153. success = 0;
  154. }
  155. if(number_of_disparities < 10)
  156. {
  157. cout << "Number of disparities should be greater than 10\n";
  158. success = 0;
  159. }
  160. if(aggregation_window < 3)
  161. {
  162. cout << "Aggregation window should be > 3";
  163. success = 0;
  164. }
  165. if(scale < 1)
  166. {
  167. cout << "The scale should be a positive number \n";
  168. success = 0;
  169. }
  170. if(P1 != 0)
  171. {
  172. if(P2 / P1 < 2)
  173. {
  174. cout << "You should probably choose a greater P2 penalty\n";
  175. success = 0;
  176. }
  177. }
  178. else
  179. {
  180. cout << " Penalties should be greater than 0\n";
  181. success = 0;
  182. }
  183. if (!parser.check() || !success)
  184. {
  185. parser.printMessage();
  186. return false;
  187. }
  188. return true;
  189. }