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