test_block_matching.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // Intel License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000, Intel Corporation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of Intel Corporation may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #include "test_precomp.hpp"
  42. namespace opencv_test { namespace {
  43. class CV_BlockMatchingTest : public cvtest::BaseTest
  44. {
  45. public:
  46. CV_BlockMatchingTest();
  47. ~CV_BlockMatchingTest();
  48. protected:
  49. void run(int /* idx */);
  50. };
  51. CV_BlockMatchingTest::CV_BlockMatchingTest(){}
  52. CV_BlockMatchingTest::~CV_BlockMatchingTest(){}
  53. static double errorLevel(const Mat &ideal, Mat &actual)
  54. {
  55. uint8_t *date, *harta;
  56. harta = actual.data;
  57. date = ideal.data;
  58. int stride, h;
  59. stride = (int)ideal.step;
  60. h = ideal.rows;
  61. int error = 0;
  62. for (int i = 0; i < ideal.rows; i++)
  63. {
  64. for (int j = 0; j < ideal.cols; j++)
  65. {
  66. if (date[i * stride + j] != 0)
  67. if (abs(date[i * stride + j] - harta[i * stride + j]) > 2 * 16)
  68. {
  69. error += 1;
  70. }
  71. }
  72. }
  73. return ((double)((error * 100) * 1.0) / (stride * h));
  74. }
  75. void CV_BlockMatchingTest::run(int )
  76. {
  77. Mat image1, image2, gt;
  78. image1 = imread(ts->get_data_path() + "stereomatching/datasets/tsukuba/im2.png", IMREAD_GRAYSCALE);
  79. image2 = imread(ts->get_data_path() + "stereomatching/datasets/tsukuba/im6.png", IMREAD_GRAYSCALE);
  80. gt = imread(ts->get_data_path() + "stereomatching/datasets/tsukuba/disp2.png", IMREAD_GRAYSCALE);
  81. if(image1.empty() || image2.empty() || gt.empty())
  82. {
  83. ts->printf(cvtest::TS::LOG, "Wrong input data \n");
  84. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  85. return;
  86. }
  87. if(image1.rows != image2.rows || image1.cols != image2.cols || gt.cols != image1.cols || gt.rows != image1.rows)
  88. {
  89. ts->printf(cvtest::TS::LOG, "Wrong input / output dimension \n");
  90. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  91. return;
  92. }
  93. RNG range;
  94. //set the parameters
  95. int binary_descriptor_type = range.uniform(0,8);
  96. int kernel_size, aggregation_window;
  97. if(binary_descriptor_type == 0)
  98. kernel_size = 5;
  99. else if(binary_descriptor_type == 2 || binary_descriptor_type == 3)
  100. kernel_size = 7;
  101. else if(binary_descriptor_type == 1)
  102. kernel_size = 11;
  103. else
  104. kernel_size = 9;
  105. if(binary_descriptor_type == 3)
  106. aggregation_window = 13;
  107. else
  108. aggregation_window = 11;
  109. Mat test = Mat(image1.rows, image1.cols, CV_8UC1);
  110. Ptr<StereoBinaryBM> sbm = StereoBinaryBM::create(16, kernel_size);
  111. //we set the corresponding parameters
  112. sbm->setPreFilterCap(31);
  113. sbm->setMinDisparity(0);
  114. sbm->setTextureThreshold(10);
  115. sbm->setUniquenessRatio(0);
  116. sbm->setSpeckleWindowSize(400);//speckle size
  117. sbm->setSpeckleRange(200);
  118. sbm->setDisp12MaxDiff(0);
  119. sbm->setScalleFactor(16);//the scaling factor
  120. sbm->setBinaryKernelType(binary_descriptor_type);//binary descriptor kernel
  121. sbm->setAgregationWindowSize(aggregation_window);
  122. //speckle removal algorithm the user can choose between the average speckle removal algorithm
  123. //or the classical version that was implemented in open cv
  124. sbm->setSpekleRemovalTechnique(CV_SPECKLE_REMOVAL_AVG_ALGORITHM);
  125. sbm->setUsePrefilter(false);//pre-filter or not the images prior to making the transformations
  126. //-- calculate the disparity image
  127. sbm->compute(image1, image2, test);
  128. if(test.empty())
  129. {
  130. ts->printf(cvtest::TS::LOG, "Wrong input / output dimension \n");
  131. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
  132. return;
  133. }
  134. if(errorLevel(gt,test) > 20)
  135. {
  136. ts->printf( cvtest::TS::LOG,
  137. "Too big error\n");
  138. ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
  139. return;
  140. }
  141. }
  142. class CV_SGBlockMatchingTest : public cvtest::BaseTest
  143. {
  144. public:
  145. CV_SGBlockMatchingTest();
  146. ~CV_SGBlockMatchingTest();
  147. protected:
  148. void run(int /* idx */);
  149. };
  150. CV_SGBlockMatchingTest::CV_SGBlockMatchingTest(){}
  151. CV_SGBlockMatchingTest::~CV_SGBlockMatchingTest(){}
  152. void CV_SGBlockMatchingTest::run(int )
  153. {
  154. Mat image1, image2, gt;
  155. image1 = imread(ts->get_data_path() + "stereomatching/datasets/tsukuba/im2.png", IMREAD_GRAYSCALE);
  156. image2 = imread(ts->get_data_path() + "stereomatching/datasets/tsukuba/im6.png", IMREAD_GRAYSCALE);
  157. gt = imread(ts->get_data_path() + "stereomatching/datasets/tsukuba/disp2.png", IMREAD_GRAYSCALE);
  158. ts->printf(cvtest::TS::LOG,(ts->get_data_path() + "stereomatching/datasets/tsukuba/im2.png").c_str());
  159. if(image1.empty() || image2.empty() || gt.empty())
  160. {
  161. ts->printf(cvtest::TS::LOG, "Wrong input data \n");
  162. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  163. return;
  164. }
  165. if(image1.rows != image2.rows || image1.cols != image2.cols || gt.cols != image1.cols || gt.rows != image1.rows)
  166. {
  167. ts->printf(cvtest::TS::LOG, "Wrong input / output dimension \n");
  168. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  169. return;
  170. }
  171. RNG range;
  172. //set the parameters
  173. int binary_descriptor_type = range.uniform(0,8);
  174. int kernel_size;
  175. if(binary_descriptor_type == 0)
  176. kernel_size = 5;
  177. else if(binary_descriptor_type == 2 || binary_descriptor_type == 3)
  178. kernel_size = 7;
  179. else if(binary_descriptor_type == 1)
  180. kernel_size = 11;
  181. else
  182. kernel_size = 9;
  183. Mat test = Mat(image1.rows, image1.cols, CV_8UC1);
  184. Mat imgDisparity16S2 = Mat(image1.rows, image1.cols, CV_16S);
  185. Ptr<StereoBinarySGBM> sgbm = StereoBinarySGBM::create(0, 16, kernel_size);
  186. //setting the penalties for sgbm
  187. sgbm->setP1(10);
  188. sgbm->setP2(100);
  189. sgbm->setMinDisparity(0);
  190. sgbm->setNumDisparities(16);//set disparity number
  191. sgbm->setUniquenessRatio(1);
  192. sgbm->setSpeckleWindowSize(400);
  193. sgbm->setSpeckleRange(200);
  194. sgbm->setDisp12MaxDiff(1);
  195. sgbm->setBinaryKernelType(binary_descriptor_type);//set the binary descriptor
  196. sgbm->setSpekleRemovalTechnique(CV_SPECKLE_REMOVAL_AVG_ALGORITHM); //the avg speckle removal algorithm
  197. sgbm->setSubPixelInterpolationMethod(CV_SIMETRICV_INTERPOLATION);// the SIMETRIC V interpolation method
  198. sgbm->compute(image1, image2, imgDisparity16S2);
  199. double minVal; double maxVal;
  200. minMaxLoc(imgDisparity16S2, &minVal, &maxVal);
  201. imgDisparity16S2.convertTo(test, CV_8UC1, 255 / (maxVal - minVal));
  202. if(test.empty())
  203. {
  204. ts->printf(cvtest::TS::LOG, "Wrong input / output dimension \n");
  205. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
  206. return;
  207. }
  208. double error = errorLevel(gt,test);
  209. if(error > 10)
  210. {
  211. ts->printf( cvtest::TS::LOG,
  212. "Too big error\n");
  213. ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
  214. return;
  215. }
  216. }
  217. TEST(block_matching_simple_test, accuracy) { CV_BlockMatchingTest test; test.safe_run(); }
  218. TEST(SG_block_matching_simple_test, accuracy) { CV_SGBlockMatchingTest test; test.safe_run(); }
  219. }} // namespace