Hello Mat

 找回密码
 立即注册
查看: 6866|回复: 4

Halcon HObject转OpenCVSharp Mat

[复制链接]

84

主题

115

帖子

731

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
1467
发表于 2020-10-18 21:32:06 | 显示全部楼层 |阅读模式
Halcon HObject转OpenCVSharp Mat:

  1. public static Mat HImageToMat(HObject hobj)
  2. {
  3.         try
  4.         {
  5.                 Mat pImage;
  6.                 HTuple htChannels;
  7.                 HTuple cType = null;
  8.                 HTuple width, height;
  9.                 width = height = 0;

  10.                 htChannels = null;
  11.                 HOperatorSet.CountChannels(hobj, out htChannels);

  12.                 if (htChannels.Length == 0)
  13.                 {
  14.                         return null;
  15.                 }
  16.                 if (htChannels[0].I == 1)
  17.                 {
  18.                         HTuple ptr;
  19.                         HOperatorSet.GetImagePointer1(hobj, out ptr, out cType, out width, out height);
  20.                         pImage = new Mat(new OpenCvSharp.Size(width, height), MatType.CV_8UC1, new Scalar(0));
  21.                         int Width = width;
  22.                        
  23.                         unsafe
  24.                         {
  25.                                 for (int i = 0; i < height; i++)
  26.                                 {
  27.                                         //long step = pImage.Step();
  28.                                         IntPtr start = IntPtr.Add(pImage.Data, i * width);
  29.                                         CopyMemory(start, new IntPtr((byte*)ptr.IP + width * i), (uint)width);
  30.                                 }
  31.                         }
  32.                        
  33.                         return pImage;
  34.                 }
  35.                 else if (htChannels[0].I == 3)
  36.                 {
  37.                         HTuple ptrRed;
  38.                         HTuple ptrGreen;
  39.                         HTuple ptrBlue;

  40.                         HOperatorSet.GetImagePointer3(hobj, out ptrRed, out ptrGreen, out ptrBlue, out cType, out width, out height);
  41.                         Mat pImageRed = new Mat(new OpenCvSharp.Size(width, height), MatType.CV_8UC1);
  42.                         Mat pImageGreen = new Mat(new OpenCvSharp.Size(width, height), MatType.CV_8UC1);
  43.                         Mat pImageBlue = new Mat(new OpenCvSharp.Size(width, height), MatType.CV_8UC1);
  44.                         pImage = new Mat(new OpenCvSharp.Size(width, height), MatType.CV_8UC3, new Scalar(0,0,0));
  45.                         unsafe
  46.                         {
  47.                                 for (int i = 0; i < height; i++)
  48.                                 {
  49.                                         long step = pImage.Step();
  50.                                         IntPtr startRed = IntPtr.Add(pImageRed.Data, i * width);
  51.                                         IntPtr startGreen = IntPtr.Add(pImageGreen.Data, i * width);
  52.                                         IntPtr startBlue = IntPtr.Add(pImageBlue.Data, i * width);
  53.                                         CopyMemory(startRed, new IntPtr((byte*)ptrRed.IP + width * i), (uint)width);
  54.                                         CopyMemory(startGreen, new IntPtr((byte*)ptrGreen.IP + width * i), (uint)width);
  55.                                         CopyMemory(startBlue, new IntPtr((byte*)ptrBlue.IP + width * i), (uint)width);
  56.                                 }
  57.                         }
  58.                         Mat[] multi = new Mat[] { pImageBlue, pImageGreen, pImageRed };
  59.                         Cv2.Merge(multi, pImage);
  60.                         pImageRed.Dispose();
  61.                         pImageGreen.Dispose();
  62.                         pImageBlue.Dispose();
  63.                         return pImage;
  64.                 }
  65.                 else
  66.                 {
  67.                         return null;
  68.                 }
  69.         }
  70.         catch (Exception ex)
  71.         {
  72.                 throw ex;
  73.         }

  74. }
复制代码
其中会用到CopyMemory这个函数,需要在方法前声明:
  1. [DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
  2. public static extern void CopyMemory(IntPtr dest, IntPtr src, uint count);

  3. [DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
  4. public static extern void CopyMemory(int dest, int src, int count);
复制代码


参考:
【1】Halcon学习——HObject转OpenCVSharp Mat




回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-4-20 13:00 , Processed in 0.287079 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表