C#圆形卡尺测量程序基于halcon

废话不多说上源码

觉得帖子有用给点个赞哈C#圆形卡尺测量程序基于halcon_第1张图片

先来个效果图

下边的是源码,自己新建一个文件粘贴进去,包含到您现在的项目 中。这串源码后边是使用方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HalconDotNet;
        public class CircleMeasure
        {
            /// 
            /// 检测形态信息
            /// 
            public Metrology_INFO m_MetrologyInfo;//圆形测量参数对象
            public Circle_INFO m_CircleInfo;//圆形测量基准圆形参数对象
            public Circle_INFO m_OutCircleInfo;//圆形测量完成参数对象
    #region 卡尺圆形测量参数信息
            /// 
            /// 圆信息
            /// 
            public struct Circle_INFO
            {
                public double Row_center, Column_center, Radius, StartPhi, EndPhi;
                public string PointOrder;
            }
            /// 
            /// 测量信息
            /// 
            public struct Metrology_INFO
            {
                public double Length1, Length2, Threshold, MeasureDis;
                public HTuple ParamName, ParamValue;
                public int PointsOrder;
            }
     #endregion
            /// <圆形测量参数对象>
            /// 
            /// 
            /// 
            /// 
            /// 
            /// 
            /// 
            /// 
            /// 
            public void MetrologyINFOSet(double _length1, double _length2, double _threshold, double _measureDis, HTuple _paraName, HTuple _paraValue, int _pointsOrder)
            {
                m_MetrologyInfo.Length1 = _length1;                        // 长/2
                m_MetrologyInfo.Length2 = _length2;                        // 宽/2
                m_MetrologyInfo.Threshold = _threshold;                    // 阈值
                m_MetrologyInfo.MeasureDis = _measureDis;                  //间隔
                m_MetrologyInfo.ParamName = _paraName;                     //参数名
                m_MetrologyInfo.ParamValue = _paraValue;                   //参数值
                m_MetrologyInfo.PointsOrder = _pointsOrder;                //点顺序 0位默认,1 顺时针,2 逆时针
            }
            /// <圆形测量基准圆形参数对象参数设置>
            /// 
            /// 
            /// <中心Y坐标="m_Row_center">
            /// <中心X坐标="m_Column_center">
            /// <半径="m_Radius">
            /// 
            /// 
            /// 
            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 逆时针
                }
            
    
            /// 
            /// 检测圆
            /// 
            /// 输入图像
            /// 输入圆
            /// 输入形态学
            /// 输出圆
            /// 输出行坐标
            /// 输出列坐标
            /// 输出检测轮廓
            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);
                }
            }
            /// 
            /// 根据位置变换圆
            /// 
            /// 变换关系
            /// 圆
            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();

你可能感兴趣的:(#,Halcon图像处理实例)