【EMGUCV】simpleblob detector 斑点检测

本文主要介绍emgucv中的simpleblob detector。

算法原理

该检测器的原理主要如下:
1. 先设置步进值(thresholdStep)跟二值化阈值范围[minThreshold,maxThreshold),把原图像SrcImg在阈值范围内按照步进依次转化为二值化图像。
2. 从这些二值化图像中提取轮廓,并且计算这些轮廓的中心。
3. 根据这些轮廓的中心坐标进行分组,坐标接近的属于同一个blob,坐标要有多接近才算接近?这个通过minDistBetweenBlobs控制。
4. 最后,在找到的这些分组中,进一步根据所选的参数来进行判断是否blob。这些参数有:
1) color: 通过判断blob中心坐标的颜色是否跟所选颜色一致来决定是否要丢弃这个blob,blobColor=0表示要提取黑色的点,blobColor=255表示要提取白色的点
2)area:通过判断blob的面积,也就是像素总数是否在[minArea,maxArea)
3)circularity:圆心率,公式为: 4πAreaperimeterperimeter ,当圆心率范围为[minCircularity ,maxCircularity )时,选中该blob
4) minInertiamaxInertia :惯性率,椭圆最小的直径跟最大的直径之间的比。当惯性率范围为[minInertiaRatio ,maxInertiaRatio)时,选中该blob
5)convexity:凸度,凸度公式是 areaareaOfConvexHull ,blob面积除以凸包面积,当该凸度在为[minConvexity ,maxConvexity )时,选中该blob

以下是从 其他网站上看到的比较形象的图示:

这里写图片描述

具体代码
需要注意的是,EMGUCV中,版本在3.0之上才会有SimpleBlobDetector这个类。
opencv中应该是2.4版本就有了,调用方法基本一致

Image<Gray, byte> imgGray = img.Convert<Gray, byte>();
/* blob detector */
//Gray Gavg = imgGray.GetAverage();
double minValue = 255;
double maxValue = 0;
Point minLoc = new Point();
Point maxLoc = new Point();
CvInvoke.MinMaxLoc(imgGray, ref minValue, ref maxValue, ref minLoc, ref maxLoc);

SimpleBlobDetectorParams blobparams = new SimpleBlobDetectorParams();
blobparams.FilterByArea = true;
blobparams.MinArea = 150;
blobparams.MaxArea = 1500;
blobparams.MinThreshold = (float)minValue + 1;
blobparams.MaxThreshold = (float)maxValue;
blobparams.FilterByCircularity = false;  //斑点圆度
blobparams.MinCircularity = (float)0.5;
blobparams.MaxCircularity = 1;
blobparams.FilterByConvexity = true;    //斑点凸度
blobparams.MinConvexity = (float)0.2;
blobparams.MaxConvexity = 1;
blobparams.FilterByInertia = true;  //斑点惯性率
blobparams.MinInertiaRatio = (float)0.4;
blobparams.MaxInertiaRatio = 1;
blobparams.FilterByColor = false;
blobparams.ThresholdStep = 2;
blobparams.MinRepeatability = new IntPtr(2);
SimpleBlobDetector detector = new SimpleBlobDetector(blobparams);

MKeyPoint[] keypoints = detector.Detect(imgGray);
Image<Bgr, byte> imgBgr = img.Copy();

foreach (MKeyPoint keypoint in keypoints)
{

    imgBgr.Draw(new Rectangle((int)(keypoint.Point.X - keypoint.Size / 2), (int)(keypoint.Point.Y - keypoint.Size / 2), (int)keypoint.Size, (int)keypoint.Size), new Bgr(255,0,0), 1);
    pictureBox1.Image = imgBgr.ToBitmap();
}
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
OpenCV提供了多种斑点检测算法,其中包括SimpleBlobDetector、MSER(Maximally Stable Extremal Regions)、SIFT(Scale-Invariant Feature Transform)等。这里以SimpleBlobDetector为例,介绍一下OpenCV中如何实现斑点检测。 1. 首先,需要导入OpenCV库并读入待检测的图像,可以使用cv::imread()函数来读入图像。 ``` #include <opencv2/opencv.hpp> using namespace cv; int main() { Mat image = imread("test.png", IMREAD_GRAYSCALE); if (image.empty()) { std::cout << "Failed to read image." << std::endl; return -1; } // 进行斑点检测 // ... return 0; } ``` 2. 创建SimpleBlobDetector对象,并设置一些参数,如阈值、最小面积、最大面积等。 ``` SimpleBlobDetector::Params params; params.minThreshold = 10; params.maxThreshold = 200; params.filterByArea = true; params.minArea = 100; params.maxArea = 10000; Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(params); ``` 3. 使用detector->detect()函数进行斑点检测,该函数返回一个vector类型的数据,其中包含了所有被检测到的斑点的坐标。 ``` std::vector<KeyPoint> keypoints; detector->detect(image, keypoints); ``` 4. 可以使用cv::drawKeypoints()函数将检测到的斑点绘制到图像上。 ``` Mat result; drawKeypoints(image, keypoints, result, Scalar(0,0,255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS); imshow("Result", result); waitKey(0); ``` 这样就完成了斑点检测的过程,可以通过调整参数来适应不同的图像和应用场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值