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.

相关推荐
Ray Liang1 天前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
Scout-leaf4 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
用户298698530144 天前
程序员效率工具:Spire.Doc如何助你一键搞定Word表格排版
后端·c#·.net
mudtools5 天前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的6 天前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
唐宋元明清21886 天前
.NET 本地Db数据库-技术方案选型
windows·c#
郑州光合科技余经理6 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
lindexi6 天前
dotnet DirectX 通过可等待交换链降低输入渲染延迟
c#·directx·d2d·direct2d·vortice
feifeigo1236 天前
matlab画图工具
开发语言·matlab
dustcell.6 天前
haproxy七层代理
java·开发语言·前端