OpenCV4入门129:BLOB特征分析-SimpleBlobDetector使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;
int main()
{
Mat src=imread("D:/sunflower.png");
//*参数设置,以下都是默认参数
SimpleBlobDetector::Params pDefaultBLOB;
pDefaultBLOB.thresholdStep = 10;
pDefaultBLOB.minThreshold = 50;
pDefaultBLOB.maxThreshold = 220;
pDefaultBLOB.minRepeatability = 2;
pDefaultBLOB.minDistBetweenBlobs = 10;
pDefaultBLOB.filterByColor = true;
pDefaultBLOB.blobColor = 0;
pDefaultBLOB.filterByArea = true;
pDefaultBLOB.minArea = 25;
pDefaultBLOB.maxArea = 5000;
pDefaultBLOB.filterByCircularity = false;
pDefaultBLOB.minCircularity = 0.8f;
pDefaultBLOB.maxCircularity = (float)3.40282e+038;
pDefaultBLOB.filterByInertia = true;
pDefaultBLOB.minInertiaRatio = 0.1f;
pDefaultBLOB.maxInertiaRatio = (float)3.40282e+038;
pDefaultBLOB.filterByConvexity = true;
pDefaultBLOB.minConvexity = 0.95f;
pDefaultBLOB.maxConvexity = (float)3.40282e+038;
//*用参数创建对象
Ptr<SimpleBlobDetector> blob=SimpleBlobDetector::create(pDefaultBLOB);
//Ptr<SimpleBlobDetector> blob=SimpleBlobDetector::create();//默认参数创建
//*blob检测
vector<KeyPoint> key_points;
blob->detect(src,key_points);
Mat outImg;
//*绘制结果
drawKeypoints(src,key_points,outImg,Scalar(0,0,255));
imshow("blob",outImg);

waitKey();
return 0;
}

奇异区域检测:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// 功能:代码 7-7 奇异区域检测
// 作者:朱伟 zhu1988wei@163.com
// 来源:《OpenCV图像处理编程实例》
// 博客:http://blog.csdn.net/zhuwei1988
// 更新:2016-8-1
// 说明:版权所有,引用或摘录请联系作者,并按照上面格式注明出处,谢谢。//
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
int main()
{
cv::Mat srcImage =
cv::imread("..\\images\\flow.jpg");
if (!srcImage.data)
return -1;
cv::imshow("srcImage", srcImage);
// 向量关键点
std::vector<KeyPoint> keypoints;
// blob类定义
SimpleBlobDetector::Params params;
// 参数定义
params.filterByArea = true;
params.minArea = 10;
params.maxArea = 100;
Ptr<SimpleBlobDetector> blobDetector = SimpleBlobDetector::create(params);
// 奇异区域检测
blobDetector->detect(srcImage, keypoints);
// 绘制关键点
drawKeypoints(srcImage, keypoints,
srcImage, Scalar(255, 0, 0));
cv::imshow("result", srcImage);
cv::waitKey();
return 0;
}