cad怎样生成轮廓线(图样中可见轮廓线用什么线)

一般在做影像处理时,为提升效率,常会将影像转为二值影像后再进行处理。在EmguCV内有许多找轮廓线的方法,但是随着版本更新,不同版本的函数不见得会一样,每次都要重新查询实在很麻烦,那不如把他们记下来。版本概要:EmguCV版本:3.2.0.2682编译器版本:VisualStudio2017Community方案平台:x64(许多导致程式无法执行的原因是因为没有改执

大家好,又见面了,我是你们的朋友全栈君。

一般在做影像处理时,为提升效率,常会将影像转为二值影像后再进行处理。
在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 夏恩的程式笔记

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/129492.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 指令的四个周期_cpu指令周期流程图

    指令的四个周期_cpu指令周期流程图指令流程图的概念菱形:译码,测试,表示判断,如零指令字是0或者1.与前面的CPU周期紧密相连,不单独占用CPU周期。每个方框箭头下面的是公共操作符符号,表示一条指令结束。mov指令将R1寄存器的数据存储到R2寄存器中,lad指令时间主存中的数据存储到寄存器中。sto是将R2中的数据根据R3中的主存地址存储到主存中。lad和sto是寄存器-主存指令需要三个CPU周期,其他都是寄存器-…

    2022年10月13日
    0
  • 有极性电容和无极性电容的区别_非极性电容

    有极性电容和无极性电容的区别_非极性电容有极性电容与无极性电容的概述有极性电容与无极性电容的概述有极性电容的识别有极性电容于无极性电容的区别网友见解有极性电容与无极性电容的概述理想的电容,本来是没有极性的。但是在实际中,为了获得大容量,就使用了某些特殊的材料和结构,这就导致了实际的电容有些是有极性的。常见的有极性电容有铝电解电容,钽电解电容等。电解电容一般是容量相对比较大的。如果要做一个大容量的无极性电容,就没那…

    2022年8月22日
    4
  • Python生成器建议收藏

    1.生成器使用yield语句,每次产生一个值,函数就会被冻结2.列表推导式可以用来创建list例:生成[1*1,2*2,3*3,4*4,5*5]的列表,即[1,4,9,16,25]

    2021年12月18日
    38
  • 清除vs2005、vs2008起始页最近打开项目

    清除vs2005、vs2008起始页最近打开项目

    2021年11月17日
    40
  • word目录链接无法跳转_怎样跳转网页

    word目录链接无法跳转_怎样跳转网页概述目前使用的next版本是5.1.4,文章左侧的目录一直不能跳转也不能展开,按网上的办法一直没法解决,今天自己琢磨了一阵总算搞定了。由于发现遇到这个问题的人不少,特此总结一下。一般分为

    2022年8月16日
    13
  • MyBatis 所有的 jdbcType类型

    MyBatis 所有的 jdbcType类型MyBatis处理MySQL字段类型date与datetime1)DATETIME显示格式:yyyy-MM-ddHH:mm:ss时间范围:[‘1000-01-0100:00:00’到’9999-12-3123:59:59’]2)DATE显示格式:yyyy-MM-dd时间范围:[‘1000-01-01’到’9999-12-31’]3)TIMESTAMP显示格式:yyyy-MM-ddHH:mm:ss时间范围:[‘1970-01-0100:00:00’到’2037-12-

    2022年10月20日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号