当前位置: 首页 > article >正文

Halcon:HObject与opencv:Mat互转

Halcon:HObject与opencv:Mat互转

  • 1. Mat转HObject
  • 2. HObject转Mat

1. Mat转HObject

        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


        [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;
            }
        }

http://www.kler.cn/a/574532.html

相关文章:

  • 【目标检测】【CVPR 2025】DEIM:具有改进匹配机制的DETR以实现快速收敛
  • Scala:case class(通俗易懂版)
  • DeepSeek大模型深度解析:架构、技术与应用全景
  • Pandas实现Excel的vlookup并且在指定列后面输出
  • Hadoop项目中的问题(1)——NetworkManager 和 network 服务冲突
  • AutoGen学习笔记系列(七)Tutorial - Managing State
  • 亚信安全发布2024威胁年报和2025威胁预测
  • Ubuntu20.04本地配置IsaacLab 4.2.0的G1训练环境(二):训练与推理
  • 兼容移动端ios,安卓,web端底部软键盘弹出,输入框被遮挡问题
  • C++————类和对象(一)
  • CentOS7安装 FFmpeg
  • 2024最新版python+pycharm安装与配置(mac和window都有讲)
  • 传统架构与集群架构搭建LAMP环境并部署WordPress服务
  • 【LeetCode 热题 100】3. 无重复字符的最长子串 | python 【中等】
  • Potplayer 怎么用鼠标左键单击播放暂停
  • GitHub教程
  • 深入理解CAS与乐观锁:Java高并发编程实战指南
  • 视频输入设备-V4L2的开发流程简述
  • css梯形tab
  • SpringMVC请求映射:@RequestMapping的高级用法