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;
            }
        }
相关推荐
一切皆有可能!!3 小时前
实践篇:利用ragas在自己RAG上实现LLM评估②
人工智能·语言模型
月白风清江有声5 小时前
爆炸仿真的学习日志
人工智能
华奥系科技6 小时前
智慧水务发展迅猛:从物联网架构到AIoT系统的跨越式升级
人工智能·物联网·智慧城市
R²AIN SUITE6 小时前
MCP协议重构AI Agent生态:万能插槽如何终结工具孤岛?
人工智能
b***25116 小时前
动力电池点焊机:驱动电池焊接高效与可靠的核心力量|比斯特自动化
人工智能·科技·自动化
Gyoku Mint6 小时前
机器学习×第二卷:概念下篇——她不再只是模仿,而是开始决定怎么靠近你
人工智能·python·算法·机器学习·pandas·ai编程·matplotlib
小和尚同志7 小时前
通俗易懂的 MCP 概念入门
人工智能·aigc
dudly7 小时前
大语言模型评测体系全解析(下篇):工具链、学术前沿与实战策略
人工智能·语言模型
zzlyx997 小时前
AI大数据模型如何与thingsboard物联网结合
人工智能·物联网
说私域7 小时前
定制开发开源AI智能名片驱动下的海报工厂S2B2C商城小程序运营策略——基于社群口碑传播与子市场细分的实证研究
人工智能·小程序·开源·零售