2018-02-09 18:24:18 +08:00
|
|
|
#include <opencv2/core.hpp>
|
|
|
|
#include <opencv2/imgproc.hpp>
|
|
|
|
#include <opencv2/highgui.hpp>
|
|
|
|
#include <opencv2/features2d.hpp>
|
2015-05-11 19:13:20 +08:00
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace cv;
|
|
|
|
|
|
|
|
|
2020-02-23 21:38:04 +08:00
|
|
|
static void help(char** argv)
|
2015-05-11 19:13:20 +08:00
|
|
|
{
|
2015-05-13 01:01:05 +08:00
|
|
|
cout << "\n This program demonstrates how to use BLOB to detect and filter region \n"
|
2020-02-23 21:38:04 +08:00
|
|
|
<< "Usage: \n"
|
|
|
|
<< argv[0]
|
|
|
|
<< " <image1(detect_blob.png as default)>\n"
|
|
|
|
<< "Press a key when image window is active to change descriptor";
|
2015-05-11 19:13:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-11 19:45:08 +08:00
|
|
|
static String Legende(SimpleBlobDetector::Params &pAct)
|
2015-05-11 19:13:20 +08:00
|
|
|
{
|
|
|
|
String s = "";
|
|
|
|
if (pAct.filterByArea)
|
|
|
|
{
|
2016-11-21 09:28:59 +08:00
|
|
|
String inf = static_cast<const ostringstream&>(ostringstream() << pAct.minArea).str();
|
|
|
|
String sup = static_cast<const ostringstream&>(ostringstream() << pAct.maxArea).str();
|
2015-05-11 19:13:20 +08:00
|
|
|
s = " Area range [" + inf + " to " + sup + "]";
|
|
|
|
}
|
|
|
|
if (pAct.filterByCircularity)
|
|
|
|
{
|
2016-11-21 09:28:59 +08:00
|
|
|
String inf = static_cast<const ostringstream&>(ostringstream() << pAct.minCircularity).str();
|
|
|
|
String sup = static_cast<const ostringstream&>(ostringstream() << pAct.maxCircularity).str();
|
2015-05-11 19:13:20 +08:00
|
|
|
if (s.length() == 0)
|
|
|
|
s = " Circularity range [" + inf + " to " + sup + "]";
|
|
|
|
else
|
|
|
|
s += " AND Circularity range [" + inf + " to " + sup + "]";
|
|
|
|
}
|
|
|
|
if (pAct.filterByColor)
|
|
|
|
{
|
2016-11-21 09:28:59 +08:00
|
|
|
String inf = static_cast<const ostringstream&>(ostringstream() << (int)pAct.blobColor).str();
|
2015-05-11 19:13:20 +08:00
|
|
|
if (s.length() == 0)
|
|
|
|
s = " Blob color " + inf;
|
|
|
|
else
|
|
|
|
s += " AND Blob color " + inf;
|
|
|
|
}
|
|
|
|
if (pAct.filterByConvexity)
|
|
|
|
{
|
2016-11-21 09:28:59 +08:00
|
|
|
String inf = static_cast<const ostringstream&>(ostringstream() << pAct.minConvexity).str();
|
|
|
|
String sup = static_cast<const ostringstream&>(ostringstream() << pAct.maxConvexity).str();
|
2015-05-11 19:13:20 +08:00
|
|
|
if (s.length() == 0)
|
|
|
|
s = " Convexity range[" + inf + " to " + sup + "]";
|
|
|
|
else
|
|
|
|
s += " AND Convexity range[" + inf + " to " + sup + "]";
|
|
|
|
}
|
|
|
|
if (pAct.filterByInertia)
|
|
|
|
{
|
2016-11-21 09:28:59 +08:00
|
|
|
String inf = static_cast<const ostringstream&>(ostringstream() << pAct.minInertiaRatio).str();
|
|
|
|
String sup = static_cast<const ostringstream&>(ostringstream() << pAct.maxInertiaRatio).str();
|
2015-05-11 19:13:20 +08:00
|
|
|
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[])
|
|
|
|
{
|
2018-10-31 20:48:56 +08:00
|
|
|
String fileName;
|
|
|
|
cv::CommandLineParser parser(argc, argv, "{@input |detect_blob.png| }{h help | | }");
|
2015-08-01 23:24:23 +08:00
|
|
|
if (parser.has("h"))
|
2015-05-11 19:13:20 +08:00
|
|
|
{
|
2020-02-23 21:38:04 +08:00
|
|
|
help(argv);
|
2015-08-01 23:24:23 +08:00
|
|
|
return 0;
|
2015-05-11 19:13:20 +08:00
|
|
|
}
|
2018-10-31 20:48:56 +08:00
|
|
|
fileName = parser.get<string>("@input");
|
|
|
|
Mat img = imread(samples::findFile(fileName), IMREAD_COLOR);
|
|
|
|
if (img.empty())
|
2015-05-11 19:13:20 +08:00
|
|
|
{
|
2018-10-31 20:48:56 +08:00
|
|
|
cout << "Image " << fileName << " is empty or cannot be found\n";
|
|
|
|
return 1;
|
2015-05-11 19:13:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SimpleBlobDetector::Params pDefaultBLOB;
|
|
|
|
// This is default parameters for SimpleBlobDetector
|
|
|
|
pDefaultBLOB.thresholdStep = 10;
|
|
|
|
pDefaultBLOB.minThreshold = 10;
|
|
|
|
pDefaultBLOB.maxThreshold = 220;
|
|
|
|
pDefaultBLOB.minRepeatability = 2;
|
|
|
|
pDefaultBLOB.minDistBetweenBlobs = 10;
|
|
|
|
pDefaultBLOB.filterByColor = false;
|
|
|
|
pDefaultBLOB.blobColor = 0;
|
|
|
|
pDefaultBLOB.filterByArea = false;
|
|
|
|
pDefaultBLOB.minArea = 25;
|
|
|
|
pDefaultBLOB.maxArea = 5000;
|
|
|
|
pDefaultBLOB.filterByCircularity = false;
|
|
|
|
pDefaultBLOB.minCircularity = 0.9f;
|
2015-05-11 19:45:08 +08:00
|
|
|
pDefaultBLOB.maxCircularity = (float)1e37;
|
2015-05-11 19:13:20 +08:00
|
|
|
pDefaultBLOB.filterByInertia = false;
|
|
|
|
pDefaultBLOB.minInertiaRatio = 0.1f;
|
2015-05-11 19:45:08 +08:00
|
|
|
pDefaultBLOB.maxInertiaRatio = (float)1e37;
|
2015-05-11 19:13:20 +08:00
|
|
|
pDefaultBLOB.filterByConvexity = false;
|
|
|
|
pDefaultBLOB.minConvexity = 0.95f;
|
2015-05-11 19:45:08 +08:00
|
|
|
pDefaultBLOB.maxConvexity = (float)1e37;
|
2015-05-13 01:01:05 +08:00
|
|
|
// Descriptor array for BLOB
|
2015-05-11 19:13:20 +08:00
|
|
|
vector<String> typeDesc;
|
|
|
|
// Param array for BLOB
|
|
|
|
vector<SimpleBlobDetector::Params> pBLOB;
|
|
|
|
vector<SimpleBlobDetector::Params>::iterator itBLOB;
|
|
|
|
// Color palette
|
|
|
|
vector< Vec3b > palette;
|
|
|
|
for (int i = 0; i<65536; i++)
|
|
|
|
{
|
2018-10-31 20:48:56 +08:00
|
|
|
uchar c1 = (uchar)rand();
|
|
|
|
uchar c2 = (uchar)rand();
|
|
|
|
uchar c3 = (uchar)rand();
|
|
|
|
palette.push_back(Vec3b(c1, c2, c3));
|
2015-05-11 19:13:20 +08:00
|
|
|
}
|
2020-02-23 21:38:04 +08:00
|
|
|
help(argv);
|
2015-05-11 19:13:20 +08:00
|
|
|
|
|
|
|
|
2018-02-08 18:51:54 +08:00
|
|
|
// These descriptors are going to be detecting and computing BLOBS with 6 different params
|
2015-05-11 19:13:20 +08:00
|
|
|
// Param for first BLOB detector we want all
|
2021-12-31 00:23:27 +08:00
|
|
|
typeDesc.push_back("BLOB"); // see http://docs.opencv.org/5.x/d0/d7a/classcv_1_1SimpleBlobDetector.html
|
2015-05-11 19:13:20 +08:00
|
|
|
pBLOB.push_back(pDefaultBLOB);
|
|
|
|
pBLOB.back().filterByArea = true;
|
|
|
|
pBLOB.back().minArea = 1;
|
|
|
|
pBLOB.back().maxArea = float(img.rows*img.cols);
|
|
|
|
// Param for second BLOB detector we want area between 500 and 2900 pixels
|
|
|
|
typeDesc.push_back("BLOB");
|
|
|
|
pBLOB.push_back(pDefaultBLOB);
|
|
|
|
pBLOB.back().filterByArea = true;
|
|
|
|
pBLOB.back().minArea = 500;
|
|
|
|
pBLOB.back().maxArea = 2900;
|
|
|
|
// Param for third BLOB detector we want only circular object
|
|
|
|
typeDesc.push_back("BLOB");
|
|
|
|
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().minInertiaRatio = 0;
|
|
|
|
pBLOB.back().maxInertiaRatio = (float)0.2;
|
2015-05-11 19:45:08 +08:00
|
|
|
// Param for fifth BLOB detector we want ratio inertia
|
2015-05-11 19:13:20 +08:00
|
|
|
typeDesc.push_back("BLOB");
|
|
|
|
pBLOB.push_back(pDefaultBLOB);
|
|
|
|
pBLOB.back().filterByConvexity = true;
|
|
|
|
pBLOB.back().minConvexity = 0.;
|
|
|
|
pBLOB.back().maxConvexity = (float)0.9;
|
2018-10-31 20:48:56 +08:00
|
|
|
// Param for six BLOB detector we want blob with gravity center color equal to 0
|
2015-05-11 19:45:08 +08:00
|
|
|
typeDesc.push_back("BLOB");
|
|
|
|
pBLOB.push_back(pDefaultBLOB);
|
|
|
|
pBLOB.back().filterByColor = true;
|
|
|
|
pBLOB.back().blobColor = 0;
|
2015-05-11 19:13:20 +08:00
|
|
|
|
|
|
|
itBLOB = pBLOB.begin();
|
|
|
|
vector<double> desMethCmp;
|
|
|
|
Ptr<Feature2D> b;
|
|
|
|
String label;
|
|
|
|
// Descriptor loop
|
|
|
|
vector<String>::iterator itDesc;
|
2016-04-29 21:41:39 +08:00
|
|
|
for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); ++itDesc)
|
2015-05-11 19:13:20 +08:00
|
|
|
{
|
|
|
|
vector<KeyPoint> keyImg1;
|
|
|
|
if (*itDesc == "BLOB")
|
|
|
|
{
|
|
|
|
b = SimpleBlobDetector::create(*itBLOB);
|
|
|
|
label = Legende(*itBLOB);
|
2016-04-29 21:41:39 +08:00
|
|
|
++itBLOB;
|
2015-05-11 19:13:20 +08:00
|
|
|
}
|
2015-05-11 19:20:52 +08:00
|
|
|
try
|
2015-05-11 19:13:20 +08:00
|
|
|
{
|
|
|
|
// We can detect keypoint with detect method
|
|
|
|
vector<KeyPoint> keyImg;
|
|
|
|
vector<Rect> zone;
|
2015-05-11 19:45:08 +08:00
|
|
|
vector<vector <Point> > region;
|
2015-05-11 19:13:20 +08:00
|
|
|
Mat desc, result(img.rows, img.cols, CV_8UC3);
|
2018-09-09 00:49:11 +08:00
|
|
|
if (b.dynamicCast<SimpleBlobDetector>().get())
|
2015-05-11 19:13:20 +08:00
|
|
|
{
|
|
|
|
Ptr<SimpleBlobDetector> sbd = b.dynamicCast<SimpleBlobDetector>();
|
|
|
|
sbd->detect(img, keyImg, Mat());
|
|
|
|
drawKeypoints(img, keyImg, result);
|
|
|
|
int i = 0;
|
2016-04-29 21:41:39 +08:00
|
|
|
for (vector<KeyPoint>::iterator k = keyImg.begin(); k != keyImg.end(); ++k, ++i)
|
2015-05-11 19:13:20 +08:00
|
|
|
circle(result, k->pt, (int)k->size, palette[i % 65536]);
|
|
|
|
}
|
|
|
|
namedWindow(*itDesc + label, WINDOW_AUTOSIZE);
|
|
|
|
imshow(*itDesc + label, result);
|
|
|
|
imshow("Original", img);
|
|
|
|
waitKey();
|
|
|
|
}
|
2018-10-20 04:21:20 +08:00
|
|
|
catch (const Exception& e)
|
2015-05-11 19:13:20 +08:00
|
|
|
{
|
|
|
|
cout << "Feature : " << *itDesc << "\n";
|
|
|
|
cout << e.msg << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|