基于局部极值的分水岭算法斑点检测simpleBlobDetector
分为以下几步:
1.对一张图片,设定一个低阈值minThreshold
,设定一个高阈值maxThreshold
,在设定一个阈值步进thresholdStep
,然后从低阈值到高阈值按照阈值步进取一系列的阈值,即对[minThreshold,maxThreshold)
区间,以thresholdStep
为间隔,用每一个阈值对图像进行二值化,得到一系列图像;
2.对每张二值图片,使用findcontours
查找这些图像的边,并计算每一个轮廓的中心;
3.根据2得到每一个图片的轮的中心点,全部放在一起。定义一个最小距离,在这个距离区域内的特征中心点[由minDistBetweenBlobs
控制多少才算接近]被归为一个group
,对应一个bolb特征,得到特征点集合。
4.从3得到的那些点,估计最后的blob特征和相应半径,并以key points
返回。对特征点进行相应的过滤,例如颜色过滤,面积过滤等
opencv中检测Blobs的类为SimpleBlobDetector
,这个类在opencv中的定义如下:
// Blob算子参数
SimpleBlobDetector::Params params;
params.thresholdStep = 10; //二值化的阈值步长
//阈值控制
params.minThreshold = 20;
params.maxThreshold = 200;
//斑点颜色(0黑色,255白色)
params.filterByColor = true;
params.blobColor = 0;
//像素面积大小控制
params.filterByArea = true;
params.minArea = 200;
params.maxArea = 80000;
//形状(凸)
params.filterByCircularity = false;
params.minCircularity = 0.7;
//形状(凹)
params.filterByConvexity = true;
params.minConvexity = 0.9;
//形状(圆)的饱满度
params.filterByCircularity = true;
params.minCircularity = 0.5;
Ptr<FeatureDetector> blobDetector = SimpleBlobDetector::create(params);
//定义检测的圆心点
vector<KeyPoint> keypoints;
blobDetector->detect(img,keypoints);
//在img上显示圆心点
Mat img_with_keypoint;
drawKeypoints(img,keypoints,img_with_keypoint,Scalar(0,0,255),DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
// 提取圆点特征的圆心(相机标定使用)
Size boardSizea(2,3);
//缓存检测到的角点
vector<Point2f> imagePointsBuf;
bool found = findCirclesGrid(imageInput, boardSizea, imagePointsBuf, CALIB_CB_SYMMETRIC_GRID | CALIB_CB_CLUSTERING, blobDetector);
drawChessboardCorners(imageInput, boardSizea, imagePointsBuf, found);
opencv3 的features2d.hpp
中SimpleBlobDetector
的定义:
/** @brief Class for extracting blobs from an image. :
The class implements a simple algorithm for extracting blobs from an image:
1. Convert the source image to binary images by applying thresholding with several thresholds from
minThreshold (inclusive) to maxThreshold (exclusive) with distance thresholdStep between
neighboring thresholds.
2. Extract connected components from every binary image by findContours and calculate their
centers.
3. Group centers from several binary images by their coordinates. Close centers form one group that
corresponds to one blob, which is controlled by the minDistBetweenBlobs parameter.
4. From the groups, estimate final centers of blobs and their radiuses and return as locations and
sizes of keypoints.
This class performs several filtrations of returned blobs. You should set filterBy\* to true/false
to turn on/off corresponding filtration. Available filtrations:
- **By color**. This filter compares the intensity of a binary image at the center of a blob to
blobColor. If they differ, the blob is filtered out. Use blobColor = 0 to extract dark blobs
and blobColor = 255 to extract light blobs.
- **By area**. Extracted blobs have an area between minArea (inclusive) and maxArea (exclusive).
- **By circularity**. Extracted blobs have circularity
(\f$\frac{4*\pi*Area}{perimeter * perimeter}\f$) between minCircularity (inclusive) and
maxCircularity (exclusive).
- **By ratio of the minimum inertia to maximum inertia**. Extracted blobs have this ratio
between minInertiaRatio (inclusive) and maxInertiaRatio (exclusive).
- **By convexity**. Extracted blobs have convexity (area / area of blob convex hull) between
minConvexity (inclusive) and maxConvexity (exclusive).
Default values of parameters are tuned to extract dark circular blobs.
*/
class CV_EXPORTS_W SimpleBlobDetector : public Feature2D
{
public:
struct CV_EXPORTS_W_SIMPLE Params
{
CV_WRAP Params();
CV_PROP_RW float thresholdStep;
CV_PROP_RW float minThreshold;
CV_PROP_RW float maxThreshold;
CV_PROP_RW size_t minRepeatability;
CV_PROP_RW float minDistBetweenBlobs;
CV_PROP_RW bool filterByColor;
CV_PROP_RW uchar blobColor;
CV_PROP_RW bool filterByArea;
CV_PROP_RW float minArea, maxArea;
CV_PROP_RW bool filterByCircularity;
CV_PROP_RW float minCircularity, maxCircularity;
CV_PROP_RW bool filterByInertia;
CV_PROP_RW float minInertiaRatio, maxInertiaRatio;
CV_PROP_RW bool filterByConvexity;
CV_PROP_RW float minConvexity, maxConvexity;
void read( const FileNode& fn );
void write( FileStorage& fs ) const;
};
CV_WRAP static Ptr<SimpleBlobDetector>
create(const SimpleBlobDetector::Params ¶meters = SimpleBlobDetector::Params());
CV_WRAP virtual String getDefaultName() const;
};