test_labeling.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 "test_precomp.hpp"
  43. #ifdef HAVE_CUDA
  44. namespace opencv_test { namespace {
  45. namespace
  46. {
  47. struct GreedyLabeling
  48. {
  49. struct dot
  50. {
  51. int x;
  52. int y;
  53. static dot make(int i, int j)
  54. {
  55. dot d; d.x = i; d.y = j;
  56. return d;
  57. }
  58. };
  59. struct InInterval
  60. {
  61. InInterval(const int& _lo, const int& _hi) : lo(-_lo), hi(_hi) {}
  62. const int lo, hi;
  63. bool operator() (const unsigned char a, const unsigned char b) const
  64. {
  65. int d = a - b;
  66. return lo <= d && d <= hi;
  67. }
  68. };
  69. GreedyLabeling(cv::Mat img)
  70. : image(img), _labels(image.size(), CV_32SC1, cv::Scalar::all(-1)) {}
  71. void operator() (cv::Mat labels) const
  72. {
  73. InInterval inInt(0, 2);
  74. dot* stack = new dot[image.cols * image.rows];
  75. int cc = -1;
  76. int* dist_labels = (int*)labels.data;
  77. int pitch = (int) labels.step1();
  78. unsigned char* source = (unsigned char*)image.data;
  79. int width = image.cols;
  80. int height = image.rows;
  81. int step1 = (int)image.step1();
  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 * 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[+step1]))
  103. *top++ = dot::make(p.x, p.y + 1);
  104. //top
  105. if( p.y > 0 && dl[-pitch] == -1 && inInt(sp[0], sp[-step1]))
  106. *top++ = dot::make(p.x, p.y - 1);
  107. p = *--top;
  108. }
  109. }
  110. delete[] stack;
  111. }
  112. void checkCorrectness(cv::Mat gpu)
  113. {
  114. cv::Mat diff = gpu - _labels;
  115. int outliers = 0;
  116. for (int j = 0; j < image.rows; ++j)
  117. for (int i = 0; i < image.cols - 1; ++i)
  118. {
  119. if ( (_labels.at<int>(j,i) == gpu.at<int>(j,i + 1)) && (diff.at<int>(j, i) != diff.at<int>(j,i + 1)))
  120. {
  121. outliers++;
  122. }
  123. }
  124. ASSERT_TRUE(outliers < gpu.cols + gpu.rows);
  125. }
  126. cv::Mat image;
  127. cv::Mat _labels;
  128. };
  129. }
  130. struct Labeling : testing::TestWithParam<cv::cuda::DeviceInfo>
  131. {
  132. cv::cuda::DeviceInfo devInfo;
  133. virtual void SetUp()
  134. {
  135. devInfo = GetParam();
  136. cv::cuda::setDevice(devInfo.deviceID());
  137. }
  138. cv::Mat loat_image()
  139. {
  140. return cv::imread(std::string( cvtest::TS::ptr()->get_data_path() ) + "labeling/label.png");
  141. }
  142. };
  143. CUDA_TEST_P(Labeling, DISABLED_ConnectedComponents)
  144. {
  145. cv::Mat image;
  146. cvtColor(loat_image(), image, cv::COLOR_BGR2GRAY);
  147. cv::threshold(image, image, 150, 255, cv::THRESH_BINARY);
  148. ASSERT_TRUE(image.type() == CV_8UC1);
  149. GreedyLabeling host(image);
  150. host(host._labels);
  151. cv::cuda::GpuMat mask;
  152. mask.create(image.rows, image.cols, CV_8UC1);
  153. cv::cuda::GpuMat components;
  154. components.create(image.rows, image.cols, CV_32SC1);
  155. cv::cuda::connectivityMask(cv::cuda::GpuMat(image), mask, cv::Scalar::all(0), cv::Scalar::all(2));
  156. cv::cuda::labelComponents(mask, components);
  157. host.checkCorrectness(cv::Mat(components));
  158. }
  159. INSTANTIATE_TEST_CASE_P(CUDA_ConnectedComponents, Labeling, ALL_DEVICES);
  160. }} // namespace
  161. #endif // HAVE_CUDA