OpenCv 109---Blob分析

1 前备知识

(1)圆度

圆度误差:指包容同一正截面实际轮廓且半径差为最小的两同心圆间的距离:fm = Rmax - Rmin;

圆度公差带:在同一正截面上半径差为公差值的两同心圆间的区域。

(2)偏心率

椭圆两焦点间的距离和长轴长度的比值。即某一椭圆轨道与理想圆环的偏离,长椭圆轨道“偏心率”高,而近于圆形的轨道“偏心率”低。
离心率定义为椭圆两焦点间的距离和长轴长度的比值。
偏心率用来描述轨道的形状,用焦点间距离除以长轴的长度可以算出偏心率。偏心率一般用e表示。
当e=0时 圆
当0<e<1时 椭圆
当e=1时 抛物线
当e>1时双曲线

(3)凸度

2 所用到的主要OpenCv API及类

(1)SimpleBlobDetector //TODO 从Opencv3 中查看中文解释

/** @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 &parameters = SimpleBlobDetector::Params());
  CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
};

(2)Function

/** @brief Draws keypoints.

@param image Source image.

@param keypoints Keypoints from the source image.

@param outImage Output image. Its content depends on the flags value defining what is drawn in the

output image. See possible flags bit values below.

@param color Color of keypoints.

@param flags Flags setting drawing features. Possible flags bit values are defined by

DrawMatchesFlags. See details above in drawMatches .

@note

For Python API, flags are modified as cv2.DRAW_MATCHES_FLAGS_DEFAULT,

cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS, cv2.DRAW_MATCHES_FLAGS_DRAW_OVER_OUTIMG,

cv2.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS 

 */

CV_EXPORTS_W void drawKeypoints( InputArray image, const std::vector<KeyPoint>& keypoints, InputOutputArray outImage,
                               const Scalar& color=Scalar::all(-1), int flags=DrawMatchesFlags::DEFAULT );

3 程序代码

#include<opencv2opencv.hpp>
#include<iostream>

using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
	//加载图像
	Mat src = imread("G:\CVworkstudy\program_wwx\研习社140课时\ZhaiZhigang140\zhifang_ball.png");
	if (src.empty())
	{
		cout << "Could not load image..." << endl;
		return -1;
	}
	imshow("srcImg", src);
	Mat gray;
	cvtColor(src, gray, CV_RGB2GRAY);

	//初始化参数设置
	SimpleBlobDetector::Params filterParams;
	filterParams.minThreshold = 10;
	filterParams.maxThreshold = 200;
	filterParams.filterByArea = true;
	filterParams.minArea = 100;
	filterParams.filterByCircularity = true;
	filterParams.minCircularity = 0.1f;
	filterParams.filterByConvexity = true;
	filterParams.minConvexity = 0.87f;
	filterParams.filterByInertia = true;
	filterParams.minInertiaRatio = 0.01f;

	//创建BLOB Detector
	Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(filterParams);

	//Blob 分析与显示
	Mat result;
	vector<KeyPoint> keypoints;
	detector->detect(gray, keypoints);
	drawKeypoints(src, keypoints, result, Scalar(0, 0, 255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
	imshow("Blob Detection Demo", result);
	waitKey(0);
}

4 运行结果

5 扩展及注意事项

//TODO

One day,I will say "I did it"
原文地址:https://www.cnblogs.com/Vince-Wu/p/11190186.html