C#调用Halcon程序三种方法

1.halcon导出C#文件,在C#里面调用
2.在C#里面使用Halcon引擎调用Halcon程序,直接运行主程序
3.在C#里面使用Halcon引擎调用.hdev下本地函数或者.hdvp外部函数

Halcon引擎手册:C:/Program Files/MVTec/HALCON-21.11-Progress/doc_en_US/html/manuals/programmers_guide/programmers_guide_0120.html
C#调用Halcon程序三种方法_第1张图片
1.导出C#(代码我只给出类,需要调用)

using VIA.ImageProcessing;
using HalconDotNet;
using System.Runtime.InteropServices;
using Halcon_接口;

class Test
{
    /// 
    /// C#调用接口
    /// 
    /// 
    public bool Measure(Imagecustom Image_Light, Imagecustom Image_Gray, string LeftOrRight,out double[] Result_Distance,out double[] Result_Height,out HTuple Display_Handle)
    {
        HObject Display = new HObject();
        HTuple result_Distance=new HTuple();
        HTuple result_Height=new HTuple();
        try
        {
            int Len = Image_Light.Length;
            int Width = Image_Light.Width;
            ToHobject3D(Image_Gray.Data, Width, Len, out HObject Image_Gray_A);
            ToHobject3D(Image_Light.Data, Width, Len, out HObject Image_Light_A);
            HalconAlgo Measure = new HalconAlgo();
            
            Measure.Measure_Run(Image_Light_A, Image_Gray_A, out Display, LeftOrRight, out result_Height, out result_Distance, out Display_Handle);

            double[] dis = new double[4];
            double[] hei = new double[4];
            for (int i=0;i<4;i++)
            {
                hei[i] = result_Height[i].D;
                dis[i] = result_Distance[i].D;

            }
            Result_Distance = dis;
            Result_Height = hei;

            return true;

        }
        catch (Exception ex)
        {
            Result_Distance = new double[4] {0, 0, 0, 0 };
            Result_Height = new double[4] {0, 0, 0, 0 };
            Display_Handle = null;
            return false;
        }
    }
		
		//图像数据转object
    public void ToHobject3D(ushort[] RawValue, int Width, int Height, out HObject Image)
    {
        IntPtr Iptr = Marshal.AllocHGlobal(Width * Height * sizeof(int));
        int[] Data = Array.ConvertAll<ushort, int>(RawValue, Convert.ToInt32);
        try
        {
            Marshal.Copy(Data, 0, Iptr, Width * Height);
            HOperatorSet.GenImage1(out Image, "int4", Width, Height, (HTuple)Iptr);
            //最好回到Halcon里将int4转成uint2 convert_image_type()
        }
        catch (Exception e)
        {
            Image = null;
        }
        finally
        {
            Marshal.FreeHGlobal(Iptr);
        }
    }
}
struct Imagecustom 
{
    ushort[] Data;
    int Width;
    int Height;
}

C#调用Halcon程序三种方法_第2张图片
2.在C#里面使用Halcon引擎调用Halcon程序

class Test
{
    public double[] Test_1()
    {
         // 程序路径
        string ProgramPathString = @"TEST.hdev";
        //打开主程序
        HDevProgram MyProgram = new HDevProgram(ProgramPathString);
        //主程序用HDevProgramCall 要是执行程序内本地函数或者外部函数使用HDevProcedure
        HDevProgramCall ProgramCall = new HDevProgramCall(MyProgram);
        //执行程序
        ProgramCall.Execute();

        double[] SaveData;
        SaveData = ProgramCall.GetCtrlVarTuple("Height_Left");//获取程序中的某个参数,变量名

        return SaveData;
    }
}

3.在C#里面使用Halcon引擎调用.hdev下本地函数或者.hdvp外部函数,不会运行主函数,相当于调用hdev里的本地函数或者是外部函数,需要输入控制变量

class Test
    {
    public double[] Test_1()
    {
        // 调用程序的引擎,***看起来没用到***
        HDevEngine MyEngine = new HDevEngine();
        // 程序路径
        string ProgramPathString = @"TEST.hdev";
        //图像路径
        string Image_Path_Light = "E:\\1.tif";
        string Image_Path_Gray = "E:\\2.tif";
        HObject Image_Light, Image_Gray;
        HOperatorSet.ReadImage(out Image_Light, Image_Path_Light);
        HOperatorSet.ReadImage(out Image_Gray, Image_Path_Gray);
        //程序
        HDevProgram Program = new HDevProgram(ProgramPathString);
        //主函数下图像处理函数,Measure本地函数名称
        HDevProcedure ProcessImageProc = new HDevProcedure(Program, "Measure");
        //回调主函数下图像处理函数
        HDevProcedureCall ProcessImageProcCall = new HDevProcedureCall(ProcessImageProc);

        //设置参数,设置Iconic 图形参数,Ctrl 控制参数
        ProcessImageProcCall.SetInputIconicParamObject("Image_Light", Image_Light);
        ProcessImageProcCall.SetInputIconicParamObject("Image_Gray", Image_Gray);
        ProcessImageProcCall.SetInputCtrlParamTuple("Left_Or_Right", "Left");
        //执行
        ProcessImageProcCall.Execute();

        //Data输出值本来是HTuple
        double[] Data;
        Data = ProcessImageProcCall.GetOutputCtrlParamTuple("Height");
        return Data;
    }
}

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