废话不多说上源码
觉得帖子有用给点个赞哈
先来个效果图
下边的是源码,自己新建一个文件粘贴进去,包含到您现在的项目 中。这串源码后边是使用方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HalconDotNet;public class CircleMeasure{/// <summary>/// 检测形态信息/// </summary>public Metrology_INFO m_MetrologyInfo;//圆形测量参数对象public Circle_INFO m_CircleInfo;//圆形测量基准圆形参数对象public Circle_INFO m_OutCircleInfo;//圆形测量完成参数对象#region 卡尺圆形测量参数信息/// <summary>/// 圆信息/// </summary>public struct Circle_INFO{public double Row_center, Column_center, Radius, StartPhi, EndPhi;public string PointOrder;}/// <summary>/// 测量信息/// </summary>public struct Metrology_INFO{public double Length1, Length2, Threshold, MeasureDis;public HTuple ParamName, ParamValue;public int PointsOrder;}#endregion/// <圆形测量参数对象>/// /// </summary>/// <param name="_length1"></长/2>/// <param name="_length2"></宽/2>/// <param name="_threshold"></阈值>/// <param name="_measureDis"></间隔>/// <param name="_paraName"></参数名>/// <param name="_paraValue"></参数值>/// <param name="_pointsOrder"></点顺序>public void MetrologyINFOSet(double _length1, double _length2, double _threshold, double _measureDis, HTuple _paraName, HTuple _paraValue, int _pointsOrder){m_MetrologyInfo.Length1 = _length1; // 长/2m_MetrologyInfo.Length2 = _length2; // 宽/2m_MetrologyInfo.Threshold = _threshold; // 阈值m_MetrologyInfo.MeasureDis = _measureDis; //间隔m_MetrologyInfo.ParamName = _paraName; //参数名m_MetrologyInfo.ParamValue = _paraValue; //参数值m_MetrologyInfo.PointsOrder = _pointsOrder; //点顺序 0位默认,1 顺时针,2 逆时针}/// <圆形测量基准圆形参数对象参数设置>/// /// </summary>/// <中心Y坐标="m_Row_center"></中心Y坐标>/// <中心X坐标="m_Column_center"></中心X坐标>/// <半径="m_Radius"></半径>/// <param name="m_StartPhi"></开始角度>/// <param name="m_EndPhi"></结束角度>/// <param name="m_PointOrder"></点顺序>public void CircleINFOSet(double m_Row_center, double m_Column_center, double m_Radius, double m_StartPhi, double m_EndPhi, string m_PointOrder){m_CircleInfo.Row_center = m_Row_center;m_CircleInfo.Column_center = m_Column_center;m_CircleInfo.Radius = m_Radius;m_CircleInfo.StartPhi = m_StartPhi;m_CircleInfo.EndPhi = m_EndPhi;m_CircleInfo.PointOrder = m_PointOrder;//点顺序 0位默认,1 顺时针,2 逆时针}/// <summary>/// 检测圆/// </summary>/// <param name="inImage">输入图像</param>/// <param name="inCircle">输入圆</param>/// <param name="inMetrology">输入形态学</param>/// <param name="outCircle">输出圆</param>/// <param name="outR">输出行坐标</param>/// <param name="outC">输出列坐标</param>/// <param name="outMeasureXLD">输出检测轮廓</param>public void MeasureCircle(HImage inImage, Circle_INFO inCircle, Metrology_INFO inMetrology, out Circle_INFO outCircle, out HTuple outR, out HTuple outC, out HXLDCont outMeasureXLD){HMetrologyModel hMetrologyModel = new HMetrologyModel();try{outCircle = new Circle_INFO();HTuple CircleResult = new HTuple();HTuple CircleInfo = new HTuple();CircleInfo.Append(new HTuple(new double[] { inCircle.Row_center, inCircle.Column_center, inCircle.Radius }));hMetrologyModel.AddMetrologyObjectGeneric(new HTuple("circle"), CircleInfo, new HTuple(inMetrology.Length1),new HTuple(inMetrology.Length2), new HTuple(1), new HTuple(inMetrology.Threshold), inMetrology.ParamName, inMetrology.ParamValue);hMetrologyModel.ApplyMetrologyModel(inImage);outMeasureXLD = hMetrologyModel.GetMetrologyObjectMeasures("all", "all", out outR, out outC);CircleResult = hMetrologyModel.GetMetrologyObjectResult(new HTuple("all"), new HTuple("all"), new HTuple("result_type"), new HTuple("all_param"));if (CircleResult.TupleLength() >= 3){outCircle.Row_center = CircleResult[0].D;outCircle.Column_center = CircleResult[1].D;outCircle.Radius = CircleResult[2].D;}hMetrologyModel.Dispose();}catch (Exception ex){outCircle = new Circle_INFO();outR = new HTuple();outC = new HTuple();outMeasureXLD = new HXLDCont();hMetrologyModel.Dispose();//异常写入日志文件// MessageBox.Show(ex.Message);}}/// <summary>/// 根据位置变换圆/// </summary>/// <param name="homMat">变换关系</param>/// <param name="circle">圆</param>public static void AffineCircle(HHomMat2D homMat, ref Circle_INFO circle){double row, col, phi;homMat.AffineTransPixel(circle.Row_center, circle.Column_center, out row, out col);phi = ((HTuple)homMat[0]).TupleAcos().D;circle.Row_center = row;circle.Column_center = col;circle.StartPhi += phi;circle.EndPhi += phi;}}
接下来介绍使用方法
//new个CircleMeasure的对象出来
public CircleMeasure mCircleMeasure = new CircleMeasure();
//卡尺圆形测量数据初始化try
{mCircleMeasure.CircleINFOSet(0, 0, 50, 0, 2 * 3.14, "positive");mCircleMeasure.MetrologyINFOSet(20,20,30,5,"measure_distance",1,0);
}catch { }开始进行测量//定义各个需要的变量HTuple outR = null, outC = null;HTuple hv_Row = null, hv_Column = null, hv_Radius = null;HTuple hv_Row11 = null, hv_Column11 = null, hv_Row12 = null, hv_Column12 = null, hv_Angle1 = null, hv_Angle2 = null;HXLDCont m_MeasureXLD = new HXLDCont();//LeftUpCamImageDisplay是halcon显示图像窗口控件名称LeftUpCamImageDisplay.HalconWindow.SetColor("red");LeftUpCamImageDisplay.HalconWindow.SetDraw("margin");//[DrawCircleMod介绍](https://blog.csdn.net/cashmood/article/details/99756580)HOperatorSet.DrawCircleMod(LeftUpCamImageDisplay.HalconWindow,1296 ,972 ,140 ,out hv_Row, out hv_Column, out hv_Radius);//设置测量参数mCircleMeasure.CircleINFOSet(hv_Row, hv_Column, hv_Radius, hv_Angle1, hv_Angle2, "positive");mCircleMeasure.MetrologyINFOSet(hv_Radius / 5, hv_Radius / 50, 30, 5, new HTuple(("measure_distance")),(new HTuple(10)), 0);mCircleMeasure.MeasureCircle(LeftUpCamCurrentImg, mCircleMeasure.m_CircleInfo, mCircleMeasure.m_MetrologyInfo, out mCircleMeasure.m_OutCircleInfo, out outR, out outC, out m_MeasureXLD);//显示测量结果LeftUpCamImageDisplay.HalconWindow.DispObj(LeftUpCamCurrentImg);LeftUpCamImageDisplay.HalconWindow.SetColor("blue");LeftUpCamImageDisplay.HalconWindow.SetDraw("margin");m_MeasureXLD.DispObj(LeftUpCamImageDisplay.HalconWindow);LeftUpCamImageDisplay.HalconWindow.SetTposition(50, 10);LeftUpCamImageDisplay.HalconWindow.WriteString(" CenterRow:" + mCircleMeasure.m_OutCircleInfo.Row_center.ToString() + " CenterCol:" + mCircleMeasure.m_OutCircleInfo.Column_center.ToString() + " Radius::" + mCircleMeasure.m_OutCircleInfo.Radius.ToString());m_MeasureXLD.Dispose();