Halcon:HObject与opencv:Mat互转

Halcon:HObject与opencv:Mat互转

  • [1. Mat转HObject](#1. Mat转HObject)
  • [2. HObject转Mat](#2. HObject转Mat)

1. Mat转HObject

csharp 复制代码
        void MatToHObject(Mat mat, out HObject hObj)
        {
            int width = mat.Width;
            int height = mat.Height;
            HTuple type, pointer, widthTuple, heightTuple;

            if (mat.Channels() == 1)
            {
                // 单通道图像(灰度图)
                type = "byte";
                pointer = new HTuple(mat.Data);
                widthTuple = width;
                heightTuple = height;
                HOperatorSet.GenImage1(out hObj, type, width, height, pointer);
            }
            else if (mat.Channels() == 3)
            {
                // 三通道图像(彩色图)
                Mat rgb = new Mat();
                Cv2.CvtColor(mat, rgb, ColorConversionCodes.BGR2RGB); // 转换为 RGB 格式
                HTuple redPointer = new HTuple(rgb.Data);
                HTuple greenPointer = new HTuple(rgb.Data + 1);
                HTuple bluePointer = new HTuple(rgb.Data + 2);
                widthTuple = width;
                heightTuple = height;
                HOperatorSet.GenImageInterleaved(out hObj, redPointer, greenPointer, bluePointer, "rgb", width, height, 8, "byte", 0, 0, -1, 0);
            }
            else
            {
                hObj = new HObject();
            }
        }

2. HObject转Mat

csharp 复制代码
        [DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
        public static extern void CopyMemory(IntPtr dest, IntPtr src, uint count);
        [DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
        public static extern void CopyMemory(int dest, int src, int count);

        public static Mat HObjectToMat(HObject hobj)
        {
            try
            {
                Mat pImage;
                HTuple htChannels;
                HTuple cType = null;
                HTuple width, height;
                width = height = 0;

                htChannels = null;
                HOperatorSet.CountChannels(hobj, out htChannels);

                if (htChannels.Length == 0)
                {
                    return null;
                }
                if (htChannels[0].I == 1)
                {
                    HTuple ptr;
                    HOperatorSet.GetImagePointer1(hobj, out ptr, out cType, out width, out height);
                    pImage = new Mat(new OpenCvSharp.Size(width, height), MatType.CV_8UC1, new Scalar(0));
                    int Width = width;

                    unsafe
                    {
                        for (int i = 0; i < height; i++)
                        {
                            //long step = pImage.Step();
                            IntPtr start = IntPtr.Add(pImage.Data, i * width);
                            CopyMemory(start, new IntPtr((byte*)ptr.IP + width * i), (uint)width);
                        }
                    }

                    return pImage;
                }
                else if (htChannels[0].I == 3)
                {
                    HTuple ptrRed;
                    HTuple ptrGreen;
                    HTuple ptrBlue;

                    HOperatorSet.GetImagePointer3(hobj, out ptrRed, out ptrGreen, out ptrBlue, out cType, out width, out height);
                    Mat pImageRed = new Mat(new OpenCvSharp.Size(width, height), MatType.CV_8UC1);
                    Mat pImageGreen = new Mat(new OpenCvSharp.Size(width, height), MatType.CV_8UC1);
                    Mat pImageBlue = new Mat(new OpenCvSharp.Size(width, height), MatType.CV_8UC1);
                    pImage = new Mat(new OpenCvSharp.Size(width, height), MatType.CV_8UC3, new Scalar(0, 0, 0));
                    unsafe
                    {
                        for (int i = 0; i < height; i++)
                        {
                            long step = pImage.Step();
                            IntPtr startRed = IntPtr.Add(pImageRed.Data, i * width);
                            IntPtr startGreen = IntPtr.Add(pImageGreen.Data, i * width);
                            IntPtr startBlue = IntPtr.Add(pImageBlue.Data, i * width);
                            CopyMemory(startRed, new IntPtr((byte*)ptrRed.IP + width * i), (uint)width);
                            CopyMemory(startGreen, new IntPtr((byte*)ptrGreen.IP + width * i), (uint)width);
                            CopyMemory(startBlue, new IntPtr((byte*)ptrBlue.IP + width * i), (uint)width);
                        }
                    }
                    Mat[] multi = new Mat[] { pImageBlue, pImageGreen, pImageRed };
                    Cv2.Merge(multi, pImage);
                    pImageRed.Dispose();
                    pImageGreen.Dispose();
                    pImageBlue.Dispose();
                    return pImage;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
相关推荐
张琪杭几秒前
pytorch tensor创建tensor
人工智能·pytorch·python
CodeAaron3 分钟前
智慧城市新基建:AI代理IP如何让城市管理“耳聪目明”?
人工智能·tcp/ip·智慧城市
山西茄子6 分钟前
DeepStream推理dewarped所有surfaces
人工智能·深度学习·计算机视觉·deepstream
天空卫士19 分钟前
AI巨浪中的安全之舵:天空卫士助力人工智能落地远航
大数据·人工智能·安全·网络安全·数据安全
隔窗听雨眠20 分钟前
DeepSeek 与 ChatGPT的主要区别
人工智能·chatgpt·deepseek
十年一梦实验室26 分钟前
波士顿动力ATLAS 3.0展示6项新AI升级(SPACEO机器人)
人工智能·机器人
hjehheje34 分钟前
clickhouse安装路径
人工智能
hjehheje35 分钟前
clickhouse ppt
人工智能
byxdaz1 小时前
CUDA编程之OpenCV与CUDA结合使用
人工智能·opencv·计算机视觉
byxdaz1 小时前
NVIDIA显卡驱动、CUDA、cuDNN 和 TensorRT 版本匹配指南
linux·人工智能·深度学习