Halcon:HObject与opencv:Mat互转

ops/2025/3/10 19:55:59/

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.ppmy.cn/ops/164739.html

相关文章

分布式锁—4.Redisson的联锁和红锁一

大纲 1.Redisson联锁MultiLock概述 2.Redisson联锁MultiLock的加锁与释放锁 3.Redisson红锁RedLock的算法原理 4.Redisson红锁RedLock的源码分析 1.Redisson联锁MultiLock概述 (1)MultiLock的简介 (2)MultiLock的使用 (3)MultiLock的初始化 (1)MultiLock的简介 一.一次…

解析 SQL,就用 sqlparse!

文章目录 解析 SQL&#xff0c;就用 sqlparse&#xff01;一、背景&#xff1a;为什么你需要 sqlparse&#xff1f;二、什么是 sqlparse&#xff1f;三、如何安装 sqlparse&#xff1f;四、简单易用的库函数1\. parse(sql)2\. format(sql, **options)3\. split(sql)4\. get_typ…

Android笔记:android 6.0 TextView对state_selected失效

在 Android 6.0 系统中&#xff0c;TextView 的 state_selected 状态失效可能由多种原因导致&#xff0c;下面为你详细分析原因并提供相应的解决办法。 原因分析 系统兼容性问题 Android 6.0 系统在处理视图状态和样式方面可能存在一些兼容性问题&#xff0c;导致 state_selec…

基于Spring Boot的扶贫助农系统的设计与实现(LW+源码+讲解)

专注于大学生项目实战开发,讲解,毕业答疑辅导&#xff0c;欢迎高校老师/同行前辈交流合作✌。 技术范围&#xff1a;SpringBoot、Vue、SSM、HLMT、小程序、Jsp、PHP、Nodejs、Python、爬虫、数据可视化、安卓app、大数据、物联网、机器学习等设计与开发。 主要内容&#xff1a;…

【认知学习篇】【DeepSeek 深度解析:技术原理、算法模型与架构设计】

大家好,今天咱们来聊聊 DeepSeek,这个听起来有点高大上的技术。其实,DeepSeek 并不是什么神秘的黑科技,它本质上是一个基于深度学习的智能系统,主要用于处理复杂的任务,比如自然语言处理、计算机视觉等。接下来,我会从技术原理、算法模型和架构设计三个方面,带大家深入…

创新监管,保障生产安全

在现代工业生产中&#xff0c;电气焊作业是不可或缺的一环&#xff0c;但同时也伴随着一定的安全风险。为了提高焊接作业的安全性&#xff0c;迪格特电子科技有限公司开发了电气焊安全作业管理平台&#xff0c;该平台通过智能化监管系统&#xff0c;实现了对焊机联网的全面监管…

Ubuntu 22.04 LTS 入门教学文档

适用对象&#xff1a;Linux 新手、开发者、运维人员 目录 Ubuntu 简介系统安装指南图形界面操作终端基础命令软件包管理用户与权限管理网络配置与管理磁盘与文件系统系统维护与故障排查附录&#xff1a;常用快捷键与资源 1. Ubuntu 简介 1.1 什么是 Ubuntu&#xff1f; Ubun…

Linux下安装elasticsearch(Elasticsearch 7.17.23)

Elasticsearch 是一个分布式的搜索和分析引擎&#xff0c;能够以近乎实时的速度存储、搜索和分析大量数据。它被广泛应用于日志分析、全文搜索、应用程序监控等场景。 本文将带你一步步在 Linux 系统上安装 Elasticsearch 7.17.23 版本&#xff0c;并完成基本的配置&#xff0…