版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
即将推出EmguCV的教程,请大家还稍作等待。
之前网友咨询如何获得图像中圆形的半径,其中有两个十字作为标定,十字之间距离为100mm。如下图:
说实在的,单靠VB.net很难获得相关圆形信息,为了弥补这部分知识,下定决心学习了EmguCV。以下是具体代码:https://blog.csdn.net/UruseiBest
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 Matkernel = 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)'获得所有轮廓 https://blog.csdn.net/UruseiBestDim contours As New VectorOfVectorOfPointDim hierarchy As New VectorOfRectCvInvoke.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'绘制轮廓 https://blog.csdn.net/UruseiBestFor i As Integer = 0 To contours.Size - 1Dim carea As VectorOfPoint = contours(i)'获得轮廓面积Dim area As Double = CvInvoke.ContourArea(carea, False)'符合条件时,绘制轮廓,排除圆形,只保留十字线 '本图中圆形面积为2449,直线面积为8,需要根据实际情况调整If area < 200 ThenCvInvoke.DrawContours(m2, contours, i, New MCvScalar(255), 0.4)Else'得到圆形,图像中只有三个轮廓:2个交叉十字线段、1个圆形'这里简化操作,否则在多个轮廓情况下,应获取最大面积的轮廓判断为圆形contourCircle = contours(i)'获取轮廓周长perimeter = CvInvoke.ArcLength(contourCircle, True)End IfNextImageBox1.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 linesCvInvoke.Line(m3, line.P1, line.P2, New MCvScalar(0, 255, 0), 2)NextImageBox2.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 linesDim angle As Double = Math.Atan2(line.P2.Y - line.P1.Y, line.P2.X - line.P1.X) * 180 / Math.PIIf angle < 0 Then angle += 180If angle > 60 AndAlso angle < 120 ThenverticalLines.Add(line)ElseIf angle > 150 OrElse angle < 30 ThenhorizontalLines.Add(line)End IfNext'对垂直和水平直线进行匹配,并计算十字中心点的位置:Dim intersections As New List(Of PointF)'得到两个相交点 https://blog.csdn.net/UruseiBestFor Each verticalLine As LineSegment2D In verticalLinesFor Each horizontalLine As LineSegment2D In horizontalLines'基于图像中两条直线真实相交,'如果垂直线的中点X坐标在水平线两端点X坐标之间'那么,这条垂直线段和这条水平线段相交Dim centerX As Single = (verticalLine.P1.X + verticalLine.P2.X) / 2If horizontalLine.P1.X < horizontalLine.P2.X ThenIf centerX > horizontalLine.P1.X And centerX < horizontalLine.P2.X ThenDim 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 IfElseIf centerX > horizontalLine.P2.X And centerX < horizontalLine.P1.X ThenDim 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 IfEnd IfNextNextIf intersections.Count <> 2 ThenMessageBox.Show("未能获得两个十字线的交叉点")Exit SubEnd IfCvInvoke.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 CircleFcf = CvInvoke.MinEnclosingCircle(contourCircle)'获得外接圆形 https://blog.csdn.net/UruseiBestCvInvoke.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 Doublerealradius1 = proportion * cf.Radius'以下是基于轮廓周长来计算实际圆半径'实际圆周长Dim realperimeter As Double = perimeter * proportion'图像中的圆半径 https://blog.csdn.net/UruseiBestDim radius As Doubleradius = (perimeter / Math.PI) / 2'实际圆半径Dim realradius2 As Doublerealradius2 = proportion * radiusMessageBox.Show("最小外接圆来计算实际圆半径:" & realradius1 & ControlChars.CrLf &"基于轮廓周长来计算实际圆半径:" & realradius2)
这个网友当时提出来问题的时候,我还没有办法解决,不过经过不断学习,目前已经学习了不少相关知识,至少可以获得圆半径了,还是略微感到欣慰。
关于EmguCV的知识,下一步整理出来。
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参看vb.net 教程 目录