Path: blob/master/modules/features2d/test/test_mser.cpp
16354 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include "test_precomp.hpp"4344namespace opencv_test { namespace {4546#undef RENDER_MSERS47#define RENDER_MSERS 04849#if defined RENDER_MSERS && RENDER_MSERS50static void renderMSERs(const Mat& gray, Mat& img, const vector<vector<Point> >& msers)51{52cvtColor(gray, img, COLOR_GRAY2BGR);53RNG rng((uint64)1749583);54for( int i = 0; i < (int)msers.size(); i++ )55{56uchar b = rng.uniform(0, 256);57uchar g = rng.uniform(0, 256);58uchar r = rng.uniform(0, 256);59Vec3b color(b, g, r);6061const Point* pt = &msers[i][0];62size_t j, n = msers[i].size();63for( j = 0; j < n; j++ )64img.at<Vec3b>(pt[j]) = color;65}66}67#endif6869TEST(Features2d_MSER, cases)70{71uchar buf[] =72{73255, 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,74255, 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,75255, 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,76255, 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,77255, 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,78255, 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,79255, 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,80255, 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,81255, 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,82255, 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,83255, 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,84255, 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,85255, 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,86255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 25587};88Mat big_image = imread(cvtest::TS::ptr()->get_data_path() + "mser/puzzle.png", 0);89Mat small_image(14, 26, CV_8U, buf);90static const int thresharr[] = { 0, 70, 120, 180, 255 };9192const int kDelta = 5;93Ptr<MSER> mserExtractor = MSER::create( kDelta );94vector<vector<Point> > msers;95vector<Rect> boxes;9697RNG rng((uint64)123456);9899for( int i = 0; i < 100; i++ )100{101bool use_big_image = rng.uniform(0, 7) != 0;102bool invert = rng.uniform(0, 2) != 0;103bool binarize = use_big_image ? rng.uniform(0, 5) != 0 : false;104bool blur = rng.uniform(0, 2) != 0;105int thresh = thresharr[rng.uniform(0, 5)];106107/*if( i == 0 )108{109use_big_image = true;110invert = binarize = blur = false;111}*/112113const Mat& src0 = use_big_image ? big_image : small_image;114Mat src = src0.clone();115116int kMinArea = use_big_image ? 256 : 10;117int kMaxArea = (int)src.total()/4;118119mserExtractor->setMinArea(kMinArea);120mserExtractor->setMaxArea(kMaxArea);121122if( invert )123bitwise_not(src, src);124if( binarize )125cv::threshold(src, src, thresh, 255, THRESH_BINARY);126if( blur )127GaussianBlur(src, src, Size(5, 5), 1.5, 1.5);128129int minRegs = use_big_image ? 7 : 2;130int maxRegs = use_big_image ? 1000 : 20;131if( binarize && (thresh == 0 || thresh == 255) )132minRegs = maxRegs = 0;133134mserExtractor->detectRegions( src, msers, boxes );135int nmsers = (int)msers.size();136ASSERT_EQ(nmsers, (int)boxes.size());137138if( maxRegs < nmsers || minRegs > nmsers )139{140printf("%d. minArea=%d, maxArea=%d, nmsers=%d, minRegs=%d, maxRegs=%d, "141"image=%s, invert=%d, binarize=%d, thresh=%d, blur=%d\n",142i, kMinArea, kMaxArea, nmsers, minRegs, maxRegs, use_big_image ? "big" : "small",143(int)invert, (int)binarize, thresh, (int)blur);144#if defined RENDER_MSERS && RENDER_MSERS145Mat image;146imshow("source", src);147renderMSERs(src, image, msers);148imshow("result", image);149waitKey();150#endif151}152153ASSERT_LE(minRegs, nmsers);154ASSERT_GE(maxRegs, nmsers);155}156}157158TEST(Features2d_MSER, history_update_regression)159{160String dataPath = cvtest::TS::ptr()->get_data_path() + "mser/";161vector<Mat> tstImages;162tstImages.push_back(imread(dataPath + "mser_test.png", IMREAD_GRAYSCALE));163tstImages.push_back(imread(dataPath + "mser_test2.png", IMREAD_GRAYSCALE));164165for(size_t j = 0; j < tstImages.size(); j++)166{167size_t previous_size = 0;168for(int minArea = 100; minArea > 10; minArea--)169{170Ptr<MSER> mser = MSER::create(1, minArea, (int)(tstImages[j].cols * tstImages[j].rows * 0.2));171mser->setPass2Only(true);172vector<vector<Point> > mserContours;173vector<Rect> boxRects;174mser->detectRegions(tstImages[j], mserContours, boxRects);175ASSERT_LE(previous_size, mserContours.size());176previous_size = mserContours.size();177}178}179}180181}} // namespace182183184