Blob是图像中具有某些共同属性(如灰度值、圆度等如下图所示属性)的一组连通像素。
SimpleBlobDetector从图像中提取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 CV_OVERRIDE;
};
//SimpleBlobDetector
cv::SimpleBlobDetector::Params pBLOBDetector;
cv::Mat src;
int iThStep = 10;
int iMinth = 10;
int iMaxth = 200;
int iMinBt = 10;
int iMinar = 10;
int iMaxar = 1500;
int iMinCir = 0;
int iMinIne = 0;
int iMinCon = 0;
void detect(int ,void *)
{
pBLOBDetector.thresholdStep = iThStep;
pBLOBDetector.minThreshold = iMinth;
pBLOBDetector.maxThreshold = iMaxth;
pBLOBDetector.minRepeatability = 2;
pBLOBDetector.minDistBetweenBlobs = iMinBt;
pBLOBDetector.filterByColor = true;
pBLOBDetector.blobColor = 0;
//斑点面积
pBLOBDetector.filterByArea = true;
pBLOBDetector.minArea = iMinar;
pBLOBDetector.maxArea = iMaxar;
//斑点圆度
pBLOBDetector.filterByCircularity = true;
pBLOBDetector.minCircularity = iMinCir *0.01;
pBLOBDetector.maxCircularity = (float)3.40282e+038;
//斑点惯性率
pBLOBDetector.filterByInertia = true;
pBLOBDetector.minInertiaRatio = iMinIne * 0.01;
pBLOBDetector.maxInertiaRatio = (float)3.40282e+038;
//斑点凸度
pBLOBDetector.filterByConvexity = true;
pBLOBDetector.minConvexity = iMinCon * 0.01;
pBLOBDetector.maxConvexity = (float)3.40282e+038;
//*用参数创建对象
cv::Ptr<cv::SimpleBlobDetector> blob = cv::SimpleBlobDetector::create(pBLOBDetector);
//Ptr blob=SimpleBlobDetector::create();//默认参数创建
//*blob检测
vector<cv::KeyPoint> key_points;
//Mat dst;
//cvtColor(src, dst, COLOR_RGB2GRAY);
blob->detect(src, key_points);
cv::Mat outImg;
//src.copyTo(outImg);
//绘制结果
cv::drawKeypoints(src, key_points, outImg, cv::Scalar(0, 0, 255));
cv::imshow("blob", outImg);
}
void test_SimpleBlobDetector()
{
//cv::Mat src;
src = cv::imread("D:\\QtProject\\Opencv_Example\\SimpleBlobDetector\\blobs.png", cv::IMREAD_GRAYSCALE);
if (src.empty()) {
cout << "Cannot load image" << endl;
return;
}
cv::imshow("src", src);
cv::namedWindow("Detect window", cv::WINDOW_NORMAL);
cv::createTrackbar("最小圆度", "Detect window", &iMinCir, 100, detect);
cv::createTrackbar("最小惯性率", "Detect window", &iMinIne, 100, detect);
cv::createTrackbar("最大凸度", "Detect window", &iMinCon, 100, detect);
cv::createTrackbar("阈值步距", "Detect window", &iThStep, 100, detect);
cv::createTrackbar("最小阈值", "Detect window", &iMinth, 255, detect);
cv::createTrackbar("最大阈值", "Detect window", &iMaxth, 255, detect);
cv::createTrackbar("最小距离", "Detect window", &iMinBt, 255, detect);
cv::createTrackbar("最小面积", "Detect window", &iMinar, 1000, detect);
cv::createTrackbar("最大面积", "Detect window", &iMaxar, 5000, detect);
}