~paparazzi-uav/paparazzi/v5.0-manual

« back to all changes in this revision

Viewing changes to sw/ext/opencv_bebop/opencv/samples/cpp/detect_blob.cpp

  • Committer: Paparazzi buildbot
  • Date: 2016-05-18 15:00:29 UTC
  • Revision ID: felix.ruess+docbot@gmail.com-20160518150029-e8lgzi5kvb4p7un9
Manual import commit 4b8bbb730080dac23cf816b98908dacfabe2a8ec from v5.0 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <opencv2/opencv.hpp>
 
2
#include <vector>
 
3
#include <map>
 
4
#include <iostream>
 
5
 
 
6
using namespace std;
 
7
using namespace cv;
 
8
 
 
9
 
 
10
static void help()
 
11
{
 
12
    cout << "\n This program demonstrates how to use BLOB to detect and filter region \n"
 
13
        "Usage: \n"
 
14
        "  ./detect_blob <image1(../data/detect_blob.png as default)>\n"
 
15
        "Press a key when image window is active to change descriptor";
 
16
}
 
17
 
 
18
 
 
19
static String Legende(SimpleBlobDetector::Params &pAct)
 
20
{
 
21
    String s = "";
 
22
    if (pAct.filterByArea)
 
23
    {
 
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 + "]";
 
27
    }
 
28
    if (pAct.filterByCircularity)
 
29
    {
 
30
        String inf = static_cast<ostringstream*>(&(ostringstream() << pAct.minCircularity))->str();
 
31
        String sup = static_cast<ostringstream*>(&(ostringstream() << pAct.maxCircularity))->str();
 
32
        if (s.length() == 0)
 
33
            s = " Circularity range [" + inf + " to  " + sup + "]";
 
34
        else
 
35
            s += " AND Circularity range [" + inf + " to  " + sup + "]";
 
36
    }
 
37
    if (pAct.filterByColor)
 
38
    {
 
39
        String inf = static_cast<ostringstream*>(&(ostringstream() << (int)pAct.blobColor))->str();
 
40
        if (s.length() == 0)
 
41
            s = " Blob color " + inf;
 
42
        else
 
43
            s += " AND Blob color " + inf;
 
44
    }
 
45
    if (pAct.filterByConvexity)
 
46
    {
 
47
        String inf = static_cast<ostringstream*>(&(ostringstream() << pAct.minConvexity))->str();
 
48
        String sup = static_cast<ostringstream*>(&(ostringstream() << pAct.maxConvexity))->str();
 
49
        if (s.length() == 0)
 
50
            s = " Convexity range[" + inf + " to  " + sup + "]";
 
51
        else
 
52
            s += " AND  Convexity range[" + inf + " to  " + sup + "]";
 
53
    }
 
54
    if (pAct.filterByInertia)
 
55
    {
 
56
        String inf = static_cast<ostringstream*>(&(ostringstream() << pAct.minInertiaRatio))->str();
 
57
        String sup = static_cast<ostringstream*>(&(ostringstream() << pAct.maxInertiaRatio))->str();
 
58
        if (s.length() == 0)
 
59
            s = " Inertia ratio range [" + inf + " to  " + sup + "]";
 
60
        else
 
61
            s += " AND  Inertia ratio range [" + inf + " to  " + sup + "]";
 
62
    }
 
63
    return s;
 
64
}
 
65
 
 
66
 
 
67
 
 
68
int main(int argc, char *argv[])
 
69
{
 
70
    vector<String> fileName;
 
71
    Mat img(600, 800, CV_8UC1);
 
72
    cv::CommandLineParser parser(argc, argv, "{@input |../data/detect_blob.png| }{h help | | }");
 
73
    if (parser.has("h"))
 
74
    {
 
75
        help();
 
76
        return 0;
 
77
    }
 
78
    fileName.push_back(parser.get<string>("@input"));
 
79
    img = imread(fileName[0], IMREAD_COLOR);
 
80
    if (img.rows*img.cols <= 0)
 
81
    {
 
82
        cout << "Image " << fileName[0] << " is empty or cannot be found\n";
 
83
        return(0);
 
84
    }
 
85
 
 
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;
 
112
    // Color palette
 
113
    vector< Vec3b >  palette;
 
114
    for (int i = 0; i<65536; i++)
 
115
    {
 
116
        palette.push_back(Vec3b((uchar)rand(), (uchar)rand(), (uchar)rand()));
 
117
    }
 
118
    help();
 
119
 
 
120
 
 
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;
 
155
 
 
156
    itBLOB = pBLOB.begin();
 
157
    vector<double> desMethCmp;
 
158
    Ptr<Feature2D> b;
 
159
    String label;
 
160
    // Descriptor loop
 
161
    vector<String>::iterator itDesc;
 
162
    for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); itDesc++)
 
163
    {
 
164
        vector<KeyPoint> keyImg1;
 
165
        if (*itDesc == "BLOB")
 
166
        {
 
167
            b = SimpleBlobDetector::create(*itBLOB);
 
168
            label = Legende(*itBLOB);
 
169
            itBLOB++;
 
170
        }
 
171
        try
 
172
        {
 
173
            // We can detect keypoint with detect method
 
174
            vector<KeyPoint>  keyImg;
 
175
            vector<Rect>  zone;
 
176
            vector<vector <Point> >  region;
 
177
            Mat     desc, result(img.rows, img.cols, CV_8UC3);
 
178
            if (b.dynamicCast<SimpleBlobDetector>() != NULL)
 
179
            {
 
180
                Ptr<SimpleBlobDetector> sbd = b.dynamicCast<SimpleBlobDetector>();
 
181
                sbd->detect(img, keyImg, Mat());
 
182
                drawKeypoints(img, keyImg, result);
 
183
                int i = 0;
 
184
                for (vector<KeyPoint>::iterator k = keyImg.begin(); k != keyImg.end(); k++, i++)
 
185
                    circle(result, k->pt, (int)k->size, palette[i % 65536]);
 
186
            }
 
187
            namedWindow(*itDesc + label, WINDOW_AUTOSIZE);
 
188
            imshow(*itDesc + label, result);
 
189
            imshow("Original", img);
 
190
            waitKey();
 
191
        }
 
192
        catch (Exception& e)
 
193
        {
 
194
            cout << "Feature : " << *itDesc << "\n";
 
195
            cout << e.msg << endl;
 
196
        }
 
197
    }
 
198
    return 0;
 
199
}