EmguCV学习笔记 VB.Net 6.S 特别示例

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。

EmguCV是一个基于OpenCV的开源免费的跨平台计算机视觉库,它向C#和VB.NET开发者提供了OpenCV库的大部分功能。

教程VB.net版本请访问:EmguCV学习笔记 VB.Net 目录-CSDN博客

教程C#版本请访问:EmguCV学习笔记 C# 目录-CSDN博客

笔者的博客网址:https://blog.csdn.net/uruseibest

教程配套文件及相关说明以及如何获得pdf教程和代码,请移步:EmguCV学习笔记

学习VB.Net知识,请移步: vb.net 教程 目录_vb中如何用datagridview-CSDN博客

 学习C#知识,请移步:C# 教程 目录_c#教程目录-CSDN博客

 

6.S 特别示例

6.S.1图像中的圆半径

本示例来自一名网友咨询如何获得图像中圆形的半径,其中有两个十字作为标记,十字之间距离为100mm。如下图:

图6-28 根据给出的定位标记求圆半径

【代码位置:frmChapter6_S1】Button1_Click、PointFToPoint

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim msrc As New Mat("C:\learnEmgucv\celiang.jpg", ImreadModes.Color)

        Dim mgray As New Mat()

        CvInvoke.CvtColor(msrc, mgray, ColorConversion.Bgr2Gray)

        Dim kernel As New Mat

        kernel = CvInvoke.GetStructuringElement(ElementShape.Cross, New Drawing.Size(3, 3), New Point(-1, -1))

        Dim merode As New Mat        '

        '这里使用了2次迭代

        CvInvoke.Dilate(mgray, merode, kernel, New Point(-1, -1), 1, BorderType.Constant, Nothing)

        CvInvoke.Threshold(merode, merode, 200, 255, ThresholdType.BinaryInv)

        ImageBox1.Image = merode

        '获得所有轮廓

        Dim contours As New VectorOfVectorOfPoint

        Dim hierarchy As New VectorOfRect

        CvInvoke.FindContours(merode, contours, hierarchy, RetrType.List, ChainApproxMethod.ChainApproxSimple)

        Dim m2 As New Mat(merode.Size, DepthType.Cv8U, 1)

        m2.SetTo(New MCvScalar(0))

        '圆轮廓

        Dim contourCircle As VectorOfPoint

        '圆轮廓的周长

        Dim perimeter As Double

        '绘制轮廓

        For i As Integer = 0 To contours.Size - 1

            Dim carea As VectorOfPoint = contours(i)

            '获得轮廓面积

            Dim area As Double = CvInvoke.ContourArea(carea, False)

            '符合条件时,绘制轮廓,排除圆形,只保留十字线

            '本图中圆形面积为2449,直线面积为8,需要根据实际情况调整

            If area < 200 Then

                CvInvoke.DrawContours(m2, contours, i, New MCvScalar(255), 0.4)

            Else

                '得到圆形,图像中只有三个轮廓:2个交叉十字线段、1个圆形

                '这里简化操作,否则在多个轮廓情况下,应获取最大面积的轮廓判断为圆形

                contourCircle = contours(i)

                '获取轮廓周长

                perimeter = CvInvoke.ArcLength(contourCircle, True)

            End If

        Next

        ImageBox1.Image = m2

        '使用HoughLinesP方法检测图像中的直线,并将其绘制到图像

        '因为本图中十字线上的线段较短,所以这里阈值设置很小

        Dim lines As LineSegment2D() = CvInvoke.HoughLinesP(m2, 1, Math.PI / 180, 5, 5, 80)

        Dim m3 As New Mat(merode.Size, DepthType.Cv8U, 3)

        m3.SetTo(New MCvScalar(0, 0, 0))

        For Each line As LineSegment2D In lines

            CvInvoke.Line(m3, line.P1, line.P2, New MCvScalar(0, 255, 0), 2)

        Next

        ImageBox2.Image = m3

        '对直线进行分类,将其分为垂直和水平两类:

        Dim verticalLines As New List(Of LineSegment2D)

        Dim horizontalLines As New List(Of LineSegment2D)

        '计算每条直线的倾斜角度来进行分类,

        '将倾斜角度在60 - 120度之间的直线划分为垂直类,

        '将倾斜角度在30 - 150度之间的直线划分为水平类。

        For Each line As LineSegment2D In lines

            Dim angle As Double = Math.Atan2(line.P2.Y - line.P1.Y, line.P2.X - line.P1.X) * 180 / Math.PI

            If angle < 0 Then angle += 180

            If angle > 60 AndAlso angle < 120 Then

                verticalLines.Add(line)

            ElseIf angle > 150 OrElse angle < 30 Then

                horizontalLines.Add(line)

            End If

        Next

        '对垂直和水平直线进行匹配,并计算十字中心点的位置:

        Dim intersections As New List(Of PointF)

        '得到两个相交点

        For Each verticalLine As LineSegment2D In verticalLines

            For Each horizontalLine As LineSegment2D In horizontalLines

                '基于图像中两条直线真实相交,

                '如果垂直线的中点X坐标在水平线两端点X坐标之间

                '那么,这条垂直线段和这条水平线段相交

                Dim centerX As Single = (verticalLine.P1.X + verticalLine.P2.X) / 2

                If horizontalLine.P1.X < horizontalLine.P2.X Then

                    If centerX > horizontalLine.P1.X And centerX < horizontalLine.P2.X Then

                        Dim intersectionPoint As New PointF(

                            (horizontalLine.P1.X + horizontalLine.P2.X + verticalLine.P1.X + verticalLine.P2.X) / 4,

                            (horizontalLine.P1.Y + horizontalLine.P2.Y + verticalLine.P1.Y + verticalLine.P2.Y) / 4

                        )

                        intersections.Add(intersectionPoint)

                    End If

                Else

                    If centerX > horizontalLine.P2.X And centerX < horizontalLine.P1.X Then

                        Dim intersectionPoint As New PointF(

                            (horizontalLine.P1.X + horizontalLine.P2.X + verticalLine.P1.X + verticalLine.P2.X) / 4,

                            (horizontalLine.P1.Y + horizontalLine.P2.Y + verticalLine.P1.Y + verticalLine.P2.Y) / 4

                        )

                        intersections.Add(intersectionPoint)

                    End If

                End If

            Next

        Next

        If intersections.Count <> 2 Then

            MessageBox.Show("未能获得两个十字线的交叉点")

            Exit Sub

        End If

        CvInvoke.Line(msrc, PointFToPoint(intersections(0)), PointFToPoint(intersections(1)), New MCvScalar(0, 255, 0), 2)

        CvInvoke.Imshow("m3", msrc)

        '计算两个交点的距离

        Dim distance As Double = Math.Sqrt(

            (intersections(0).X - intersections(1).X) ^ 2 +

            (intersections(0).Y - intersections(1).Y) ^ 2

        )

        '实际中两交点距离为100毫米,计算相应比例

        Dim proportion As Double = 100 / distance

        '以下是基于最小外接圆来计算实际圆半径

        Dim cf As CircleF

        cf = CvInvoke.MinEnclosingCircle(contourCircle)

        '获得外接圆形

        CvInvoke.Circle(msrc, New Point(CInt(cf.Center.X), CInt(cf.Center.Y)), cf.Radius, New MCvScalar(0, 0, 255), 2)

        CvInvoke.Imshow("m4", msrc)

        '实际圆半径

        Dim realradius1 As Double

        realradius1 = proportion * cf.Radius

        '以下是基于轮廓周长来计算实际圆半径

        '实际圆周长

        Dim realperimeter As Double = perimeter * proportion

        '图像中的圆半径

        Dim radius As Double

        radius = (perimeter / Math.PI) / 2

        '实际圆半径

        Dim realradius2 As Double

        realradius2 = proportion * radius

        MessageBox.Show("最小外接圆来计算实际圆半径:" & realradius1 & ControlChars.CrLf &

                        "基于轮廓周长来计算实际圆半径:" & realradius2)

    End Sub

输出结果如下图所示:

图6-29 求出的圆半径

  • 12
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VB.NET是一种面向对象的编程语言,它是微软公司开发的一种基于.NET框架的编程语言。下面是一些VB.NET学习笔记的内容: 1. 基本语法:VB.NET的语法与其他编程语言类似,包括变量声明、条件语句、循环语句等。学习者需要掌握基本的语法规则和关键字。 2. 数据类型:VB.NET支持多种数据类型,包括整型、浮点型、字符型、布尔型等。学习者需要了解各种数据类型的特点和使用方法。 3. 控制结构:学习者需要掌握条件语句(如if语句、switch语句)和循环语句(如for循环、while循环)等控制结构,以实现程序的流程控制。 4. 函数和过程:VB.NET支持函数和过程的定义和调用。学习者需要了解函数和过程的区别,以及如何传递参数和返回值。 5. 面向对象编程:VB.NET是一种面向对象的编程语言,学习者需要了解类、对象、继承、多态等面向对象的概念和使用方法。 6. 异常处理:学习者需要了解如何使用try-catch语句来捕获和处理异常,以提高程序的健壮性。 7. 文件操作:学习者需要了解如何读写文件、创建和管理文件夹等文件操作的基本知识。 8. 数据库编程:VB.NET可以与数据库进行交互,学习者需要了解如何连接数据库、执行SQL语句、读写数据库等数据库编程的基本知识。 9. GUI编程:VB.NET提供了丰富的GUI组件和控件,学习者需要了解如何设计窗体、添加控件、处理事件等GUI编程的基本知识。 10. 调试和测试:学习者需要了解如何使用调试工具来调试程序,以及如何编写和运行单元测试来验证程序的正确性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值