add logic for handling min diversity in mser

This commit is contained in:
danielenricocahall 2021-05-27 19:16:24 -04:00
parent 125b9f6057
commit fa73b91e39
4 changed files with 34 additions and 1 deletions

View File

@ -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;

View File

@ -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_ )
{

View File

@ -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 = MSER::create(1, minArea, (int)(tstImages[j].cols * tstImages[j].rows * 0.2));
mser->setPass2Only(true);
mser->setMinDiversity(0);
vector<vector<Point> > mserContours;
vector<Rect> 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 = MSER::create(1, 1);
vector<vector<Point> > mserContours;
vector<Rect> 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

View File

@ -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):