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;
            }
        }
相关推荐
whoarethenext17 分钟前
使用 C++/OpenCV 和 MFCC 构建双重认证智能门禁系统
开发语言·c++·opencv·mfcc
jndingxin18 分钟前
OpenCV CUDA模块设备层-----高效地计算两个 uint 类型值的带权重平均值
人工智能·opencv·计算机视觉
Sweet锦29 分钟前
零基础保姆级本地化部署文心大模型4.5开源系列
人工智能·语言模型·文心一言
hie988941 小时前
MATLAB锂离子电池伪二维(P2D)模型实现
人工智能·算法·matlab
晨同学03271 小时前
opencv的颜色通道问题 & rgb & bgr
人工智能·opencv·计算机视觉
蓝婷儿2 小时前
Python 机器学习核心入门与实战进阶 Day 3 - 决策树 & 随机森林模型实战
人工智能·python·机器学习
大千AI助手2 小时前
PageRank:互联网的马尔可夫链平衡态
人工智能·机器学习·贝叶斯·mc·pagerank·条件概率·马尔科夫链
小和尚同志2 小时前
Cline | Cline + Grok3 免费 AI 编程新体验
人工智能·aigc
我就是全世界2 小时前
TensorRT-LLM:大模型推理加速的核心技术与实践优势
人工智能·机器学习·性能优化·大模型·tensorrt-llm
.30-06Springfield2 小时前
决策树(Decision tree)算法详解(ID3、C4.5、CART)
人工智能·python·算法·决策树·机器学习