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

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

相关文章:

  • Linux下安装elasticsearch(Elasticsearch 7.17.23)
  • 出现FullGC的排查思路
  • MATLAB程序介绍,三维环境下的IMM(交互式多模型),使用CV和CT模型,EKF作为滤波
  • 如何在web页面下做自动化测试?
  • OCR识别技术在集装箱号码识别中的应用
  • LLM run
  • 利用Postman和Apipost进行API测试的实践与优化-动态参数
  • SpringMvc的设计模式
  • Blazor-全局路由跳转事件
  • 污酸提铼系统提升改造工艺
  • k8s面试题总结(十)
  • TDengine SQL查询语法
  • 基于Spring Boot + Vue的图书个性化推荐系统(LW+PPT)
  • flask实现mvc模式
  • JVM内存结构和各种结构的作用
  • 蓝桥备赛(11)- 数据结构、算法与STL
  • VMware虚拟机中CentOS8系统账户,忘记密码怎么找回
  • [内网安全] Windows 域认证 — Kerberos 协议认证
  • Objective-C 中 @synthesize VS @dynamic
  • 每日一题——缺失的第一个正数
  • 贸易公司网站设计案例/网站关键字优化公司
  • 衢州市哪里都网站建设公司比较好/推广赚佣金
  • 动物自己做的网站/网站底部友情链接代码
  • wordpress可视化界面/seo公司软件
  • 大连地区网站建设/培训课程总结
  • 网站后台页面是什么/百度竞价代运营