C# :HImage转Mat方法

原来网上查过HImage转Mat的方式,本来想直接copy,奈何查到的总有不完美的地方,且最近还从之前的代码发现了bug后改掉了。

这段代码测试后可用,直接分享出来。

我的halcon版本是 17.12 后面新版本是否使用可以同步留言。

OpenCV是用的C# nugget包下拉的,这个根据个人.net Framework 版本做调整就好

csharp 复制代码
 public Mat HImageToMat(HImage hImage)
 {
     try
     {
         Mat mImage;
         HTuple htChannels;
         HOperatorSet.CountChannels(hImage, out htChannels);

         if (htChannels.Length == 0 || (htChannels[0].I != 1 && htChannels[0].I != 3))
             return null;

         HTuple width, height;
         hImage.GetImageSize(out width, out height);

         // 处理单通道图像
         if (htChannels[0].I == 1)
         {
             HTuple ptr, type;
             HOperatorSet.GetImagePointer1(hImage, out ptr, out type, out _, out _);

             MatType cvType = GetCvType(type);
             mImage = new Mat(height, width, cvType);

             unsafe
             {
                 byte* srcPtr = (byte*)ptr.IP;
                 int step = (int)mImage.Step();
                 for (int row = 0; row < height; row++)
                 {
                     Buffer.MemoryCopy(
                         srcPtr + row * width,
                         mImage.DataPointer + row * step,
                         step,
                         width
                     );
                 }
             }
             return mImage;
         }
         // 处理三通道图像
         else
         {
             HTuple ptrR, ptrG, ptrB, type;
             HOperatorSet.GetImagePointer3(hImage, out ptrR, out ptrG, out ptrB, out type, out _, out _);

             MatType cvType = GetCvType(type);
             Mat[] channels = new Mat[3]
             {
                 new Mat(height, width, cvType), // B
                 new Mat(height, width, cvType), // G
                 new Mat(height, width, cvType)  // R
             };

             unsafe
             {
                 // 复制数据到各通道 (Halcon: R-G-B → OpenCV: B-G-R)
                 CopyChannel(ptrB, channels[0], width, height); // B
                 CopyChannel(ptrG, channels[1], width, height); // G
                 CopyChannel(ptrR, channels[2], width, height); // R
             }

             mImage = new Mat();
             Cv2.Merge(channels, mImage);

             foreach (var channel in channels)
                 channel.Dispose();

             return mImage;
         }
     }
     catch (Exception ex)
     {
         throw new Exception("HImage转Mat失败", ex);
     }
 }

That 's all . Thank you.

相关推荐
小羊没烦恼!3 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
神仙别闹3 天前
基于C#+MySQL实现(WinForm)个人聊天室软件
c#
伞伞悦读3 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
C语言小火车3 天前
C/C++ 为什么需要编译器?
开发语言·c++
霍霍的袁3 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
kybs19913 天前
全球灾害数据分析可视化 毕业设计-附源码66794
vue.js·spring boot·mysql·安全·django·c#·asp.net
孙启超3 天前
【AI开发之Rust】第 11 课:智能指针与内部可变性
开发语言·后端·rust
此生决int3 天前
深入理解C++系列(20)——C++11(下)
开发语言·c++
CoderYanger3 天前
A.每日一题:835. 图像重叠
java·开发语言·程序人生·leetcode·面试·职场和发展·学习方法
伞伞悦读3 天前
【第37期】Python JSON 与配置详解:序列化、反序列化、嵌套结构和配置文件
开发语言·python·json