Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/BlobDetector/blob.cpp
3118 views
1
/**
2
* OpenCV SimpleBlobDetector Example
3
*
4
* Copyright 2015 by Satya Mallick <[email protected]>
5
*
6
*/
7
8
#include "opencv2/opencv.hpp"
9
10
using namespace cv;
11
using namespace std;
12
13
int main( int argc, char** argv )
14
{
15
16
// Read image
17
Mat im = imread( "blob.jpg", IMREAD_GRAYSCALE );
18
19
// Setup SimpleBlobDetector parameters.
20
SimpleBlobDetector::Params params;
21
22
// Change thresholds
23
params.minThreshold = 10;
24
params.maxThreshold = 200;
25
26
// Filter by Area.
27
params.filterByArea = true;
28
params.minArea = 1500;
29
30
// Filter by Circularity
31
params.filterByCircularity = true;
32
params.minCircularity = 0.1;
33
34
// Filter by Convexity
35
params.filterByConvexity = true;
36
params.minConvexity = 0.87;
37
38
// Filter by Inertia
39
params.filterByInertia = true;
40
params.minInertiaRatio = 0.01;
41
42
43
// Storage for blobs
44
vector<KeyPoint> keypoints;
45
46
47
#if CV_MAJOR_VERSION < 3 // If you are using OpenCV 2
48
49
// Set up detector with params
50
SimpleBlobDetector detector(params);
51
52
// Detect blobs
53
detector.detect( im, keypoints);
54
#else
55
56
// Set up detector with params
57
Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(params);
58
59
// Detect blobs
60
detector->detect( im, keypoints);
61
#endif
62
63
// Draw detected blobs as red circles.
64
// DrawMatchesFlags::DRAW_RICH_KEYPOINTS flag ensures
65
// the size of the circle corresponds to the size of blob
66
67
Mat im_with_keypoints;
68
drawKeypoints( im, keypoints, im_with_keypoints, Scalar(0,0,255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS );
69
70
// Show blobs
71
imshow("keypoints", im_with_keypoints );
72
waitKey(0);
73
74
}
75
76
77