BlobDetector的使用与参数说明(OpenCV/C++)

通过opencv的BlobDetector方法可以检测斑点、圆点、椭圆等形状

以下是使用方式及代码说明:

1、导入必要的OpenCV库和头文件。

#include <opencv2/opencv.hpp>
#include <opencv2/blob/blobdetector.hpp>

2、读取图像并将其转换为灰度图像。

cv::Mat image = cv::imread("image.jpg", cv::IMREAD_GRAYSCALE);

3、创建一个BlobDetector对象,并设置相关参数。

cv::SimpleBlobDetector::Params params;

// 设置检测圆的阈值
params.minThreshold = 10;
params.maxThreshold = 200;

//设置识别的预期颜色
//params.blobColor = 0;    //0表示预期检测圆的颜色为白色

// 用于过滤面积小的blob
params.filterByArea = false;    //true表示启动该功能,false表示关闭该功能
params.minArea = 0.1;

//用于过滤不符合圆形形状的blob
params.filterByCircularity = true;
params.minCircularity = 0.8;

//用于过滤不规则或者非凸的blob
params.filterByConvexity = true;
params.minConvexity = 0.8;

//用于过滤不符合椭圆形状的blob
params.filterByInertia = true;
params.minInertiaRatio = 0.8;

// 创建BlobDetector对象
cv::Ptr<cv::SimpleBlobDetector> detector = cv::SimpleBlobDetector::create(params);

4、使用BlobDetector在图像中检测圆形。

std::vector<cv::KeyPoint> keypoints;
detector->detect(image, keypoints);

5、绘制检测到的圆。

cv::Mat imageWithKeypoints;
cv::drawKeypoints(image, keypoints, imageWithKeypoints, cv::Scalar(0, 0, 255), cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);

6、显示并保存带有检测到的圆的图像。

cv::imshow("Circle Detection", imageWithKeypoints);
cv::waitKey(0);
cv::imwrite("result.jpg", imageWithKeypoints);
相关推荐
游王子32 分钟前
OpenCV(11):人脸检测、物体识别
人工智能·opencv·计算机视觉
Forget the Dream43 分钟前
设计模式之责任链模式
java·c++·设计模式·责任链模式
停止重构44 分钟前
【开源】低代码 C++程序框架,Linux多线程程序
c++·低代码·多线程·开源框架·云计算引擎
Duramentee1 小时前
C++ 设计模式 十二:责任链模式 (读书 现代c++设计模式)
c++·设计模式·责任链模式
_GR1 小时前
Qt开发⑧Qt的窗口_下_浮动窗口+对话框
开发语言·css·c++·qt·microsoft
萌の鱼1 小时前
leetcode 3183. 达到总和的方法数量
数据结构·c++·算法·leetcode
Forget the Dream2 小时前
设计模式之代理模式
java·c++·设计模式·代理模式
楼台的春风2 小时前
【图像亮度、对比度调整,直方图均衡化及图像平滑】
图像处理·人工智能·opencv·算法·计算机视觉·matlab·嵌入式
攻城狮7号4 小时前
【第十节】C++设计模式(结构型模式)-Flyweight( 享元)模式
c++·设计模式·享元模式
Forget the Dream4 小时前
设计模式之享元模式
java·c++·设计模式·享元模式