问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

Emgu CV教程:使用Moments()函数计算轮廓矩和质心

创作时间:
作者:
@小白创作中心

Emgu CV教程:使用Moments()函数计算轮廓矩和质心

引用
CSDN
1.
https://blog.csdn.net/sswss12345/article/details/137454996

Emgu CV是一个基于OpenCV的C#计算机视觉库,提供了丰富的图像处理和计算机视觉功能。其中,Moments()函数用于计算图像轮廓的矩,可以进一步计算轮廓的质心、面积等特征。本文将详细介绍Moments()函数的使用方法,并通过一个汉字“之”的轮廓分析实例,展示如何计算和可视化轮廓的质心。

一、概念介绍

1. 矩

矩(英文叫moment)是一个数学中的概念,主要用于描述图像的形状特征。虽然矩的数学定义较为复杂,但实际应用中并不需要深入了解其数学细节。在Emgu CV中,我们主要关注如何使用矩函数进行图形特征提取。

2. 矩能干什么

矩可以用来描述轮廓的多种特性,包括:

  1. 零阶矩:表示轮廓的面积。
  2. 一阶矩:可以用来计算图像的质心。
  3. 二阶矩:可以提供关于图像周长、长轴、短轴等信息。

3. 矩函数

在Emgu CV中,计算矩的函数是Moments(),其官方定义如下:

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

二、演示

1. 原始素材

原始图像srcMat如下图所示,是一个汉字“之”。我们的目标是利用代码找到每个笔画的质心,即图像中轮廓的平面中心。

2. 代码

为了实现这一目标,我们需要进行以下步骤:

  1. 将图像转换为灰度图并进行二值化处理。
  2. 检索图像中的轮廓。
  3. 计算每个轮廓的矩和质心。
  4. 在图像上绘制轮廓和质心位置。

以下是完整的代码实现:

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. 运行结果

运行上述代码后,我们可以得到汉字“之”的四个笔画轮廓及其质心位置。每个笔画轮廓的质心用红色小圆形标注出来。通过Moments()函数计算得到的零阶矩(M00属性值)即为轮廓的面积。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号