test_run_length_morphology.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include "test_precomp.hpp"
  5. #include "opencv2/ximgproc/run_length_morphology.hpp"
  6. #include "opencv2/imgproc.hpp"
  7. namespace opencv_test {
  8. namespace {
  9. const Size img_size(640, 480);
  10. const int tile_size(20);
  11. typedef tuple<cv::MorphTypes, int, int> RLMParams;
  12. typedef tuple<cv::MorphTypes, int, int, int> RLMSParams;
  13. class RLTestBase
  14. {
  15. public:
  16. RLTestBase() { }
  17. protected:
  18. std::vector<Mat> test_image;
  19. std::vector<Mat> test_image_rle;
  20. void generateCheckerBoard(Mat& image);
  21. void generateRandomImage(Mat& image);
  22. bool areImagesIdentical(Mat& pixelImage, Mat& rleImage);
  23. bool arePixelImagesIdentical(Mat& image1, Mat& image2);
  24. void setUp_impl();
  25. };
  26. void RLTestBase::generateCheckerBoard(Mat& image)
  27. {
  28. image.create(img_size, CV_8UC1);
  29. for (int iy = 0; iy < img_size.height; iy += tile_size)
  30. {
  31. Range rowRange(iy, std::min(iy + tile_size, img_size.height));
  32. for (int ix = 0; ix < img_size.width; ix += tile_size)
  33. {
  34. Range colRange(ix, std::min(ix + tile_size, img_size.width));
  35. Mat tile(image, rowRange, colRange);
  36. bool bBright = ((iy + ix) % (2 * tile_size) == 0);
  37. tile = (bBright ? Scalar(255) : Scalar(0));
  38. }
  39. }
  40. }
  41. void RLTestBase::generateRandomImage(Mat& image)
  42. {
  43. image.create(img_size, CV_8UC1);
  44. randu(image, Scalar::all(0), Scalar::all(255));
  45. }
  46. void RLTestBase::setUp_impl()
  47. {
  48. test_image.resize(2);
  49. test_image_rle.resize(2);
  50. generateCheckerBoard(test_image[0]);
  51. rl::threshold(test_image[0], test_image_rle[0], 100.0, THRESH_BINARY);
  52. cv::Mat theRandom;
  53. generateRandomImage(theRandom);
  54. double dThreshold = 254.0;
  55. cv::threshold(theRandom, test_image[1], dThreshold, 255.0, THRESH_BINARY);
  56. rl::threshold(theRandom, test_image_rle[1], dThreshold, THRESH_BINARY);
  57. }
  58. bool RLTestBase::areImagesIdentical(Mat& pixelImage, Mat& rleImage)
  59. {
  60. cv::Mat rleConverted;
  61. rleConverted = cv::Mat::zeros(pixelImage.rows, pixelImage.cols, CV_8UC1);
  62. rl::paint(rleConverted, rleImage, Scalar(255.0));
  63. return arePixelImagesIdentical(pixelImage, rleConverted);
  64. }
  65. bool RLTestBase::arePixelImagesIdentical(Mat& image1, Mat& image2)
  66. {
  67. cv::Mat diff;
  68. cv::absdiff(image1, image2, diff);
  69. int nDiff = cv::countNonZero(diff);
  70. return (nDiff == 0);
  71. }
  72. class RL_Identical_Result_Simple : public RLTestBase, public ::testing::TestWithParam<RLMSParams>
  73. {
  74. public:
  75. RL_Identical_Result_Simple() { }
  76. protected:
  77. virtual void SetUp() { setUp_impl(); }
  78. };
  79. TEST_P(RL_Identical_Result_Simple, simple)
  80. {
  81. Mat resPix, resRLE;
  82. RLMSParams param = GetParam();
  83. cv::MorphTypes elementType = get<0>(param);
  84. int nSize = get<1>(param);
  85. int image = get<2>(param);
  86. int op = get<3>(param);
  87. Mat element = getStructuringElement(elementType, Size(nSize * 2 + 1, nSize * 2 + 1),
  88. Point(nSize, nSize));
  89. morphologyEx(test_image[image], resPix, op, element);
  90. Mat elementRLE = rl::getStructuringElement(elementType, Size(nSize * 2 + 1, nSize * 2 + 1));
  91. rl::morphologyEx(test_image_rle[image], resRLE, op, elementRLE);
  92. ASSERT_TRUE(areImagesIdentical(resPix, resRLE));
  93. }
  94. INSTANTIATE_TEST_CASE_P(TypicalSET, RL_Identical_Result_Simple, Combine(Values(MORPH_RECT, MORPH_CROSS, MORPH_ELLIPSE),
  95. Values(1, 5, 11), Values(0, 1), Values(MORPH_ERODE, MORPH_DILATE, MORPH_OPEN, MORPH_CLOSE, MORPH_GRADIENT, MORPH_TOPHAT, MORPH_BLACKHAT)));
  96. class RL_Identical_Result : public RLTestBase, public ::testing::TestWithParam<RLMParams>
  97. {
  98. public:
  99. RL_Identical_Result() { }
  100. protected:
  101. virtual void SetUp() { setUp_impl(); }
  102. };
  103. TEST_P(RL_Identical_Result, erosion_no_boundary)
  104. {
  105. Mat resPix, resRLE;
  106. RLMParams param = GetParam();
  107. cv::MorphTypes elementType = get<0>(param);
  108. int nSize = get<1>(param);
  109. int image = get<2>(param);
  110. Mat element = getStructuringElement(elementType, Size(nSize * 2 + 1, nSize * 2 + 1),
  111. Point(nSize, nSize));
  112. erode(test_image[image], resPix, element, cv::Point(-1,-1), 1, BORDER_CONSTANT, cv::Scalar(0));
  113. Mat elementRLE = rl::getStructuringElement(elementType, Size(nSize * 2 + 1, nSize * 2 + 1));
  114. rl::erode(test_image_rle[image], resRLE, elementRLE, false);
  115. ASSERT_TRUE(areImagesIdentical(resPix, resRLE));
  116. }
  117. TEST_P(RL_Identical_Result, erosion_with_offset)
  118. {
  119. Mat resPix, resRLE;
  120. RLMParams param = GetParam();
  121. cv::MorphTypes elementType = get<0>(param);
  122. int nSize = get<1>(param);
  123. int image = get<2>(param);
  124. int nOffset = nSize - 1;
  125. Mat element = getStructuringElement(elementType, Size(nSize * 2 + 1, nSize * 2 + 1),
  126. Point(nSize, nSize));
  127. erode(test_image[image], resPix, element, cv::Point(nSize + nOffset, nSize + nOffset));
  128. Mat elementRLE = rl::getStructuringElement(elementType, Size(nSize * 2 + 1, nSize * 2 + 1));
  129. rl::erode(test_image_rle[image], resRLE, elementRLE, true, Point(nOffset, nOffset));
  130. ASSERT_TRUE(areImagesIdentical(resPix, resRLE));
  131. }
  132. TEST_P(RL_Identical_Result, dilation_with_offset)
  133. {
  134. Mat resPix, resRLE;
  135. RLMParams param = GetParam();
  136. cv::MorphTypes elementType = get<0>(param);
  137. int nSize = get<1>(param);
  138. int image = get<2>(param);
  139. int nOffset = nSize - 1;
  140. Mat element = getStructuringElement(elementType, Size(nSize * 2 + 1, nSize * 2 + 1),
  141. Point(nSize, nSize));
  142. dilate(test_image[image], resPix, element, cv::Point(nSize + nOffset, nSize + nOffset));
  143. Mat elementRLE = rl::getStructuringElement(elementType, Size(nSize * 2 + 1, nSize * 2 + 1));
  144. rl::dilate(test_image_rle[image], resRLE, elementRLE, Point(nOffset, nOffset));
  145. ASSERT_TRUE(areImagesIdentical(resPix, resRLE));
  146. }
  147. INSTANTIATE_TEST_CASE_P(TypicalSET, RL_Identical_Result, Combine(Values(MORPH_RECT, MORPH_CROSS, MORPH_ELLIPSE), Values(1,5,11), Values(0,1)));
  148. class RL_CreateCustomKernel : public RLTestBase, public testing::Test
  149. {
  150. public:
  151. RL_CreateCustomKernel() { }
  152. protected:
  153. virtual void SetUp() { setUp_impl(); }
  154. };
  155. TEST_F(RL_CreateCustomKernel, check_valid)
  156. {
  157. // create a diamond
  158. int nSize = 21;
  159. std::vector<Point3i> runs;
  160. for (int i = 0; i < nSize; ++i)
  161. {
  162. runs.emplace_back(Point3i(-i, i, -nSize + i));
  163. runs.emplace_back(Point3i(-i, i, nSize - i));
  164. }
  165. runs.emplace_back(Point3i(-nSize, nSize, 0));
  166. Mat kernel, dest;
  167. rl::createRLEImage(runs, kernel);
  168. ASSERT_TRUE(rl::isRLMorphologyPossible(kernel));
  169. rl::erode(test_image_rle[0], dest, kernel);
  170. //only one row means: no runs, all pixels off
  171. ASSERT_TRUE(dest.rows == 1);
  172. }
  173. typedef tuple<int> RLPParams;
  174. class RL_Paint : public RLTestBase, public ::testing::TestWithParam<RLPParams>
  175. {
  176. public:
  177. RL_Paint() { }
  178. protected:
  179. virtual void SetUp() { setUp_impl(); }
  180. };
  181. TEST_P(RL_Paint, same_result)
  182. {
  183. Mat converted, pixBinary, painted;
  184. RLPParams param = GetParam();
  185. int rType = get<0>(param);
  186. double dThreshold = 100.0;
  187. double dMaxValue = 105.0;
  188. test_image[1].convertTo(converted, rType);
  189. cv::threshold(converted, pixBinary, dThreshold, dMaxValue, THRESH_BINARY);
  190. painted.create(test_image[1].rows, test_image[1].cols, rType);
  191. painted = cv::Scalar(0.0);
  192. rl::paint(painted, test_image_rle[1], Scalar(dMaxValue));
  193. ASSERT_TRUE(arePixelImagesIdentical(pixBinary, painted));
  194. }
  195. INSTANTIATE_TEST_CASE_P(TypicalSET, RL_Paint, Values(CV_8U, CV_16U, CV_16S, CV_32F, CV_64F));
  196. }
  197. }