opencv Moments(矩)

1.矩

严格来讲矩是概率与统计中的一个概念,是随机变量的一种数字特征。

维基百科 In mathematics, a moment is a specific quantitative measure, used in both mechanics and statistics, of the shape of a set of points. If the points represent mass, then the zeroth moment is the total mass, the first moment divided by the total mass is the center of mass, and the second moment is the rotational inertia. If the points represent probability density, then the zeroth moment is the total probability (i.e. one), the first moment is the mean, the second central moment is the variance, the third moment is the skewness, and the fourth moment (with normalization and shift) is the kurtosis. The mathematical concept is closely related to the concept of moment in physics.For a bounded distribution of mass or probability, the collection of all the moments (of all orders, from 0 to ∞) uniquely determines the distribution.

在这里插入图片描述
数学期望 E ( x ) E(x) E(x)就是当k=1,c=0时的矩,即一阶原点矩
方差就是二阶中心矩

2 .期望和方差

在这里插入图片描述
图片出处:添加链接描述

3.OpenCV中图像的矩

opencv 中有三种矩 :空间矩 、中心矩、归一化的中心矩

  1. 空间矩的公式
    在这里插入图片描述
  2. 中心矩的公式
    在这里插入图片描述
  3. 归一化的中心矩
    在这里插入图片描述
class CV_EXPORTS_W_MAP Moments
{
public:
    //! the default constructor
    Moments();
    //! the full constructor
    Moments(double m00, double m10, double m01, double m20, double m11,
            double m02, double m30, double m21, double m12, double m03 );
    ! the conversion from CvMoments
    //Moments( const CvMoments& moments );
    ! the conversion to CvMoments
    //operator CvMoments() const;

    //! @name spatial moments
    //! @{
    CV_PROP_RW double  m00, m10, m01, m20, m11, m02, m30, m21, m12, m03;
    //! @}

    //! @name central moments
    //! @{
    CV_PROP_RW double  mu20, mu11, mu02, mu30, mu21, mu12, mu03;
    //! @}

    //! @name central normalized moments
    //! @{
    CV_PROP_RW double  nu20, nu11, nu02, nu30, nu21, nu12, nu03;
    //! @}
};

4.计算图像矩的作用

在这里插入图片描述
在这里插入图片描述
角度的计算:
在这里插入图片描述
在这里插入图片描述
图像的矩通常描述了该图像形状的全局特征,并被广泛的应用在各种图像处理、计算机视觉和机器人技术领域的目标识别与方位估计中。一阶矩与形状有关,二阶矩显示曲线围绕直线平均值的扩展程度,三阶矩则是关于平均值的对称性的测量。不变矩是图像的统计特性,满足平移、伸缩、旋转均不变的不变性。

如果把图像看成是一块质量密度不均匀的薄板,其图像的灰度分布函数f(x,y)就是薄板的密度分布函数,则其各阶矩有着不同的含义,如零阶矩表示它的总质量;一阶矩表示它的质心;二阶矩又叫惯性矩,表示图像的大小和方向。事实上,如果仅考虑阶次为2的矩集,则原始图像等同于一个具有确定的大小、方向和离心率,以图像质心为中心且具有恒定辐射率的椭圆。

由三阶矩以下矩构成的七个矩不变量具有平移、旋转和尺度不变性等等。当密度分布函数发生改变时,图像的实质没有改变,仍然可以看做一个薄板,只是密度分布有所改变。虽然此时各阶矩的值可能发生变化,但由各阶矩计算出的不变矩仍具有平移、旋转和尺度不变性。通过这个思想,可对图像进行简化处理,保留最能反映目标特性的信息,再用简化后的图像计算不变矩特征,可减少计算量。

void StartOp::ImageProcess29()
{
	
	Mat src;
	src = imread("../../Images/11.png", 1);
	if (!src.data) {
		cout << "文件打开失败" << endl;
	}
	namedWindow("input", WINDOW_KEEPRATIO);
	imshow("input", src);

	/*
		cvtColor(src,src,COLOR_BGR2GRAY);
		threshold(src,src,200,255,THRESH_BINARY_INV);
		Moments m = moments(src,true);
		//重心
		Point2d center(m.m10/m.m00,m.m01/m.m00);
		//计算方向
		double a = m.m20 / m.m00 - center.x*center.x;
		double b = m.m11 / m.m00 - center.x*center.y;
		double c = m.m02 / m.m00 - center.y*center.y;
		double theta = fastAtan2(2 * b, (a - c)) / 2;//此为形状的方向
	*/


	Mat dst;
	cvtColor(src,dst,COLOR_BGR2GRAY);
	GaussianBlur(dst,dst,Size(3,3),0,0);

	Canny(dst, dst, 200, 200 * 2, 3, false);

	vector<vector<Point>> contours;
	vector<Vec4i> hierachy;
	findContours(dst, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0,0));

	vector<Moments> contours_moments(contours.size());
	vector<Point2f> ccs(contours.size());
	for (size_t i = 0; i < contours.size(); i++) {
		/*
		moments
		第一个参数,输入参数,可以是光栅图像(单通道,8位或浮点的二维数组)或二维数组(1N或N1)。 
		第二个参数,默认值false,若此参数取true,则所有非零像素为1.此参数仅对图像使用。
		*/
		contours_moments[i] = moments(contours[i]);
		ccs[i] = Point(static_cast<float>(contours_moments[i].m10 / contours_moments[i].m00), static_cast<float>(contours_moments[i].m01 / contours_moments[i].m00));
	}

	RNG rng(2345);
	for (size_t i = 0; i < contours.size(); i++) {

		Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
		printf("center point x : %.2f y : %.2f\n", ccs[i].x, ccs[i].y);
		/*
		计算轮廓面积–contourArea函数
			double contourArea(inputArray contour, bool		oriented=false) 
			*第一个参数,输入的向量,二维点(轮廓顶点)。 
			*第二个参数,面向区域标识符,若为true,该函数返回一个带符号的面积值,其正负取决于轮廓的方向(顺时针还是逆时针)。根据这个特性我们可以根据面积的符号来确定轮廓的位置。需要注意的是,这个参数有默认值false,表示以绝对值返回,不带符号。
		计算轮廓长度–arcLength函数 
			double arcLength(inputArray curve,bool closed) 
			*第一个参数,输入的二维点集。 
			*第二个参数,一个用于指示曲线是否封闭的标识符,默认值closed,表示曲线封闭。		
		*/
		printf("contours %d area : %.2f   arc length : %.2f  mooArea: %.2f\n", i, contourArea(contours[i]), arcLength(contours[i], true), contours_moments[i].m00);
		drawContours(dst, contours, i, color, 2, 8, hierachy, 0, Point(0, 0));
		circle(dst, ccs[i], 2, color, 2, 8);
	}
	imshow("output",dst);
}

参考博客:图像的矩特征

图像的矩,以及利用矩求图像的重心,方向

矩:数学矩-图像矩

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值