test_distancetransform.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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_DisTransTest : public cvtest::ArrayTest
  44. {
  45. public:
  46. CV_DisTransTest();
  47. protected:
  48. void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
  49. double get_success_error_level( int test_case_idx, int i, int j );
  50. void run_func();
  51. void prepare_to_validation( int );
  52. void get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high );
  53. int prepare_test_case( int test_case_idx );
  54. int mask_size;
  55. int dist_type;
  56. int fill_labels;
  57. float mask[3];
  58. };
  59. CV_DisTransTest::CV_DisTransTest()
  60. {
  61. test_array[INPUT].push_back(NULL);
  62. test_array[OUTPUT].push_back(NULL);
  63. test_array[OUTPUT].push_back(NULL);
  64. test_array[REF_OUTPUT].push_back(NULL);
  65. test_array[REF_OUTPUT].push_back(NULL);
  66. optional_mask = false;
  67. element_wise_relative_error = true;
  68. }
  69. void CV_DisTransTest::get_test_array_types_and_sizes( int test_case_idx,
  70. vector<vector<Size> >& sizes, vector<vector<int> >& types )
  71. {
  72. RNG& rng = ts->get_rng();
  73. cvtest::ArrayTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
  74. types[INPUT][0] = CV_8UC1;
  75. types[OUTPUT][0] = types[REF_OUTPUT][0] = CV_32FC1;
  76. types[OUTPUT][1] = types[REF_OUTPUT][1] = CV_32SC1;
  77. if( cvtest::randInt(rng) & 1 )
  78. {
  79. mask_size = 3;
  80. }
  81. else
  82. {
  83. mask_size = 5;
  84. }
  85. dist_type = cvtest::randInt(rng) % 3;
  86. dist_type = dist_type == 0 ? CV_DIST_C : dist_type == 1 ? CV_DIST_L1 : CV_DIST_L2;
  87. // for now, check only the "labeled" distance transform mode
  88. fill_labels = 0;
  89. if( !fill_labels )
  90. sizes[OUTPUT][1] = sizes[REF_OUTPUT][1] = cvSize(0,0);
  91. }
  92. double CV_DisTransTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
  93. {
  94. Size sz = test_mat[INPUT][0].size();
  95. return dist_type == CV_DIST_C || dist_type == CV_DIST_L1 ? 0 : 0.01*MAX(sz.width, sz.height);
  96. }
  97. void CV_DisTransTest::get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high )
  98. {
  99. cvtest::ArrayTest::get_minmax_bounds( i, j, type, low, high );
  100. if( i == INPUT && CV_MAT_DEPTH(type) == CV_8U )
  101. {
  102. low = Scalar::all(0);
  103. high = Scalar::all(10);
  104. }
  105. }
  106. int CV_DisTransTest::prepare_test_case( int test_case_idx )
  107. {
  108. int code = cvtest::ArrayTest::prepare_test_case( test_case_idx );
  109. if( code > 0 )
  110. {
  111. // the function's response to an "all-nonzeros" image is not determined,
  112. // so put at least one zero point
  113. Mat& mat = test_mat[INPUT][0];
  114. RNG& rng = ts->get_rng();
  115. int i = cvtest::randInt(rng) % mat.rows;
  116. int j = cvtest::randInt(rng) % mat.cols;
  117. mat.at<uchar>(i,j) = 0;
  118. }
  119. return code;
  120. }
  121. void CV_DisTransTest::run_func()
  122. {
  123. cvDistTransform( test_array[INPUT][0], test_array[OUTPUT][0], dist_type, mask_size,
  124. dist_type == CV_DIST_USER ? mask : 0, test_array[OUTPUT][1] );
  125. }
  126. static void
  127. cvTsDistTransform( const CvMat* _src, CvMat* _dst, int dist_type,
  128. int mask_size, float* _mask, CvMat* /*_labels*/ )
  129. {
  130. int i, j, k;
  131. int width = _src->cols, height = _src->rows;
  132. const float init_val = 1e6;
  133. float mask[3];
  134. CvMat* temp;
  135. int ofs[16] = {0};
  136. float delta[16];
  137. int tstep, count;
  138. CV_Assert( mask_size == 3 || mask_size == 5 );
  139. if( dist_type == CV_DIST_USER )
  140. memcpy( mask, _mask, sizeof(mask) );
  141. else if( dist_type == CV_DIST_C )
  142. {
  143. mask_size = 3;
  144. mask[0] = mask[1] = 1.f;
  145. }
  146. else if( dist_type == CV_DIST_L1 )
  147. {
  148. mask_size = 3;
  149. mask[0] = 1.f;
  150. mask[1] = 2.f;
  151. }
  152. else if( mask_size == 3 )
  153. {
  154. mask[0] = 0.955f;
  155. mask[1] = 1.3693f;
  156. }
  157. else
  158. {
  159. mask[0] = 1.0f;
  160. mask[1] = 1.4f;
  161. mask[2] = 2.1969f;
  162. }
  163. temp = cvCreateMat( height + mask_size-1, width + mask_size-1, CV_32F );
  164. tstep = temp->step / sizeof(float);
  165. if( mask_size == 3 )
  166. {
  167. count = 4;
  168. ofs[0] = -1; delta[0] = mask[0];
  169. ofs[1] = -tstep-1; delta[1] = mask[1];
  170. ofs[2] = -tstep; delta[2] = mask[0];
  171. ofs[3] = -tstep+1; delta[3] = mask[1];
  172. }
  173. else
  174. {
  175. count = 8;
  176. ofs[0] = -1; delta[0] = mask[0];
  177. ofs[1] = -tstep-2; delta[1] = mask[2];
  178. ofs[2] = -tstep-1; delta[2] = mask[1];
  179. ofs[3] = -tstep; delta[3] = mask[0];
  180. ofs[4] = -tstep+1; delta[4] = mask[1];
  181. ofs[5] = -tstep+2; delta[5] = mask[2];
  182. ofs[6] = -tstep*2-1; delta[6] = mask[2];
  183. ofs[7] = -tstep*2+1; delta[7] = mask[2];
  184. }
  185. for( i = 0; i < mask_size/2; i++ )
  186. {
  187. float* t0 = (float*)(temp->data.ptr + i*temp->step);
  188. float* t1 = (float*)(temp->data.ptr + (temp->rows - i - 1)*temp->step);
  189. for( j = 0; j < width + mask_size - 1; j++ )
  190. t0[j] = t1[j] = init_val;
  191. }
  192. for( i = 0; i < height; i++ )
  193. {
  194. uchar* s = _src->data.ptr + i*_src->step;
  195. float* tmp = (float*)(temp->data.ptr + temp->step*(i + (mask_size/2))) + (mask_size/2);
  196. for( j = 0; j < mask_size/2; j++ )
  197. tmp[-j-1] = tmp[j + width] = init_val;
  198. for( j = 0; j < width; j++ )
  199. {
  200. if( s[j] == 0 )
  201. tmp[j] = 0;
  202. else
  203. {
  204. float min_dist = init_val;
  205. for( k = 0; k < count; k++ )
  206. {
  207. float t = tmp[j+ofs[k]] + delta[k];
  208. if( min_dist > t )
  209. min_dist = t;
  210. }
  211. tmp[j] = min_dist;
  212. }
  213. }
  214. }
  215. for( i = height - 1; i >= 0; i-- )
  216. {
  217. float* d = (float*)(_dst->data.ptr + i*_dst->step);
  218. float* tmp = (float*)(temp->data.ptr + temp->step*(i + (mask_size/2))) + (mask_size/2);
  219. for( j = width - 1; j >= 0; j-- )
  220. {
  221. float min_dist = tmp[j];
  222. if( min_dist > mask[0] )
  223. {
  224. for( k = 0; k < count; k++ )
  225. {
  226. float t = tmp[j-ofs[k]] + delta[k];
  227. if( min_dist > t )
  228. min_dist = t;
  229. }
  230. tmp[j] = min_dist;
  231. }
  232. d[j] = min_dist;
  233. }
  234. }
  235. cvReleaseMat( &temp );
  236. }
  237. void CV_DisTransTest::prepare_to_validation( int /*test_case_idx*/ )
  238. {
  239. CvMat _input = cvMat(test_mat[INPUT][0]), _output = cvMat(test_mat[REF_OUTPUT][0]);
  240. cvTsDistTransform( &_input, &_output, dist_type, mask_size, mask, 0 );
  241. }
  242. TEST(Imgproc_DistanceTransform, accuracy) { CV_DisTransTest test; test.safe_run(); }
  243. BIGDATA_TEST(Imgproc_DistanceTransform, large_image_12218)
  244. {
  245. const int lls_maxcnt = 79992000; // labels's maximum count
  246. const int lls_mincnt = 1; // labels's minimum count
  247. int i, j, nz;
  248. Mat src(8000, 20000, CV_8UC1), dst, labels;
  249. for( i = 0; i < src.rows; i++ )
  250. for( j = 0; j < src.cols; j++ )
  251. src.at<uchar>(i, j) = (j > (src.cols / 2)) ? 0 : 255;
  252. distanceTransform(src, dst, labels, cv::DIST_L2, cv::DIST_MASK_3, DIST_LABEL_PIXEL);
  253. double scale = (double)lls_mincnt / (double)lls_maxcnt;
  254. labels.convertTo(labels, CV_32SC1, scale);
  255. Size size = labels.size();
  256. nz = cv::countNonZero(labels);
  257. EXPECT_EQ(nz, (size.height*size.width / 2));
  258. }
  259. }} // namespace