1
#include <opencv2/opencv.hpp>
12
cout << "\n This program demonstrates how to use BLOB to detect and filter region \n"
14
" ./detect_blob <image1(../data/detect_blob.png as default)>\n"
15
"Press a key when image window is active to change descriptor";
19
static String Legende(SimpleBlobDetector::Params &pAct)
22
if (pAct.filterByArea)
24
String inf = static_cast<ostringstream*>(&(ostringstream() << pAct.minArea))->str();
25
String sup = static_cast<ostringstream*>(&(ostringstream() << pAct.maxArea))->str();
26
s = " Area range [" + inf + " to " + sup + "]";
28
if (pAct.filterByCircularity)
30
String inf = static_cast<ostringstream*>(&(ostringstream() << pAct.minCircularity))->str();
31
String sup = static_cast<ostringstream*>(&(ostringstream() << pAct.maxCircularity))->str();
33
s = " Circularity range [" + inf + " to " + sup + "]";
35
s += " AND Circularity range [" + inf + " to " + sup + "]";
37
if (pAct.filterByColor)
39
String inf = static_cast<ostringstream*>(&(ostringstream() << (int)pAct.blobColor))->str();
41
s = " Blob color " + inf;
43
s += " AND Blob color " + inf;
45
if (pAct.filterByConvexity)
47
String inf = static_cast<ostringstream*>(&(ostringstream() << pAct.minConvexity))->str();
48
String sup = static_cast<ostringstream*>(&(ostringstream() << pAct.maxConvexity))->str();
50
s = " Convexity range[" + inf + " to " + sup + "]";
52
s += " AND Convexity range[" + inf + " to " + sup + "]";
54
if (pAct.filterByInertia)
56
String inf = static_cast<ostringstream*>(&(ostringstream() << pAct.minInertiaRatio))->str();
57
String sup = static_cast<ostringstream*>(&(ostringstream() << pAct.maxInertiaRatio))->str();
59
s = " Inertia ratio range [" + inf + " to " + sup + "]";
61
s += " AND Inertia ratio range [" + inf + " to " + sup + "]";
68
int main(int argc, char *argv[])
70
vector<String> fileName;
71
Mat img(600, 800, CV_8UC1);
72
cv::CommandLineParser parser(argc, argv, "{@input |../data/detect_blob.png| }{h help | | }");
78
fileName.push_back(parser.get<string>("@input"));
79
img = imread(fileName[0], IMREAD_COLOR);
80
if (img.rows*img.cols <= 0)
82
cout << "Image " << fileName[0] << " is empty or cannot be found\n";
86
SimpleBlobDetector::Params pDefaultBLOB;
87
// This is default parameters for SimpleBlobDetector
88
pDefaultBLOB.thresholdStep = 10;
89
pDefaultBLOB.minThreshold = 10;
90
pDefaultBLOB.maxThreshold = 220;
91
pDefaultBLOB.minRepeatability = 2;
92
pDefaultBLOB.minDistBetweenBlobs = 10;
93
pDefaultBLOB.filterByColor = false;
94
pDefaultBLOB.blobColor = 0;
95
pDefaultBLOB.filterByArea = false;
96
pDefaultBLOB.minArea = 25;
97
pDefaultBLOB.maxArea = 5000;
98
pDefaultBLOB.filterByCircularity = false;
99
pDefaultBLOB.minCircularity = 0.9f;
100
pDefaultBLOB.maxCircularity = (float)1e37;
101
pDefaultBLOB.filterByInertia = false;
102
pDefaultBLOB.minInertiaRatio = 0.1f;
103
pDefaultBLOB.maxInertiaRatio = (float)1e37;
104
pDefaultBLOB.filterByConvexity = false;
105
pDefaultBLOB.minConvexity = 0.95f;
106
pDefaultBLOB.maxConvexity = (float)1e37;
107
// Descriptor array for BLOB
108
vector<String> typeDesc;
109
// Param array for BLOB
110
vector<SimpleBlobDetector::Params> pBLOB;
111
vector<SimpleBlobDetector::Params>::iterator itBLOB;
113
vector< Vec3b > palette;
114
for (int i = 0; i<65536; i++)
116
palette.push_back(Vec3b((uchar)rand(), (uchar)rand(), (uchar)rand()));
121
// This descriptor are going to be detect and compute BLOBS with 6 differents params
122
// Param for first BLOB detector we want all
123
typeDesc.push_back("BLOB"); // see http://docs.opencv.org/trunk/d0/d7a/classcv_1_1SimpleBlobDetector.html
124
pBLOB.push_back(pDefaultBLOB);
125
pBLOB.back().filterByArea = true;
126
pBLOB.back().minArea = 1;
127
pBLOB.back().maxArea = float(img.rows*img.cols);
128
// Param for second BLOB detector we want area between 500 and 2900 pixels
129
typeDesc.push_back("BLOB");
130
pBLOB.push_back(pDefaultBLOB);
131
pBLOB.back().filterByArea = true;
132
pBLOB.back().minArea = 500;
133
pBLOB.back().maxArea = 2900;
134
// Param for third BLOB detector we want only circular object
135
typeDesc.push_back("BLOB");
136
pBLOB.push_back(pDefaultBLOB);
137
pBLOB.back().filterByCircularity = true;
138
// Param for Fourth BLOB detector we want ratio inertia
139
typeDesc.push_back("BLOB");
140
pBLOB.push_back(pDefaultBLOB);
141
pBLOB.back().filterByInertia = true;
142
pBLOB.back().minInertiaRatio = 0;
143
pBLOB.back().maxInertiaRatio = (float)0.2;
144
// Param for fifth BLOB detector we want ratio inertia
145
typeDesc.push_back("BLOB");
146
pBLOB.push_back(pDefaultBLOB);
147
pBLOB.back().filterByConvexity = true;
148
pBLOB.back().minConvexity = 0.;
149
pBLOB.back().maxConvexity = (float)0.9;
150
// Param for six BLOB detector we want blob with gravity center color equal to 0 bug #4321 must be fixed
151
typeDesc.push_back("BLOB");
152
pBLOB.push_back(pDefaultBLOB);
153
pBLOB.back().filterByColor = true;
154
pBLOB.back().blobColor = 0;
156
itBLOB = pBLOB.begin();
157
vector<double> desMethCmp;
161
vector<String>::iterator itDesc;
162
for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); itDesc++)
164
vector<KeyPoint> keyImg1;
165
if (*itDesc == "BLOB")
167
b = SimpleBlobDetector::create(*itBLOB);
168
label = Legende(*itBLOB);
173
// We can detect keypoint with detect method
174
vector<KeyPoint> keyImg;
176
vector<vector <Point> > region;
177
Mat desc, result(img.rows, img.cols, CV_8UC3);
178
if (b.dynamicCast<SimpleBlobDetector>() != NULL)
180
Ptr<SimpleBlobDetector> sbd = b.dynamicCast<SimpleBlobDetector>();
181
sbd->detect(img, keyImg, Mat());
182
drawKeypoints(img, keyImg, result);
184
for (vector<KeyPoint>::iterator k = keyImg.begin(); k != keyImg.end(); k++, i++)
185
circle(result, k->pt, (int)k->size, palette[i % 65536]);
187
namedWindow(*itDesc + label, WINDOW_AUTOSIZE);
188
imshow(*itDesc + label, result);
189
imshow("Original", img);
194
cout << "Feature : " << *itDesc << "\n";
195
cout << e.msg << endl;