【Emgu CV教程】10.12、Moments()函数计算轮廓矩和质心

发布于:2024-04-08 ⋅ 阅读:(82) ⋅ 点赞:(0)


一、概念介绍

1.矩

矩,英文叫moment,是一个数学中的概念,以下的解释来自百度百科:
在这里插入图片描述
是不是看不懂,没关系,数学基础不好的,根本不需要了解以上概念,只需要学会使用Emgu.CV中的矩函数进行图形特征提取就行。

2.矩能干什么

矩可以用来描述轮廓的均值、方差、偏度、峰度等特性。矩的性质(或者说具体的应用)如下:

  1. 零阶矩是轮廓的均值,可以用来描述图像的面积。
  2. 一阶矩是轮廓的均方根,可以用来描述图像的质心。
  3. 二阶矩是轮廓的方差,可以用来描述图像的周长、长轴、短轴、扭矩等信息。

3.矩函数

矩函数是Moments(),官方定义如下:

public static Moments Moments
(
	IInputArray arr, // 输入轮廓
	bool binaryImage = false // 默认值false,若此参数取true,则所有非零像素为1。
)

二、演示

1.原始素材

原始图像srcMat如下图:
在这里插入图片描述
是一个汉字“之”,现在我们要利用代码找到每个笔画的质心,也就是图像中轮廓的平面中心。

2.代码

轮廓检索模式要选择RetrType.External,代码如下:

Mat tempMat = srcMat.Clone();
Mat dstMat = srcMat.Clone();
Mat gray = new Mat();
int threshold = 40;

// 转成灰度图再二值化
CvInvoke.CvtColor(tempMat, gray, ColorConversion.Bgr2Gray);
CvInvoke.Threshold(gray, gray, threshold, 255, ThresholdType.Binary);

// 定义轮廓集合
VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
VectorOfRect hierarchy = new VectorOfRect();

CvInvoke.FindContours(gray, contours, hierarchy, RetrType.External, ChainApproxMethod.ChainApproxNone);

// 在一张黑色图中画出所有轮廓
Mat allContours = new Mat(new System.Drawing.Size(gray.Cols, gray.Rows), DepthType.Cv8U, 1);
allContours.SetTo(new MCvScalar(0, 0, 0));
CvInvoke.DrawContours(allContours, contours, -1, new MCvScalar(255, 255, 255), 1);

// 按照面积筛选,太小的轮廓不计算
Dictionary<int, double> dict = new Dictionary<int, double>();
if (contours.Size > 0)
{
    for (int i = 0; i < contours.Size; i++)
    {
        double area = CvInvoke.ContourArea(contours[i]);
        Rectangle rect = CvInvoke.BoundingRectangle(contours[i]);

        if (rect.Width > 20 && rect.Height > 20 && area < 3000000)
        {
            dict.Add(i, area);
        }
    }
}

var item = dict.OrderByDescending(v => v.Value); // v.Value就代表面积,是降序排列

// 计算轮廓中心并画出
int ksize = item.Count();
double[] m00 = new double[ksize];
double[] m01 = new double[ksize];
double[] m10 = new double[ksize];
System.Drawing.Point[] gravity = new System.Drawing.Point[ksize]; // 用于存储轮廓质心坐标
Moments[] moments = new Moments[ksize]; // 创建MCvMoments数组

// 开始画轮廓的最小正外接矩形
int index = 0;
foreach (var it in item)
{
    int key = it.Key;
    VectorOfPoint contour = contours[key];
    moments[index] = CvInvoke.Moments(contour, false); // 计算当前轮廓的矩
    m00[index] = moments[index].M00;
    m01[index] = moments[index].M01;
    m10[index] = moments[index].M10;
    int x = Convert.ToInt32(m10[index] / m00[index]); // 计算当前轮廓质心坐标
    int y = Convert.ToInt32(m01[index] / m00[index]);
    gravity[index] = new System.Drawing.Point(x, y);
    Rectangle rect = CvInvoke.BoundingRectangle(contours[key]);
    index++;
    CvInvoke.PutText(dstMat, "Contour:" + index.ToString(), new System.Drawing.Point(rect.X, rect.Y - 10), FontFace.HersheyComplex, 0.5, new Bgr(0, 255, 0).MCvScalar, 1, LineType.EightConnected, false);
}

CvInvoke.PutText(dstMat, "Contours number:" + item.Count(), new System.Drawing.Point(20, 20), FontFace.HersheyComplex, 0.5, new Bgr(0, 255, 0).MCvScalar, 1, LineType.EightConnected, false);
CvInvoke.DrawContours(dstMat, contours, -1, new MCvScalar(0, 255, 0), 2, LineType.EightConnected, hierarchy);

// 画出中心点位置
foreach (System.Drawing.Point centerPoint in gravity)
{
    CvInvoke.Circle(dstMat, centerPoint, 2, new MCvScalar(0, 0, 255), 2);
}

CvInvoke.Imshow("All contours, " + allContours.Size.ToString(), allContours);
CvInvoke.Imshow("Final result image, " + dstMat.Size.ToString(), dstMat); // 显示最终结果

3.运行结果

在这里插入图片描述

一共四笔写成的“之”字,每一个笔画轮廓的质心用红色小圆形标注出来。刚才说的零阶矩是轮廓的的面积,请看下面:
在这里插入图片描述
利用Moment()函数求出矩后,其下面包含的M00属性值,就是轮廓的面积。


原创不易,请勿抄袭。共同进步,相互学习。