From 3a53e726dd85a195418531b0e71300051e6cd08a Mon Sep 17 00:00:00 2001 From: laurentBerger Date: Thu, 7 May 2015 11:52:06 +0200 Subject: [PATCH] example BLOB_MSER and fixed bug in blobdetector --- modules/features2d/src/blobdetector.cpp | 4 +- samples/cpp/BLOB_MSER.cpp | 65 +++++++++++-------------- 2 files changed, 30 insertions(+), 39 deletions(-) diff --git a/modules/features2d/src/blobdetector.cpp b/modules/features2d/src/blobdetector.cpp index daf1d36440..e53eff35a4 100644 --- a/modules/features2d/src/blobdetector.cpp +++ b/modules/features2d/src/blobdetector.cpp @@ -266,6 +266,8 @@ void SimpleBlobDetectorImpl::findBlobs(InputArray _image, InputArray _binaryImag continue; } + if(moms.m00 == 0.0) + continue; center.location = Point2d(moms.m10 / moms.m00, moms.m01 / moms.m00); if (params.filterByColor) @@ -286,8 +288,6 @@ void SimpleBlobDetectorImpl::findBlobs(InputArray _image, InputArray _binaryImag center.radius = (dists[(dists.size() - 1) / 2] + dists[dists.size() / 2]) / 2.; } - if(moms.m00 == 0.0) - continue; centers.push_back(center); diff --git a/samples/cpp/BLOB_MSER.cpp b/samples/cpp/BLOB_MSER.cpp index b0206a1d24..41412babae 100644 --- a/samples/cpp/BLOB_MSER.cpp +++ b/samples/cpp/BLOB_MSER.cpp @@ -110,13 +110,12 @@ int main(int argc, char *argv[]) help(); return(0); } - Mat imgOrig = imread(fileName[0], IMREAD_UNCHANGED),img; - if (imgOrig.rows*imgOrig.cols <= 0) + Mat img = imread(fileName[0], IMREAD_GRAYSCALE); + if (img.rows*img.cols <= 0) { cout << "Image " << fileName[0] << " is empty or cannot be found\n"; return(0); } - GaussianBlur(imgOrig,img,Size(11,11),0.1,0.1); SimpleBlobDetector::Params pDefaultBLOB; MSERParams pDefaultMSER; @@ -154,15 +153,23 @@ int main(int argc, char *argv[]) for (int i=0;i<65536;i++) palette.push_back(Vec3b(rand(),rand(),rand())); help(); + typeDesc.push_back("MSER"); pMSER.push_back(pDefaultMSER); - pMSER.back().minArea = 1; - pMSER.back().maxArea = img.rows*img.cols; - + pMSER.back().delta=0; + pMSER.back().minArea = 25; + pMSER.back().maxArea = 80000; + pMSER.back().maxVariation= 0.5; + pMSER.back().minDiversity=0.8; // variation de taille entre deux seuillages ? + pMSER.back().areaThreshold= 200; + pMSER.back().maxEvolution = 1.01; + pMSER.back().minMargin=0.003; + pMSER.back().edgeBlurSize= 5; typeDesc.push_back("BLOB"); pBLOB.push_back(pDefaultBLOB); pBLOB.back().filterByColor = true; - pBLOB.back().blobColor = 255; + pBLOB.back().blobColor = 0; + // This descriptor are going to be detect and compute 4 BLOBS with 4 differents params // Param for first BLOB detector we want all typeDesc.push_back("BLOB"); // see http://docs.opencv.org/trunk/d0/d7a/classcv_1_1SimpleBlobDetector.html @@ -211,28 +218,20 @@ int main(int argc, char *argv[]) itBLOB++; } if (*itDesc == "MSER"){ - b = MSER::create(itMSER->delta, itMSER->minArea,itMSER->maxArea,itMSER->maxVariation,itMSER->minDiversity,itMSER->maxEvolution, - itMSER->areaThreshold,itMSER->minMargin,itMSER->edgeBlurSize); + b = MSER::create(itMSER->delta, itMSER->minArea, itMSER->maxArea, itMSER->maxVariation, itMSER->minDiversity, itMSER->maxEvolution, + itMSER->areaThreshold, itMSER->minMargin, itMSER->edgeBlurSize); + b.dynamicCast()->setPass2Only(true); + //b = MSER::create(); } try { // We can detect keypoint with detect method vector keyImg; vector zone; vector> region; - Mat desc, result; + Mat desc, result(img.rows,img.cols,CV_8UC3); int nb = img.channels(); - if (img.channels() == 3) - { - img.copyTo(result); - } - else - { - vector plan; - plan.push_back(img); - plan.push_back(img); - plan.push_back(img); - merge(plan, result); - } + + if (b.dynamicCast() != NULL) { Ptr sbd = b.dynamicCast(); @@ -245,32 +244,24 @@ int main(int argc, char *argv[]) if (b.dynamicCast() != NULL) { Ptr sbd = b.dynamicCast(); - sbd->detectRegions(img, region, zone); + sbd->detectRegions(img, region, zone); int i = 0; for (vector::iterator r = zone.begin(); r != zone.end();r++,i++) { - rectangle(result, *r, palette[i % 65536],2); + // we draw a white rectangle which include all region pixels + rectangle(result, *r, Vec3b(255, 255, 255), 2); } i=0; for (vector>::iterator itr = region.begin(); itr != region.end(); itr++, i++) - { + { for (vector ::iterator itp = region[i].begin(); itp != region[i].end(); itp++) - { - - result.at(itp->y, itp->x) = Vec3b(0,0,0); - } - } - i = 0; - for (vector>::iterator itr = region.begin(); itr != region.end(); itr++, i++) { - for (vector ::iterator itp = region[i].begin(); itp != region[i].end(); itp++) - { - - result.at(itp->y, itp->x) = Vec3b(0,255,255); - } + // all pixels belonging to region are red + result.at(itp->y, itp->x) = Vec3b(0,0,128); } } + } namedWindow(*itDesc+label , WINDOW_AUTOSIZE); imshow(*itDesc + label, result); imshow("Original", img);