C# Opencv Mat类型转Halcon 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);
               }
           }
       }

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

你可能感兴趣的:(c#,算法,开发语言,opencv,halcon)