example BLOB_MSER and fixed bug in blobdetector

pull/4022/head
laurentBerger 10 years ago
parent 171b58e2ca
commit 3a53e726dd
  1. 4
      modules/features2d/src/blobdetector.cpp
  2. 65
      samples/cpp/BLOB_MSER.cpp

@ -266,6 +266,8 @@ void SimpleBlobDetectorImpl::findBlobs(InputArray _image, InputArray _binaryImag
continue; continue;
} }
if(moms.m00 == 0.0)
continue;
center.location = Point2d(moms.m10 / moms.m00, moms.m01 / moms.m00); center.location = Point2d(moms.m10 / moms.m00, moms.m01 / moms.m00);
if (params.filterByColor) 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.; center.radius = (dists[(dists.size() - 1) / 2] + dists[dists.size() / 2]) / 2.;
} }
if(moms.m00 == 0.0)
continue;
centers.push_back(center); centers.push_back(center);

@ -110,13 +110,12 @@ int main(int argc, char *argv[])
help(); help();
return(0); return(0);
} }
Mat imgOrig = imread(fileName[0], IMREAD_UNCHANGED),img; Mat img = imread(fileName[0], IMREAD_GRAYSCALE);
if (imgOrig.rows*imgOrig.cols <= 0) if (img.rows*img.cols <= 0)
{ {
cout << "Image " << fileName[0] << " is empty or cannot be found\n"; cout << "Image " << fileName[0] << " is empty or cannot be found\n";
return(0); return(0);
} }
GaussianBlur(imgOrig,img,Size(11,11),0.1,0.1);
SimpleBlobDetector::Params pDefaultBLOB; SimpleBlobDetector::Params pDefaultBLOB;
MSERParams pDefaultMSER; MSERParams pDefaultMSER;
@ -154,15 +153,23 @@ int main(int argc, char *argv[])
for (int i=0;i<65536;i++) for (int i=0;i<65536;i++)
palette.push_back(Vec3b(rand(),rand(),rand())); palette.push_back(Vec3b(rand(),rand(),rand()));
help(); help();
typeDesc.push_back("MSER"); typeDesc.push_back("MSER");
pMSER.push_back(pDefaultMSER); pMSER.push_back(pDefaultMSER);
pMSER.back().minArea = 1; pMSER.back().delta=0;
pMSER.back().maxArea = img.rows*img.cols; 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"); typeDesc.push_back("BLOB");
pBLOB.push_back(pDefaultBLOB); pBLOB.push_back(pDefaultBLOB);
pBLOB.back().filterByColor = true; 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 // This descriptor are going to be detect and compute 4 BLOBS with 4 differents params
// Param for first BLOB detector we want all // Param for first BLOB detector we want all
typeDesc.push_back("BLOB"); // see http://docs.opencv.org/trunk/d0/d7a/classcv_1_1SimpleBlobDetector.html 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++; itBLOB++;
} }
if (*itDesc == "MSER"){ if (*itDesc == "MSER"){
b = MSER::create(itMSER->delta, itMSER->minArea,itMSER->maxArea,itMSER->maxVariation,itMSER->minDiversity,itMSER->maxEvolution, b = MSER::create(itMSER->delta, itMSER->minArea, itMSER->maxArea, itMSER->maxVariation, itMSER->minDiversity, itMSER->maxEvolution,
itMSER->areaThreshold,itMSER->minMargin,itMSER->edgeBlurSize); itMSER->areaThreshold, itMSER->minMargin, itMSER->edgeBlurSize);
b.dynamicCast<MSER>()->setPass2Only(true);
//b = MSER::create();
} }
try { try {
// We can detect keypoint with detect method // We can detect keypoint with detect method
vector<KeyPoint> keyImg; vector<KeyPoint> keyImg;
vector<Rect> zone; vector<Rect> zone;
vector<vector <Point>> region; vector<vector <Point>> region;
Mat desc, result; Mat desc, result(img.rows,img.cols,CV_8UC3);
int nb = img.channels(); int nb = img.channels();
if (img.channels() == 3)
{
img.copyTo(result);
}
else
{
vector<Mat> plan;
plan.push_back(img);
plan.push_back(img);
plan.push_back(img);
merge(plan, result);
}
if (b.dynamicCast<SimpleBlobDetector>() != NULL) if (b.dynamicCast<SimpleBlobDetector>() != NULL)
{ {
Ptr<SimpleBlobDetector> sbd = b.dynamicCast<SimpleBlobDetector>(); Ptr<SimpleBlobDetector> sbd = b.dynamicCast<SimpleBlobDetector>();
@ -245,32 +244,24 @@ int main(int argc, char *argv[])
if (b.dynamicCast<MSER>() != NULL) if (b.dynamicCast<MSER>() != NULL)
{ {
Ptr<MSER> sbd = b.dynamicCast<MSER>(); Ptr<MSER> sbd = b.dynamicCast<MSER>();
sbd->detectRegions(img, region, zone); sbd->detectRegions(img, region, zone);
int i = 0; int i = 0;
for (vector<Rect>::iterator r = zone.begin(); r != zone.end();r++,i++) for (vector<Rect>::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; i=0;
for (vector<vector <Point>>::iterator itr = region.begin(); itr != region.end(); itr++, i++) for (vector<vector <Point>>::iterator itr = region.begin(); itr != region.end(); itr++, i++)
{ {
for (vector <Point>::iterator itp = region[i].begin(); itp != region[i].end(); itp++) for (vector <Point>::iterator itp = region[i].begin(); itp != region[i].end(); itp++)
{
result.at<Vec3b>(itp->y, itp->x) = Vec3b(0,0,0);
}
}
i = 0;
for (vector<vector <Point>>::iterator itr = region.begin(); itr != region.end(); itr++, i++)
{ {
for (vector <Point>::iterator itp = region[i].begin(); itp != region[i].end(); itp++) // all pixels belonging to region are red
{ result.at<Vec3b>(itp->y, itp->x) = Vec3b(0,0,128);
result.at<Vec3b>(itp->y, itp->x) = Vec3b(0,255,255);
}
} }
} }
}
namedWindow(*itDesc+label , WINDOW_AUTOSIZE); namedWindow(*itDesc+label , WINDOW_AUTOSIZE);
imshow(*itDesc + label, result); imshow(*itDesc + label, result);
imshow("Original", img); imshow("Original", img);

Loading…
Cancel
Save