perf_stereosgbm.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * By downloading, copying, installing or using the software you agree to this license.
  3. * If you do not agree to this license, do not download, install,
  4. * copy or use the software.
  5. *
  6. *
  7. * License Agreement
  8. * For Open Source Computer Vision Library
  9. * (3 - clause BSD License)
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification,
  12. * are permitted provided that the following conditions are met :
  13. *
  14. * * Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and / or other materials provided with the distribution.
  20. *
  21. * * Neither the names of the copyright holders nor the names of the contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * This software is provided by the copyright holders and contributors "as is" and
  26. * any express or implied warranties, including, but not limited to, the implied
  27. * warranties of merchantability and fitness for a particular purpose are disclaimed.
  28. * In no event shall copyright holders or contributors be liable for any direct,
  29. * indirect, incidental, special, exemplary, or consequential damages
  30. * (including, but not limited to, procurement of substitute goods or services;
  31. * loss of use, data, or profits; or business interruption) however caused
  32. * and on any theory of liability, whether in contract, strict liability,
  33. * or tort(including negligence or otherwise) arising in any way out of
  34. * the use of this software, even if advised of the possibility of such damage.
  35. */
  36. #include "perf_precomp.hpp"
  37. namespace opencv_test
  38. {
  39. using namespace perf;
  40. using namespace testing;
  41. static void MakeArtificialExample(Mat& dst_left_view, Mat& dst_view);
  42. CV_ENUM(SGBMModes, StereoSGBM::MODE_SGBM, StereoSGBM::MODE_SGBM_3WAY, StereoSGBM::MODE_HH4);
  43. typedef tuple<Size, int, SGBMModes> SGBMParams;
  44. typedef TestBaseWithParam<SGBMParams> TestStereoCorrespSGBM;
  45. #ifndef _DEBUG
  46. PERF_TEST_P( TestStereoCorrespSGBM, SGBM, Combine(Values(Size(1280,720),Size(640,480)), Values(256,128), SGBMModes::all()) )
  47. #else
  48. PERF_TEST_P( TestStereoCorrespSGBM, DISABLED_TooLongInDebug_SGBM, Combine(Values(Size(1280,720),Size(640,480)), Values(256,128), SGBMModes::all()) )
  49. #endif
  50. {
  51. SGBMParams params = GetParam();
  52. Size sz = get<0>(params);
  53. int num_disparities = get<1>(params);
  54. int mode = get<2>(params);
  55. Mat src_left(sz, CV_8UC3);
  56. Mat src_right(sz, CV_8UC3);
  57. Mat dst(sz, CV_16S);
  58. MakeArtificialExample(src_left,src_right);
  59. int wsize = 3;
  60. int P1 = 8*src_left.channels()*wsize*wsize;
  61. TEST_CYCLE()
  62. {
  63. Ptr<StereoSGBM> sgbm = StereoSGBM::create(0,num_disparities,wsize,P1,4*P1,1,63,25,0,0,mode);
  64. sgbm->compute(src_left,src_right,dst);
  65. }
  66. SANITY_CHECK(dst, .01, ERROR_RELATIVE);
  67. }
  68. typedef tuple<Size, int> BMParams;
  69. typedef TestBaseWithParam<BMParams> TestStereoCorrespBM;
  70. PERF_TEST_P(TestStereoCorrespBM, BM, Combine(Values(Size(1280, 720), Size(640, 480)), Values(256, 128)))
  71. {
  72. BMParams params = GetParam();
  73. Size sz = get<0>(params);
  74. int num_disparities = get<1>(params);
  75. Mat src_left(sz, CV_8UC1);
  76. Mat src_right(sz, CV_8UC1);
  77. Mat dst(sz, CV_16S);
  78. MakeArtificialExample(src_left, src_right);
  79. int wsize = 21;
  80. TEST_CYCLE()
  81. {
  82. Ptr<StereoBM> bm = StereoBM::create(num_disparities, wsize);
  83. bm->compute(src_left, src_right, dst);
  84. }
  85. SANITY_CHECK(dst, .01, ERROR_RELATIVE);
  86. }
  87. void MakeArtificialExample(Mat& dst_left_view, Mat& dst_right_view)
  88. {
  89. RNG rng(0);
  90. int w = dst_left_view.cols;
  91. int h = dst_left_view.rows;
  92. //params:
  93. unsigned char bg_level = (unsigned char)rng.uniform(0.0,255.0);
  94. unsigned char fg_level = (unsigned char)rng.uniform(0.0,255.0);
  95. int rect_width = (int)rng.uniform(w/16,w/2);
  96. int rect_height = (int)rng.uniform(h/16,h/2);
  97. int rect_disparity = (int)(0.15*w);
  98. double sigma = 3.0;
  99. int rect_x_offset = (w-rect_width) /2;
  100. int rect_y_offset = (h-rect_height)/2;
  101. if(dst_left_view.channels()==3)
  102. {
  103. dst_left_view = Scalar(Vec3b(bg_level,bg_level,bg_level));
  104. dst_right_view = Scalar(Vec3b(bg_level,bg_level,bg_level));
  105. }
  106. else
  107. {
  108. dst_left_view = Scalar(bg_level);
  109. dst_right_view = Scalar(bg_level);
  110. }
  111. Mat dst_left_view_rect = Mat(dst_left_view, Rect(rect_x_offset,rect_y_offset,rect_width,rect_height));
  112. if(dst_left_view.channels()==3)
  113. dst_left_view_rect = Scalar(Vec3b(fg_level,fg_level,fg_level));
  114. else
  115. dst_left_view_rect = Scalar(fg_level);
  116. rect_x_offset-=rect_disparity;
  117. Mat dst_right_view_rect = Mat(dst_right_view, Rect(rect_x_offset,rect_y_offset,rect_width,rect_height));
  118. if(dst_right_view.channels()==3)
  119. dst_right_view_rect = Scalar(Vec3b(fg_level,fg_level,fg_level));
  120. else
  121. dst_right_view_rect = Scalar(fg_level);
  122. //add some gaussian noise:
  123. unsigned char *l, *r;
  124. for(int i=0;i<h;i++)
  125. {
  126. l = dst_left_view.ptr(i);
  127. r = dst_right_view.ptr(i);
  128. if(dst_left_view.channels()==3)
  129. {
  130. for(int j=0;j<w;j++)
  131. {
  132. l[0] = saturate_cast<unsigned char>(l[0] + rng.gaussian(sigma));
  133. l[1] = saturate_cast<unsigned char>(l[1] + rng.gaussian(sigma));
  134. l[2] = saturate_cast<unsigned char>(l[2] + rng.gaussian(sigma));
  135. l+=3;
  136. r[0] = saturate_cast<unsigned char>(r[0] + rng.gaussian(sigma));
  137. r[1] = saturate_cast<unsigned char>(r[1] + rng.gaussian(sigma));
  138. r[2] = saturate_cast<unsigned char>(r[2] + rng.gaussian(sigma));
  139. r+=3;
  140. }
  141. }
  142. else
  143. {
  144. for(int j=0;j<w;j++)
  145. {
  146. l[0] = saturate_cast<unsigned char>(l[0] + rng.gaussian(sigma));
  147. l++;
  148. r[0] = saturate_cast<unsigned char>(r[0] + rng.gaussian(sigma));
  149. r++;
  150. }
  151. }
  152. }
  153. }
  154. }