Example BLOB_MSER
This commit is contained in:
parent
6b9e273f92
commit
171b58e2ca
@ -9,7 +9,7 @@ static void help()
|
|||||||
{
|
{
|
||||||
cout << "\n This program demonstrates how to use BLOB and MSER to detect region \n"
|
cout << "\n This program demonstrates how to use BLOB and MSER to detect region \n"
|
||||||
"Usage: \n"
|
"Usage: \n"
|
||||||
" ./BLOB_MSER <image1(../data/basketball1.png as default)>\n"
|
" ./BLOB_MSER <image1(../data/forme2.jpg as default)>\n"
|
||||||
"Press a key when image window is active to change descriptor";
|
"Press a key when image window is active to change descriptor";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,6 +45,53 @@ struct MSERParams
|
|||||||
int edgeBlurSize;
|
int edgeBlurSize;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
String Legende(SimpleBlobDetector::Params &pAct)
|
||||||
|
{
|
||||||
|
String s="";
|
||||||
|
if (pAct.filterByArea)
|
||||||
|
{
|
||||||
|
String inf = static_cast<ostringstream*>(&(ostringstream() << pAct.minArea))->str();
|
||||||
|
String sup = static_cast<ostringstream*>(&(ostringstream() << pAct.maxArea))->str();
|
||||||
|
s = " Area range [" + inf + " to " + sup + "]";
|
||||||
|
}
|
||||||
|
if (pAct.filterByCircularity)
|
||||||
|
{
|
||||||
|
String inf = static_cast<ostringstream*>(&(ostringstream() << pAct.minCircularity))->str();
|
||||||
|
String sup = static_cast<ostringstream*>(&(ostringstream() << pAct.maxCircularity))->str();
|
||||||
|
if (s.length()==0)
|
||||||
|
s = " Circularity range [" + inf + " to " + sup + "]";
|
||||||
|
else
|
||||||
|
s += " AND Circularity range [" + inf + " to " + sup + "]";
|
||||||
|
}
|
||||||
|
if (pAct.filterByColor)
|
||||||
|
{
|
||||||
|
String inf = static_cast<ostringstream*>(&(ostringstream() << pAct.blobColor))->str();
|
||||||
|
if (s.length() == 0)
|
||||||
|
s = " Blob color " + inf;
|
||||||
|
else
|
||||||
|
s += " AND Blob color " + inf;
|
||||||
|
}
|
||||||
|
if (pAct.filterByConvexity)
|
||||||
|
{
|
||||||
|
String inf = static_cast<ostringstream*>(&(ostringstream() << pAct.minConvexity))->str();
|
||||||
|
String sup = static_cast<ostringstream*>(&(ostringstream() << pAct.maxConvexity))->str();
|
||||||
|
if (s.length() == 0)
|
||||||
|
s = " Convexity range[" + inf + " to " + sup + "]";
|
||||||
|
else
|
||||||
|
s += " AND Convexity range[" + inf + " to " + sup + "]";
|
||||||
|
}
|
||||||
|
if (pAct.filterByInertia)
|
||||||
|
{
|
||||||
|
String inf = static_cast<ostringstream*>(&(ostringstream() << pAct.minInertiaRatio))->str();
|
||||||
|
String sup = static_cast<ostringstream*>(&(ostringstream() << pAct.maxInertiaRatio))->str();
|
||||||
|
if (s.length() == 0)
|
||||||
|
s = " Inertia ratio range [" + inf + " to " + sup + "]";
|
||||||
|
else
|
||||||
|
s += " AND Inertia ratio range [" + inf + " to " + sup + "]";
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
@ -52,7 +99,7 @@ int main(int argc, char *argv[])
|
|||||||
vector<String> fileName;
|
vector<String> fileName;
|
||||||
if (argc == 1)
|
if (argc == 1)
|
||||||
{
|
{
|
||||||
fileName.push_back("../data/forme.jpg");
|
fileName.push_back("../data/BLOB_MSER.bmp");
|
||||||
}
|
}
|
||||||
else if (argc == 2)
|
else if (argc == 2)
|
||||||
{
|
{
|
||||||
@ -69,11 +116,13 @@ int main(int argc, char *argv[])
|
|||||||
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),5,5);
|
GaussianBlur(imgOrig,img,Size(11,11),0.1,0.1);
|
||||||
|
|
||||||
SimpleBlobDetector::Params pDefaultBLOB;
|
SimpleBlobDetector::Params pDefaultBLOB;
|
||||||
|
MSERParams pDefaultMSER;
|
||||||
// This is default parameters for SimpleBlobDetector
|
// This is default parameters for SimpleBlobDetector
|
||||||
pDefaultBLOB.thresholdStep = 10;
|
pDefaultBLOB.thresholdStep = 10;
|
||||||
pDefaultBLOB.minThreshold = 1;
|
pDefaultBLOB.minThreshold = 10;
|
||||||
pDefaultBLOB.maxThreshold = 220;
|
pDefaultBLOB.maxThreshold = 220;
|
||||||
pDefaultBLOB.minRepeatability = 2;
|
pDefaultBLOB.minRepeatability = 2;
|
||||||
pDefaultBLOB.minDistBetweenBlobs = 10;
|
pDefaultBLOB.minDistBetweenBlobs = 10;
|
||||||
@ -83,7 +132,7 @@ int main(int argc, char *argv[])
|
|||||||
pDefaultBLOB.minArea = 25;
|
pDefaultBLOB.minArea = 25;
|
||||||
pDefaultBLOB.maxArea = 5000;
|
pDefaultBLOB.maxArea = 5000;
|
||||||
pDefaultBLOB.filterByCircularity = false;
|
pDefaultBLOB.filterByCircularity = false;
|
||||||
pDefaultBLOB.minCircularity = 0.8f;
|
pDefaultBLOB.minCircularity = 0.9f;
|
||||||
pDefaultBLOB.maxCircularity = std::numeric_limits<float>::max();
|
pDefaultBLOB.maxCircularity = std::numeric_limits<float>::max();
|
||||||
pDefaultBLOB.filterByInertia = false;
|
pDefaultBLOB.filterByInertia = false;
|
||||||
pDefaultBLOB.minInertiaRatio = 0.1f;
|
pDefaultBLOB.minInertiaRatio = 0.1f;
|
||||||
@ -91,7 +140,6 @@ int main(int argc, char *argv[])
|
|||||||
pDefaultBLOB.filterByConvexity = false;
|
pDefaultBLOB.filterByConvexity = false;
|
||||||
pDefaultBLOB.minConvexity = 0.95f;
|
pDefaultBLOB.minConvexity = 0.95f;
|
||||||
pDefaultBLOB.maxConvexity = std::numeric_limits<float>::max();
|
pDefaultBLOB.maxConvexity = std::numeric_limits<float>::max();
|
||||||
MSERParams pDefaultMSER;
|
|
||||||
// Descriptor array (BLOB or MSER)
|
// Descriptor array (BLOB or MSER)
|
||||||
vector<String> typeDesc;
|
vector<String> typeDesc;
|
||||||
// Param array for BLOB
|
// Param array for BLOB
|
||||||
@ -99,35 +147,58 @@ int main(int argc, char *argv[])
|
|||||||
vector<SimpleBlobDetector::Params>::iterator itBLOB;
|
vector<SimpleBlobDetector::Params>::iterator itBLOB;
|
||||||
// Param array for MSER
|
// Param array for MSER
|
||||||
vector<MSERParams> pMSER;
|
vector<MSERParams> pMSER;
|
||||||
|
vector<MSERParams>::iterator itMSER;
|
||||||
|
|
||||||
// Color palette
|
// Color palette
|
||||||
vector<Vec3b> palette;
|
vector<Vec3b> palette;
|
||||||
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");
|
||||||
|
pMSER.push_back(pDefaultMSER);
|
||||||
|
pMSER.back().minArea = 1;
|
||||||
|
pMSER.back().maxArea = img.rows*img.cols;
|
||||||
|
|
||||||
|
typeDesc.push_back("BLOB");
|
||||||
|
pBLOB.push_back(pDefaultBLOB);
|
||||||
|
pBLOB.back().filterByColor = true;
|
||||||
|
pBLOB.back().blobColor = 255;
|
||||||
// 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
|
||||||
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
|
||||||
pBLOB.push_back(pDefaultBLOB);
|
pBLOB.push_back(pDefaultBLOB);
|
||||||
pBLOB.back().filterByArea = true;
|
pBLOB.back().filterByArea = true;
|
||||||
pBLOB.back().minArea = 1;
|
pBLOB.back().minArea = 1;
|
||||||
pBLOB.back().maxArea = img.rows*img.cols;
|
pBLOB.back().maxArea = img.rows*img.cols;
|
||||||
|
// Param for second BLOB detector we want area between 500 and 2900 pixels
|
||||||
typeDesc.push_back("BLOB");
|
typeDesc.push_back("BLOB");
|
||||||
pBLOB.push_back(pDefaultBLOB);
|
pBLOB.push_back(pDefaultBLOB);
|
||||||
pBLOB.back().filterByArea = true;
|
pBLOB.back().filterByArea = true;
|
||||||
pBLOB.back().maxArea = img.rows*img.cols;
|
pBLOB.back().minArea = 500;
|
||||||
pBLOB.back().filterByCircularity = true;
|
pBLOB.back().maxArea = 2900;
|
||||||
|
// Param for third BLOB detector we want only circular object
|
||||||
typeDesc.push_back("BLOB");
|
typeDesc.push_back("BLOB");
|
||||||
pBLOB.push_back(pDefaultBLOB);
|
pBLOB.push_back(pDefaultBLOB);
|
||||||
|
pBLOB.back().filterByCircularity = true;
|
||||||
|
// Param for Fourth BLOB detector we want ratio inertia
|
||||||
|
typeDesc.push_back("BLOB");
|
||||||
|
pBLOB.push_back(pDefaultBLOB);
|
||||||
pBLOB.back().filterByInertia = true;
|
pBLOB.back().filterByInertia = true;
|
||||||
typeDesc.push_back("BLOB");
|
pBLOB.back().minInertiaRatio = 0;
|
||||||
|
pBLOB.back().maxInertiaRatio = 0.2;
|
||||||
|
// Param for Fourth BLOB detector we want ratio inertia
|
||||||
|
typeDesc.push_back("BLOB");
|
||||||
pBLOB.push_back(pDefaultBLOB);
|
pBLOB.push_back(pDefaultBLOB);
|
||||||
pBLOB.back().filterByColor = true;
|
pBLOB.back().filterByConvexity = true;
|
||||||
pBLOB.back().blobColor = 60;
|
pBLOB.back().minConvexity = 0.;
|
||||||
typeDesc.push_back("MSER");
|
pBLOB.back().maxConvexity = 0.9;
|
||||||
itBLOB=pBLOB.begin();
|
|
||||||
|
|
||||||
|
itBLOB = pBLOB.begin();
|
||||||
|
itMSER = pMSER.begin();
|
||||||
vector<double> desMethCmp;
|
vector<double> desMethCmp;
|
||||||
Ptr<Feature2D> b;
|
Ptr<Feature2D> b;
|
||||||
|
String label;
|
||||||
// Descriptor loop
|
// Descriptor loop
|
||||||
vector<String>::iterator itDesc;
|
vector<String>::iterator itDesc;
|
||||||
for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); itDesc++)
|
for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); itDesc++)
|
||||||
@ -135,11 +206,14 @@ int main(int argc, char *argv[])
|
|||||||
vector<KeyPoint> keyImg1;
|
vector<KeyPoint> keyImg1;
|
||||||
if (*itDesc == "BLOB"){
|
if (*itDesc == "BLOB"){
|
||||||
b = SimpleBlobDetector::create(*itBLOB);
|
b = SimpleBlobDetector::create(*itBLOB);
|
||||||
|
label=Legende(*itBLOB);
|
||||||
|
|
||||||
itBLOB++;
|
itBLOB++;
|
||||||
}
|
}
|
||||||
if (*itDesc == "MSER"){
|
if (*itDesc == "MSER"){
|
||||||
b = MSER::create();
|
b = MSER::create(itMSER->delta, itMSER->minArea,itMSER->maxArea,itMSER->maxVariation,itMSER->minDiversity,itMSER->maxEvolution,
|
||||||
}
|
itMSER->areaThreshold,itMSER->minMargin,itMSER->edgeBlurSize);
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
// We can detect keypoint with detect method
|
// We can detect keypoint with detect method
|
||||||
vector<KeyPoint> keyImg;
|
vector<KeyPoint> keyImg;
|
||||||
@ -167,7 +241,6 @@ int main(int argc, char *argv[])
|
|||||||
int i=0;
|
int i=0;
|
||||||
for (vector<KeyPoint>::iterator k=keyImg.begin();k!=keyImg.end();k++,i++)
|
for (vector<KeyPoint>::iterator k=keyImg.begin();k!=keyImg.end();k++,i++)
|
||||||
circle(result,k->pt,k->size,palette[i%65536]);
|
circle(result,k->pt,k->size,palette[i%65536]);
|
||||||
|
|
||||||
}
|
}
|
||||||
if (b.dynamicCast<MSER>() != NULL)
|
if (b.dynamicCast<MSER>() != NULL)
|
||||||
{
|
{
|
||||||
@ -194,12 +267,12 @@ int main(int argc, char *argv[])
|
|||||||
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) += palette[i % 65536];
|
result.at<Vec3b>(itp->y, itp->x) = Vec3b(0,255,255);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
namedWindow(*itDesc , WINDOW_AUTOSIZE);
|
namedWindow(*itDesc+label , WINDOW_AUTOSIZE);
|
||||||
imshow(*itDesc, result);
|
imshow(*itDesc + label, result);
|
||||||
imshow("Original", img);
|
imshow("Original", img);
|
||||||
FileStorage fs(*itDesc + "_" + fileName[0] + ".xml", FileStorage::WRITE);
|
FileStorage fs(*itDesc + "_" + fileName[0] + ".xml", FileStorage::WRITE);
|
||||||
fs<<*itDesc<<keyImg;
|
fs<<*itDesc<<keyImg;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user