diff --git a/modules/features2d/include/opencv2/features2d.hpp b/modules/features2d/include/opencv2/features2d.hpp index 343ffd81e4..dec2ffcc61 100644 --- a/modules/features2d/include/opencv2/features2d.hpp +++ b/modules/features2d/include/opencv2/features2d.hpp @@ -524,6 +524,9 @@ public: CV_WRAP virtual void setMaxArea(int maxArea) = 0; CV_WRAP virtual int getMaxArea() const = 0; + CV_WRAP virtual void setMinDiversity(double minDiversity) = 0; + CV_WRAP virtual double getMinDiversity() const = 0; + CV_WRAP virtual void setPass2Only(bool f) = 0; CV_WRAP virtual bool getPass2Only() const = 0; CV_WRAP virtual String getDefaultName() const CV_OVERRIDE; diff --git a/modules/features2d/src/mser.cpp b/modules/features2d/src/mser.cpp index 4fe07bd6eb..c9b2e1be90 100644 --- a/modules/features2d/src/mser.cpp +++ b/modules/features2d/src/mser.cpp @@ -96,6 +96,9 @@ public: void setMaxArea(int maxArea) CV_OVERRIDE { params.maxArea = maxArea; } int getMaxArea() const CV_OVERRIDE { return params.maxArea; } + void setMinDiversity(double minDiversity) CV_OVERRIDE { params.minDiversity = minDiversity; } + double getMinDiversity() const CV_OVERRIDE { return params.minDiversity; } + void setPass2Only(bool f) CV_OVERRIDE { params.pass2Only = f; } bool getPass2Only() const CV_OVERRIDE { return params.pass2Only; } @@ -200,7 +203,7 @@ public: if( checked ) return; checked = true; - if( size < wp.p.minArea || size > wp.p.maxArea || var < 0.f || var > wp.p.maxVariation ) + if( size < wp.p.minArea || size > wp.p.maxArea || var < wp.p.minDiversity || var > wp.p.maxVariation ) return; if( child_ ) { diff --git a/modules/features2d/test/test_mser.cpp b/modules/features2d/test/test_mser.cpp index d0691083d1..9f7e35dc41 100644 --- a/modules/features2d/test/test_mser.cpp +++ b/modules/features2d/test/test_mser.cpp @@ -119,6 +119,7 @@ TEST(Features2d_MSER, cases) mserExtractor->setMinArea(kMinArea); mserExtractor->setMaxArea(kMaxArea); + mserExtractor->setMinDiversity(0); if( invert ) bitwise_not(src, src); @@ -170,6 +171,7 @@ TEST(Features2d_MSER, history_update_regression) { Ptr mser = MSER::create(1, minArea, (int)(tstImages[j].cols * tstImages[j].rows * 0.2)); mser->setPass2Only(true); + mser->setMinDiversity(0); vector > mserContours; vector boxRects; mser->detectRegions(tstImages[j], mserContours, boxRects); @@ -179,4 +181,28 @@ TEST(Features2d_MSER, history_update_regression) } } + +TEST(Features2d_MSER, bug_5630) +{ + String dataPath = cvtest::TS::ptr()->get_data_path() + "mser/"; + Mat img = imread(dataPath + "mser_test.png", IMREAD_GRAYSCALE); + Ptr mser = MSER::create(1, 1); + vector > mserContours; + vector boxRects; + + // set min diversity and run detection + mser->setMinDiversity(0.1); + mser->detectRegions(img, mserContours, boxRects); + size_t originalNumberOfContours = mserContours.size(); + + // increase min diversity and run detection again + mser->setMinDiversity(0.2); + mser->detectRegions(img, mserContours, boxRects); + size_t newNumberOfContours = mserContours.size(); + + // there should be fewer regions detected with a higher min diversity + ASSERT_LT(newNumberOfContours, originalNumberOfContours); + +} + }} // namespace diff --git a/modules/python/test/test_mser.py b/modules/python/test/test_mser.py index c76e9d4c79..37c9e1dfe8 100644 --- a/modules/python/test/test_mser.py +++ b/modules/python/test/test_mser.py @@ -35,6 +35,7 @@ class mser_test(NewOpenCVTests): kDelta = 5 mserExtractor = cv.MSER_create() mserExtractor.setDelta(kDelta) + mserExtractor.setMinDiversity(0) np.random.seed(10) for _i in range(100):