OpenCv 109---Blob分析

摘要:
1前备知识圆度圆度误差:指包容同一正截面实际轮廓且半径差为最小的两同心圆间的距离:fm=Rmax-Rmin;圆度公差带:在同一正截面上半径差为公差值的两同心圆间的区域。偏心率椭圆两焦点间的距离和长轴长度的比值。即某一椭圆轨道与理想圆环的偏离,长椭圆轨道“偏心率”高,而近于圆形的轨道“偏心率”低。偏心率一般用e表示。

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 : publicFeature2D
{
public:
  structCV_EXPORTS_W_SIMPLE Params
  {
      CV_WRAP Params();
      CV_PROP_RW floatthresholdStep;//阈值步进值
      CV_PROP_RW floatminThreshold;//进行二值化时的最小阈值
      CV_PROP_RW floatmaxThreshold;//进行二值化的最大阈值
      CV_PROP_RW size_t minRepeatability;
      CV_PROP_RW floatminDistBetweenBlobs;

      CV_PROP_RW boolfilterByColor;
      CV_PROP_RW uchar blobColor;

      CV_PROP_RW boolfilterByArea;
      CV_PROP_RW floatminArea, maxArea;

      CV_PROP_RW boolfilterByCircularity;
      CV_PROP_RW floatminCircularity, maxCircularity;

      CV_PROP_RW boolfilterByInertia;
      CV_PROP_RW floatminInertiaRatio, maxInertiaRatio;

      CV_PROP_RW boolfilterByConvexity;
      CV_PROP_RW floatminConvexity, 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() constCV_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 运行结果

OpenCv 109---Blob分析第1张

OpenCv 109---Blob分析第2张

5 扩展及注意事项

//TODO

免责声明:文章转载自《OpenCv 109---Blob分析》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇vue之列表渲染sql级联删除与级联更新的思考下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

前端er们如何最快开发h5移动端页面?

因此,它总结了移动终端H5最快发展的最佳方案。web移动终端的发展应注重简化,以满足基本业务需求,设计应尽可能扁平化。前视图层angularjs或react作为框架,node作为中间层,js处理从后端接口获取的数据并操作渲染模板文件,这相当于在MVC中完成控制器层的工作。底层是数据库和后端。...

iTerm2 配色方案(转)

ITerm2配色方案(噢,我的zsh&amp;Powerline)item2.png转到官方网站下载ITerm2,并以全黑色背景打开它。屏幕截图2017-08-01下午5.45.28点。Pngsolarized可以说是目前网络上最流行的配色方案,我个人认为比较这种配色更好。第一种方法:打开iTerm2首选项、配置文件/颜色,然后直接选择Solariz...

SqlServer数据库存入decimal类型数据注意事项

对于sqlserver,Decimal可用于存储具有小数点和固定值的值。与浮点和实数不同,十进制用于存储近似值。目的是满足精确数学运算的需要。它是最大和最精确的浮点数字类型。对于十进制类型,请注意必须指定精度;否则,十进制只能存储为整数,就像int一样。例如,十进制是存储长度为18位和小数点后2位的数据。...

JS获取当前时间

如果有更好的方法,请提出建议。进一步解释如下:varmyDate=newDate();我的日期。getYear();//获取当前年份(2位数)myDate getFullYear();//获取完整的年份(4位数,1970-???=0)||);}//----------------------------------------------//日期格式//格式...

H3C 12508 收集诊断信息

案例:H3C12508单板卡出现remove状态,需要配合研发收集诊断信息。)总体:12500交换机返回三种文件----故障时诊断信息,主备单板的日志文件,主备单板的诊断日志操作步骤:一、故障时诊断信息:disdiagnostic-informationdiag收集必须在问题出现的时候,单板重起之前执行。在save时请选择Y保存到CF卡方式。一般情况下,此命...

GitLab的基础使用-创建用户(users)

否则,将追究法律责任。1、 以管理员身份登录GitLab的WebUI,并创建用户1˃使用管理员登录GitLab。管理员登录成功后,点击下图所示的小扳手,然后点击进入管理员的Dashboard界面。如果时间间隔过长,可以要求运维人员重置密码。操作和维护人员可以参考第一步来重置用户的密码。实际上,您也可以通过参考第三步中的方法找到自己的密码,而不必麻烦操作和维护...