Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/cpp/detect_blob.cpp
16337 views
1
#include <opencv2/core.hpp>
2
#include <opencv2/imgproc.hpp>
3
#include <opencv2/highgui.hpp>
4
#include <opencv2/features2d.hpp>
5
#include <vector>
6
#include <map>
7
#include <iostream>
8
9
using namespace std;
10
using namespace cv;
11
12
13
static void help()
14
{
15
cout << "\n This program demonstrates how to use BLOB to detect and filter region \n"
16
"Usage: \n"
17
" ./detect_blob <image1(../data/detect_blob.png as default)>\n"
18
"Press a key when image window is active to change descriptor";
19
}
20
21
22
static String Legende(SimpleBlobDetector::Params &pAct)
23
{
24
String s = "";
25
if (pAct.filterByArea)
26
{
27
String inf = static_cast<const ostringstream&>(ostringstream() << pAct.minArea).str();
28
String sup = static_cast<const ostringstream&>(ostringstream() << pAct.maxArea).str();
29
s = " Area range [" + inf + " to " + sup + "]";
30
}
31
if (pAct.filterByCircularity)
32
{
33
String inf = static_cast<const ostringstream&>(ostringstream() << pAct.minCircularity).str();
34
String sup = static_cast<const ostringstream&>(ostringstream() << pAct.maxCircularity).str();
35
if (s.length() == 0)
36
s = " Circularity range [" + inf + " to " + sup + "]";
37
else
38
s += " AND Circularity range [" + inf + " to " + sup + "]";
39
}
40
if (pAct.filterByColor)
41
{
42
String inf = static_cast<const ostringstream&>(ostringstream() << (int)pAct.blobColor).str();
43
if (s.length() == 0)
44
s = " Blob color " + inf;
45
else
46
s += " AND Blob color " + inf;
47
}
48
if (pAct.filterByConvexity)
49
{
50
String inf = static_cast<const ostringstream&>(ostringstream() << pAct.minConvexity).str();
51
String sup = static_cast<const ostringstream&>(ostringstream() << pAct.maxConvexity).str();
52
if (s.length() == 0)
53
s = " Convexity range[" + inf + " to " + sup + "]";
54
else
55
s += " AND Convexity range[" + inf + " to " + sup + "]";
56
}
57
if (pAct.filterByInertia)
58
{
59
String inf = static_cast<const ostringstream&>(ostringstream() << pAct.minInertiaRatio).str();
60
String sup = static_cast<const ostringstream&>(ostringstream() << pAct.maxInertiaRatio).str();
61
if (s.length() == 0)
62
s = " Inertia ratio range [" + inf + " to " + sup + "]";
63
else
64
s += " AND Inertia ratio range [" + inf + " to " + sup + "]";
65
}
66
return s;
67
}
68
69
70
71
int main(int argc, char *argv[])
72
{
73
vector<String> fileName;
74
Mat img(600, 800, CV_8UC1);
75
cv::CommandLineParser parser(argc, argv, "{@input |../data/detect_blob.png| }{h help | | }");
76
if (parser.has("h"))
77
{
78
help();
79
return 0;
80
}
81
fileName.push_back(parser.get<string>("@input"));
82
img = imread(fileName[0], IMREAD_COLOR);
83
if (img.rows*img.cols <= 0)
84
{
85
cout << "Image " << fileName[0] << " is empty or cannot be found\n";
86
return(0);
87
}
88
89
SimpleBlobDetector::Params pDefaultBLOB;
90
// This is default parameters for SimpleBlobDetector
91
pDefaultBLOB.thresholdStep = 10;
92
pDefaultBLOB.minThreshold = 10;
93
pDefaultBLOB.maxThreshold = 220;
94
pDefaultBLOB.minRepeatability = 2;
95
pDefaultBLOB.minDistBetweenBlobs = 10;
96
pDefaultBLOB.filterByColor = false;
97
pDefaultBLOB.blobColor = 0;
98
pDefaultBLOB.filterByArea = false;
99
pDefaultBLOB.minArea = 25;
100
pDefaultBLOB.maxArea = 5000;
101
pDefaultBLOB.filterByCircularity = false;
102
pDefaultBLOB.minCircularity = 0.9f;
103
pDefaultBLOB.maxCircularity = (float)1e37;
104
pDefaultBLOB.filterByInertia = false;
105
pDefaultBLOB.minInertiaRatio = 0.1f;
106
pDefaultBLOB.maxInertiaRatio = (float)1e37;
107
pDefaultBLOB.filterByConvexity = false;
108
pDefaultBLOB.minConvexity = 0.95f;
109
pDefaultBLOB.maxConvexity = (float)1e37;
110
// Descriptor array for BLOB
111
vector<String> typeDesc;
112
// Param array for BLOB
113
vector<SimpleBlobDetector::Params> pBLOB;
114
vector<SimpleBlobDetector::Params>::iterator itBLOB;
115
// Color palette
116
vector< Vec3b > palette;
117
for (int i = 0; i<65536; i++)
118
{
119
palette.push_back(Vec3b((uchar)rand(), (uchar)rand(), (uchar)rand()));
120
}
121
help();
122
123
124
// These descriptors are going to be detecting and computing BLOBS with 6 different params
125
// Param for first BLOB detector we want all
126
typeDesc.push_back("BLOB"); // see http://docs.opencv.org/trunk/d0/d7a/classcv_1_1SimpleBlobDetector.html
127
pBLOB.push_back(pDefaultBLOB);
128
pBLOB.back().filterByArea = true;
129
pBLOB.back().minArea = 1;
130
pBLOB.back().maxArea = float(img.rows*img.cols);
131
// Param for second BLOB detector we want area between 500 and 2900 pixels
132
typeDesc.push_back("BLOB");
133
pBLOB.push_back(pDefaultBLOB);
134
pBLOB.back().filterByArea = true;
135
pBLOB.back().minArea = 500;
136
pBLOB.back().maxArea = 2900;
137
// Param for third BLOB detector we want only circular object
138
typeDesc.push_back("BLOB");
139
pBLOB.push_back(pDefaultBLOB);
140
pBLOB.back().filterByCircularity = true;
141
// Param for Fourth BLOB detector we want ratio inertia
142
typeDesc.push_back("BLOB");
143
pBLOB.push_back(pDefaultBLOB);
144
pBLOB.back().filterByInertia = true;
145
pBLOB.back().minInertiaRatio = 0;
146
pBLOB.back().maxInertiaRatio = (float)0.2;
147
// Param for fifth BLOB detector we want ratio inertia
148
typeDesc.push_back("BLOB");
149
pBLOB.push_back(pDefaultBLOB);
150
pBLOB.back().filterByConvexity = true;
151
pBLOB.back().minConvexity = 0.;
152
pBLOB.back().maxConvexity = (float)0.9;
153
// Param for six BLOB detector we want blob with gravity center color equal to 0 bug #4321 must be fixed
154
typeDesc.push_back("BLOB");
155
pBLOB.push_back(pDefaultBLOB);
156
pBLOB.back().filterByColor = true;
157
pBLOB.back().blobColor = 0;
158
159
itBLOB = pBLOB.begin();
160
vector<double> desMethCmp;
161
Ptr<Feature2D> b;
162
String label;
163
// Descriptor loop
164
vector<String>::iterator itDesc;
165
for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); ++itDesc)
166
{
167
vector<KeyPoint> keyImg1;
168
if (*itDesc == "BLOB")
169
{
170
b = SimpleBlobDetector::create(*itBLOB);
171
label = Legende(*itBLOB);
172
++itBLOB;
173
}
174
try
175
{
176
// We can detect keypoint with detect method
177
vector<KeyPoint> keyImg;
178
vector<Rect> zone;
179
vector<vector <Point> > region;
180
Mat desc, result(img.rows, img.cols, CV_8UC3);
181
if (b.dynamicCast<SimpleBlobDetector>().get())
182
{
183
Ptr<SimpleBlobDetector> sbd = b.dynamicCast<SimpleBlobDetector>();
184
sbd->detect(img, keyImg, Mat());
185
drawKeypoints(img, keyImg, result);
186
int i = 0;
187
for (vector<KeyPoint>::iterator k = keyImg.begin(); k != keyImg.end(); ++k, ++i)
188
circle(result, k->pt, (int)k->size, palette[i % 65536]);
189
}
190
namedWindow(*itDesc + label, WINDOW_AUTOSIZE);
191
imshow(*itDesc + label, result);
192
imshow("Original", img);
193
waitKey();
194
}
195
catch (Exception& e)
196
{
197
cout << "Feature : " << *itDesc << "\n";
198
cout << e.msg << endl;
199
}
200
}
201
return 0;
202
}
203
204