微信公众号:OpenCV学堂
关注获取更多计算机视觉与深度学习知识
Blob分析
Blob分析函数与演示
-根据BLOB面积过滤
-根据灰度/颜色值过滤
-根据圆度过滤
-根据长轴与短轴过滤
-根据凹凸进行过滤
对应的参数列表如下:
SimpleBlobDetector::Params::Params()
bool filterByArea
bool filterByCircularity
bool filterByColor
bool filterByConvexity
bool filterByInertia
float maxArea
float maxCircularity
float maxConvexity
float maxInertiaRatio
float maxThreshold
float minArea
float minCircularity
float minConvexity
float minDistBetweenBlobs
float minInertiaRatio
#include "opencv2/opencv.hpp"
#include
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
// 加载图像
Mat src = imread("D:/lena.jpg");
Mat gray;
cvtColor(src, gray, COLOR_BGR2GRAY);
cv::imshow("输入图像", src);
// 初始化参数设置
SimpleBlobDetector::Params params;
params.minThreshold = 10;
params.maxThreshold = 240;
params.filterByArea = true;
params.minArea = 50;
params.filterByCircularity = true;
params.minCircularity = 0.1;
params.filterByConvexity = true;
params.minConvexity = 0.5;
params.filterByInertia = true;
params.minInertiaRatio = 0.5;
// 创建BLOB Detetor
Ptr detector = SimpleBlobDetector::create(params);
// BLOB分析与显示
Mat result;
vector keypoints;
detector->detect(gray, keypoints);
for (auto kpt : keypoints) {
std::cout << "key point radius: " << kpt.size << std::endl;
cv::circle(src, kpt.pt, 2, cv::Scalar(0, 255, 0), 2, 8, 0);
cv::circle(src, kpt.pt, kpt.size/2, cv::Scalar(255, 0, 255), 3, 8, 0);
}
imshow("Blob检测-OpenCV学堂", src);
waitKey(0);
}
特殊使用技巧
thresholdStep = 10;
minThreshold = 50;
maxThreshold = 220;
minRepeatability = 2;
minDistBetweenBlobs = 10;
filterByColor = true;
blobColor = 0;
filterByArea = true;
minArea = 25;
maxArea = 5000;
filterByCircularity = false;
minCircularity = 0.8f;
maxCircularity = std::numeric_limits
::max();
filterByInertia = true;
//minInertiaRatio = 0.6;
minInertiaRatio = 0.1f;
maxInertiaRatio = std::numeric_limits
::max();
filterByConvexity = true;
//minConvexity = 0.8;
minConvexity = 0.95f;
maxConvexity = std::numeric_limits
::max();
collectContours = false;
static void validateParameters(const SimpleBlobDetector::Params& p)
{
if (p.thresholdStep <= 0)
CV_Error(Error::StsBadArg, "thresholdStep>0");
if (p.minThreshold > p.maxThreshold || p.minThreshold < 0)
CV_Error(Error::StsBadArg, "0<=minThreshold<=maxThreshold");
if (p.minDistBetweenBlobs <=0 )
CV_Error(Error::StsBadArg, "minDistBetweenBlobs>0");
if (p.minArea > p.maxArea || p.minArea <=0)
CV_Error(Error::StsBadArg, "0);
if (p.minCircularity > p.maxCircularity || p.minCircularity <= 0)
CV_Error(Error::StsBadArg, "0);
if (p.minInertiaRatio > p.maxInertiaRatio || p.minInertiaRatio <= 0)
CV_Error(Error::StsBadArg, "0);
if (p.minConvexity > p.maxConvexity || p.minConvexity <= 0)
CV_Error(Error::StsBadArg, "0);
}
推荐阅读
Ubuntu系统下编译OpenCV4.8源码记录
Pytoorch轻松学 – RetinaNet自定义对象检测
基于OpenCV实现精准线线间距测量
OpenVINO2023异步回调流水线提升推理吞吐率
NNCF压缩与量化YOLOv8模型与OpenVINO部署测试
技能 | 三种主流的深度学习模型部署框架
深度学习模型C++推理,推荐看看 OpenVINO2022
OpenCV4系统化学习路线图(2023版)
扫码查看 OpenCV4 系统化学习路线图
作者新书
https://item.jd.com/10092255924058.html
学习课程有专属答疑群
负责贴身答疑解惑
读者专属QQ群 :657875553
进群暗号:OpenCV4读者