test_mser.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. namespace opencv_test { namespace {
  44. #undef RENDER_MSERS
  45. #define RENDER_MSERS 0
  46. #if defined RENDER_MSERS && RENDER_MSERS
  47. static void renderMSERs(const Mat& gray, Mat& img, const vector<vector<Point> >& msers)
  48. {
  49. cvtColor(gray, img, COLOR_GRAY2BGR);
  50. RNG rng((uint64)1749583);
  51. for( int i = 0; i < (int)msers.size(); i++ )
  52. {
  53. uchar b = rng.uniform(0, 256);
  54. uchar g = rng.uniform(0, 256);
  55. uchar r = rng.uniform(0, 256);
  56. Vec3b color(b, g, r);
  57. const Point* pt = &msers[i][0];
  58. size_t j, n = msers[i].size();
  59. for( j = 0; j < n; j++ )
  60. img.at<Vec3b>(pt[j]) = color;
  61. }
  62. }
  63. #endif
  64. TEST(Features2d_MSER, cases)
  65. {
  66. uchar buf[] =
  67. {
  68. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  69. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  70. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  71. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  72. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  73. 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255,
  74. 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255,
  75. 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255,
  76. 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255,
  77. 255, 255, 255, 255, 255, 255, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 255, 255, 255, 255, 255,
  78. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  79. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  80. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  81. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
  82. };
  83. Mat big_image = imread(cvtest::TS::ptr()->get_data_path() + "mser/puzzle.png", 0);
  84. Mat small_image(14, 26, CV_8U, buf);
  85. static const int thresharr[] = { 0, 70, 120, 180, 255 };
  86. const int kDelta = 5;
  87. Ptr<MSER> mserExtractor = MSER::create( kDelta );
  88. vector<vector<Point> > msers;
  89. vector<Rect> boxes;
  90. RNG rng((uint64)123456);
  91. for( int i = 0; i < 100; i++ )
  92. {
  93. bool use_big_image = rng.uniform(0, 7) != 0;
  94. bool invert = rng.uniform(0, 2) != 0;
  95. bool binarize = use_big_image ? rng.uniform(0, 5) != 0 : false;
  96. bool blur = rng.uniform(0, 2) != 0;
  97. int thresh = thresharr[rng.uniform(0, 5)];
  98. /*if( i == 0 )
  99. {
  100. use_big_image = true;
  101. invert = binarize = blur = false;
  102. }*/
  103. const Mat& src0 = use_big_image ? big_image : small_image;
  104. Mat src = src0.clone();
  105. int kMinArea = use_big_image ? 256 : 10;
  106. int kMaxArea = (int)src.total()/4;
  107. mserExtractor->setMinArea(kMinArea);
  108. mserExtractor->setMaxArea(kMaxArea);
  109. if( invert )
  110. bitwise_not(src, src);
  111. if( binarize )
  112. cv::threshold(src, src, thresh, 255, THRESH_BINARY);
  113. if( blur )
  114. GaussianBlur(src, src, Size(5, 5), 1.5, 1.5);
  115. int minRegs = use_big_image ? 7 : 2;
  116. int maxRegs = use_big_image ? 1000 : 20;
  117. if( binarize && (thresh == 0 || thresh == 255) )
  118. minRegs = maxRegs = 0;
  119. mserExtractor->detectRegions( src, msers, boxes );
  120. int nmsers = (int)msers.size();
  121. ASSERT_EQ(nmsers, (int)boxes.size());
  122. if( maxRegs < nmsers || minRegs > nmsers )
  123. {
  124. printf("%d. minArea=%d, maxArea=%d, nmsers=%d, minRegs=%d, maxRegs=%d, "
  125. "image=%s, invert=%d, binarize=%d, thresh=%d, blur=%d\n",
  126. i, kMinArea, kMaxArea, nmsers, minRegs, maxRegs, use_big_image ? "big" : "small",
  127. (int)invert, (int)binarize, thresh, (int)blur);
  128. #if defined RENDER_MSERS && RENDER_MSERS
  129. Mat image;
  130. imshow("source", src);
  131. renderMSERs(src, image, msers);
  132. imshow("result", image);
  133. waitKey();
  134. #endif
  135. }
  136. ASSERT_LE(minRegs, nmsers);
  137. ASSERT_GE(maxRegs, nmsers);
  138. }
  139. }
  140. TEST(Features2d_MSER, history_update_regression)
  141. {
  142. String dataPath = cvtest::TS::ptr()->get_data_path() + "mser/";
  143. vector<Mat> tstImages;
  144. tstImages.push_back(imread(dataPath + "mser_test.png", IMREAD_GRAYSCALE));
  145. tstImages.push_back(imread(dataPath + "mser_test2.png", IMREAD_GRAYSCALE));
  146. for(size_t j = 0; j < tstImages.size(); j++)
  147. {
  148. size_t previous_size = 0;
  149. for(int minArea = 100; minArea > 10; minArea--)
  150. {
  151. Ptr<MSER> mser = MSER::create(1, minArea, (int)(tstImages[j].cols * tstImages[j].rows * 0.2));
  152. mser->setPass2Only(true);
  153. vector<vector<Point> > mserContours;
  154. vector<Rect> boxRects;
  155. mser->detectRegions(tstImages[j], mserContours, boxRects);
  156. ASSERT_LE(previous_size, mserContours.size());
  157. previous_size = mserContours.size();
  158. }
  159. }
  160. }
  161. }} // namespace