perf_labeling.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #include "perf_precomp.hpp"
  43. namespace opencv_test { namespace {
  44. DEF_PARAM_TEST_1(Image, string);
  45. struct GreedyLabeling
  46. {
  47. struct dot
  48. {
  49. int x;
  50. int y;
  51. static dot make(int i, int j)
  52. {
  53. dot d; d.x = i; d.y = j;
  54. return d;
  55. }
  56. };
  57. struct InInterval
  58. {
  59. InInterval(const int& _lo, const int& _hi) : lo(-_lo), hi(_hi) {}
  60. const int lo, hi;
  61. bool operator() (const unsigned char a, const unsigned char b) const
  62. {
  63. int d = a - b;
  64. return lo <= d && d <= hi;
  65. }
  66. private:
  67. InInterval& operator=(const InInterval&);
  68. };
  69. GreedyLabeling(cv::Mat img)
  70. : image(img), _labels(image.size(), CV_32SC1, cv::Scalar::all(-1)) {stack = new dot[image.cols * image.rows];}
  71. ~GreedyLabeling(){delete[] stack;}
  72. void operator() (cv::Mat labels) const
  73. {
  74. labels.setTo(cv::Scalar::all(-1));
  75. InInterval inInt(0, 2);
  76. int cc = -1;
  77. int* dist_labels = (int*)labels.data;
  78. int pitch = static_cast<int>(labels.step1());
  79. unsigned char* source = (unsigned char*)image.data;
  80. int width = image.cols;
  81. int height = image.rows;
  82. for (int j = 0; j < image.rows; ++j)
  83. for (int i = 0; i < image.cols; ++i)
  84. {
  85. if (dist_labels[j * pitch + i] != -1) continue;
  86. dot* top = stack;
  87. dot p = dot::make(i, j);
  88. cc++;
  89. dist_labels[j * pitch + i] = cc;
  90. while (top >= stack)
  91. {
  92. int* dl = &dist_labels[p.y * pitch + p.x];
  93. unsigned char* sp = &source[p.y * image.step1() + p.x];
  94. dl[0] = cc;
  95. //right
  96. if( p.x < (width - 1) && dl[ +1] == -1 && inInt(sp[0], sp[+1]))
  97. *top++ = dot::make(p.x + 1, p.y);
  98. //left
  99. if( p.x > 0 && dl[-1] == -1 && inInt(sp[0], sp[-1]))
  100. *top++ = dot::make(p.x - 1, p.y);
  101. //bottom
  102. if( p.y < (height - 1) && dl[+pitch] == -1 && inInt(sp[0], sp[+image.step1()]))
  103. *top++ = dot::make(p.x, p.y + 1);
  104. //top
  105. if( p.y > 0 && dl[-pitch] == -1 && inInt(sp[0], sp[-static_cast<int>(image.step1())]))
  106. *top++ = dot::make(p.x, p.y - 1);
  107. p = *--top;
  108. }
  109. }
  110. }
  111. cv::Mat image;
  112. cv::Mat _labels;
  113. dot* stack;
  114. };
  115. PERF_TEST_P(Image, DISABLED_Labeling_ConnectivityMask,
  116. Values<string>("gpu/labeling/aloe-disp.png"))
  117. {
  118. declare.time(1.0);
  119. const cv::Mat image = readImage(GetParam(), cv::IMREAD_GRAYSCALE);
  120. ASSERT_FALSE(image.empty());
  121. if (PERF_RUN_CUDA())
  122. {
  123. cv::cuda::GpuMat d_image(image);
  124. cv::cuda::GpuMat mask;
  125. TEST_CYCLE() cv::cuda::connectivityMask(d_image, mask, cv::Scalar::all(0), cv::Scalar::all(2));
  126. CUDA_SANITY_CHECK(mask);
  127. }
  128. else
  129. {
  130. FAIL_NO_CPU();
  131. }
  132. }
  133. PERF_TEST_P(Image, DISABLED_Labeling_ConnectedComponents,
  134. Values<string>("gpu/labeling/aloe-disp.png"))
  135. {
  136. declare.time(1.0);
  137. const cv::Mat image = readImage(GetParam(), cv::IMREAD_GRAYSCALE);
  138. ASSERT_FALSE(image.empty());
  139. if (PERF_RUN_CUDA())
  140. {
  141. cv::cuda::GpuMat d_mask;
  142. cv::cuda::connectivityMask(cv::cuda::GpuMat(image), d_mask, cv::Scalar::all(0), cv::Scalar::all(2));
  143. cv::cuda::GpuMat components;
  144. TEST_CYCLE() cv::cuda::labelComponents(d_mask, components);
  145. CUDA_SANITY_CHECK(components);
  146. }
  147. else
  148. {
  149. GreedyLabeling host(image);
  150. TEST_CYCLE() host(host._labels);
  151. cv::Mat components = host._labels;
  152. CPU_SANITY_CHECK(components);
  153. }
  154. }
  155. }} // namespace