【C#】用EmguCV 绘制各种轮廓线

1 篇文章 0 订阅
1 篇文章 0 订阅
本文介绍了如何在C#中使用EmguCV进行图像处理,特别是寻找和绘制轮廓线的方法,包括BoundingBox、ConvexHull、MinAreaBoundingBox和MinAreaCircle等。通过示例代码和对比,探讨了不同方法在处理复杂形状时的效果和适用场景。
摘要由CSDN通过智能技术生成

一般在做影像处理时,为提升效率,常会将影像转为二值影像后再进行处理。
在EmguCV内有许多找轮廓线的方法,但是随着版本更新,不同版本的函数
不见得会一样,每次都要重新查询实在很麻烦,那不如把他们记下来。

版本概要:
EmguCV版本:3.2.0.2682
编译器版本: Visual Studio 2017 Community
方案平台: x64 (许多导致程式无法执行的原因是因为没有改执行平台!)

正文开始。
首先我们用小画家画了一张图来作为范本—一朵云。
因为形状奇特,非常适合用来说明。
这里写图片描述

1. BoundingBox: 可以框住全部范围的矩形。

这是没有经过旋转地矩形,有经过旋转的矩形在后面讨论。

using System;
using System.Windows.Forms;
using System.Drawing;

using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
using Emgu.CV.Util;


namespace Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            Image<Gray, byte> I = new Image<Gray, byte>(@"D:\Test\1.jpg");
            Image<Bgr, byte> DrawI = I.Convert<Bgr, byte>();

            Image<Gray, byte> CannyImage = I.Clone();
            CvInvoke.Canny(I, CannyImage, 255, 255, 5, true);

            MyCV.BoundingBox(CannyImage, DrawI);
            pictureBox1.Image = DrawI.Bitmap;
        }
    }

    public class MyCV
    {
        public static void BoundingBox(Image<Gray, byte> src, Image<Bgr, byte> draw)
        {
            using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())
            {
                CvInvoke.FindContours(src, contours, null, RetrType.External,
                                      ChainApproxMethod.ChainApproxSimple);

                int count = contours.Size;
                for (int i = 0; i < count; i++)
                {
                    using (VectorOfPoint contour = contours[i])
                    {
                        Rectangle BoundingBox = CvInvoke.BoundingRectangle(contour);
                        CvInvoke.Rectangle(draw, BoundingBox, new MCvScalar(255, 0, 255, 255), 3);
                    }
                }
            }
        }
    }
}

这里写图片描述

注:后面的程式码仅写出操作的函数,省略主视窗及名称空间,请自行代换主视窗的程式码。

在这边常有看到一些范例程式会建议使用ApproxPolyDP这个方法,取得近似的形状,
经过测试,若是在一些精度需求不高的情况下可以这么做,但就这个云形的例子而言不建议这样做。
下面是采用ApproxPolyDP函数的程式码与结果。

public static void ApproxBoundingBox(Image<Gray, byte> src, Image<Bgr, byte> draw)
{
    using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())
    {
        CvInvoke.FindContours(src, contours, null, RetrType.External,
                              ChainApproxMethod.ChainApproxSimple);

        int count = contours.Size;
        for (int i = 0; i < count; i++)
        {
            using (VectorOfPoint contour = contours[i])
            using (VectorOfPoint approxContour = new VectorOfPoint())
            {
                CvInvoke.ApproxPolyDP(contour, approxContour, CvInvoke.ArcLength(contour, true) * 0.05, true);
                Rectangle BoundingBox = CvInvoke.BoundingRectangle(approxContour);
                CvInvoke.Rectangle(draw, BoundingBox, new MCvScalar(255, 0, 255, 255), 3);
            }
        }
    }
}

这里写图片描述

可以看到有许多卷卷的地方,都被近似掉了,以至于框选出来的范围会失真。
但,若目标是长方形或三角形这种比较规则的形状,使用近似的方法可以提升执行的效率。

其实若是直接把轮廓线画出来就可以看得更清楚,近似后许多细节会消失。
以下是程式码与执行结果。

public static void DrawContour(Image<Gray, byte> src, Image<Bgr, byte> draw)
{
    using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())
    {
        CvInvoke.FindContours(src, contours, null, RetrType.External,
                                ChainApproxMethod.ChainApproxSimple);

        int count = contours.Size;
        for (int i = 0; i < count; i++)
        {
            using (VectorOfPoint contour = contours[i])
            using (VectorOfPoint approxContour = new VectorOfPoint())
            {
                // 原始輪廓線
                CvInvoke.DrawContours(draw, contours, i, new MCvScalar(255, 0, 255, 255), 3);

                // 近似後輪廓線
                CvInvoke.ApproxPolyDP(contour, approxContour, 
                                      CvInvoke.ArcLength(contour, true) * 0.02, true);
                Point[] pts = approxContour.ToArray();
                for(int j=0; j<pts.Length; j++)
                {
                    Point p1 = new Point(pts[j].X, pts[j].Y);
                    Point p2;

                    if (j == pts.Length - 1)
                        p2 = new Point(pts[0].X, pts[0].Y);
                    else
                        p2 = new Point(pts[j+1].X, pts[j+1].Y);

                    CvInvoke.Line(draw, p1, p2, new MCvScalar(255, 0, 0, 0), 3);
                }
            }
        }
    }
}

这里写图片描述

2. ConvexHull: 可以框住区块的最小多边形。

public static void ConvexHull(Image<Gray, byte> src, Image<Bgr, byte> draw)
{
    using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())
    {
        CvInvoke.FindContours(src, contours, null, RetrType.External,
                                ChainApproxMethod.ChainApproxSimple);

        int count = contours.Size;
        for (int i = 0; i < count; i++)
        {
            using (VectorOfPoint contour = contours[i])
            {
                PointF[] temp = Array.ConvertAll(contour.ToArray(),
                                                new Converter<Point, PointF>(Point2PointF));
                PointF[] pts = CvInvoke.ConvexHull(temp, true);

                for (int j = 0; j < pts.Length; j++)
                {
                    Point p1 = new Point((int)pts[j].X, (int)pts[j].Y);
                    Point p2;

                    if (j == pts.Length - 1)
                        p2 = new Point((int)pts[0].X, (int)pts[0].Y);
                    else
                        p2 = new Point((int)pts[j + 1].X, (int)pts[j + 1].Y);

                    CvInvoke.Line(draw, p1, p2, new MCvScalar(255, 0, 255, 255), 3);
                }
            }
        }
    }
}

private static PointF Point2PointF(Point P)
{
    PointF PF = new PointF
    {
        X = P.X,
        Y = P.Y
    };
    return PF;
}

这里写图片描述

3. MinAreaBoundingBox: 可框住区域的最小矩形。

这是可旋转的矩形,意即找到面积最小,又可以框住该区域的矩形。

public static void MinAreaBoundingBox(Image<Gray, byte> src, Image<Bgr, byte> draw)
{
    using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())
    {
        CvInvoke.FindContours(src, contours, null, RetrType.External,
                                ChainApproxMethod.ChainApproxSimple);

        int count = contours.Size;
        for (int i = 0; i < count; i++)
        {
            using (VectorOfPoint contour = contours[i])
            {
                RotatedRect BoundingBox = CvInvoke.MinAreaRect(contour);
                CvInvoke.Polylines(draw, Array.ConvertAll(BoundingBox.GetVertices(), Point.Round), 
                                   true, new Bgr(Color.DeepPink).MCvScalar, 3);
            }
        }
    }
}

这里写图片描述

4. MinAreaCircle:可框住区域的最小圆形。

public static void MinAreaCircle(Image<Gray, byte> src, Image<Bgr, byte> draw)
{
    using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())
    {
        CvInvoke.FindContours(src, contours, null, RetrType.External,
                                ChainApproxMethod.ChainApproxSimple);

        int count = contours.Size;
        for (int i = 0; i < count; i++)
        {
            using (VectorOfPoint contour = contours[i])
            {
                CircleF circle = CvInvoke.MinEnclosingCircle(contour);       
                CvInvoke.Circle(draw, new Point((int)circle.Center.X, (int) circle.Center.Y),
                                (int)circle.Radius, new MCvScalar(255, 0, 255, 255), 3);
            }
        }
    }
}

这里写图片描述
在EmguCV内一种轮廓线就一种画法。
真的是要足够熟练才能够驾驭这些函数唉!

像是可怜的小夏已经陷在这些函数内好几天了,真是头昏眼花,临表泣涕,不知所云。

翻译自:dotblogs.com.tw 夏恩的程式笔记

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值