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.

相关推荐
hez20104 天前
在 .NET 上构建超大托管数组
c#·.net·.net core·gc·clr
雨落倾城夏未凉9 天前
第四章c#方法-参数数组和可选参数(16)
后端·c#
唐青枫10 天前
线程不是越多越快:C#.NET Thread 生命周期、同步与后台工作线程实战
c#·.net
唐青枫11 天前
别只会反射:C#.NET Emit 动态生成代码实战详解
c#·.net
咕白m62511 天前
.NET 环境下 Word 超链接批量提取方案
c#·.net
用户917215619021111 天前
C# 通信协议增量解析:用状态机处理半包和粘包
c#
小码编匠12 天前
C# 工控上位机必备:数据转换工具类与十个核心模块
后端·c#·.net
唐青枫14 天前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
Artech14 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf16 天前
C#摸鱼实录——IoC与DI案例详解
c#