Halcon常用算子搭配检测实例

Halcon常用算子搭配检测实例

文章目录

  • C# 图像类型转换之Mat类型转HObject类型
    • Opencv Mat类型转Halcon HObject类型:

C# 图像类型转换之Mat类型转HObject类型

Opencv Mat类型转Halcon HObject类型:

  主要用于Mat类型转HObject类型,不会产生图像形变

 public void MatToHObject(Mat imgMat, out HObject imgHOject)
        {
            int ImageWidth = imgMat.Width;
            int ImageHeight = imgMat.Height;
            int channel = imgMat.Channels();
            long size = ImageWidth * ImageHeight * channel;
            int col_byte_num = ImageWidth * channel;

            byte[] rgbValues = new byte[size];
            unsafe
            {
                for (int i = 0; i < ImageHeight; i++)
                {
                    IntPtr c = imgMat.Ptr(i);
                    // 一行一行将mat 像素复制到byte[]
                    Marshal.Copy(c, rgbValues, i * col_byte_num, col_byte_num);
                }
                void* p;
                IntPtr ptr;
                fixed (byte* pc = rgbValues)
                {
                    p = (void*)pc;
                    ptr = new IntPtr(p);
                }

                if (channel == 1)
                {
                    HOperatorSet.GenImage1(out imgHOject, "byte", ImageWidth, ImageHeight, ptr);
                }
                else
                {
                    HOperatorSet.GenImageInterleaved(out imgHOject, ptr, "bgr", ImageWidth, ImageHeight, 0, "byte", 0, 0, 0, 0, -1, 0);
                }
            }
        }

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