设置首页收藏本站
开启左侧

Halcon 和 C# 联合编程 - 图像变量的相互转换(HObject、HImage、B...

[复制链接]
zwjob 发表于 2019-1-17 11:23:25 | 显示全部楼层 |阅读模式
下面代码是我在搜索 get_image_pointer1 过程中得到,给我带来了一些灵感。
  1. /// <summary>
  2. /// 灰度图像变量 HObject -> Bitmap
  3. /// 原文:[url=https://blog.csdn.net/bingbingxie1/article/details/83344704]https://blog.csdn.net/bingbingxie1/article/details/83344704[/url]
  4. /// </summary>
  5. public static Bitmap HObject2Bitmap(HObject ho)
  6. {
  7.     try
  8.     {
  9.         HTuple type, width, height, pointer;
  10.         //HOperatorSet.AccessChannel(ho, out ho, 1);
  11.         HOperatorSet.GetImagePointer1(ho, out pointer, out type, out width, out height);
  12.         //himg.GetImagePointer1(out type, out width, out height);

  13.         Bitmap bmp = new Bitmap(width.I, height.I, PixelFormat.Format8bppIndexed);
  14.         ColorPalette pal = bmp.Palette;
  15.         for (int i = 0; i <= 255; i++)
  16.         {
  17.             pal.Entries[i] = Color.FromArgb(255, i, i, i);
  18.         }
  19.         bmp.Palette = pal;
  20.         BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
  21.         int PixelSize = Bitmap.GetPixelFormatSize(bitmapData.PixelFormat) / 8;
  22.         int stride = bitmapData.Stride;
  23.         int ptr = bitmapData.Scan0.ToInt32();
  24.         for (int i = 0; i < height; i++)
  25.         {
  26.             CopyMemory(ptr, pointer, width * PixelSize);
  27.             pointer += width;
  28.             ptr += bitmapData.Stride;
  29.         }

  30.         bmp.UnlockBits(bitmapData);
  31.         return bmp;
  32.     }
  33.     catch (Exception exc)
  34.     {
  35.         return null;
  36.     }
  37. }
  38. /// <summary>
  39. /// 灰度图像变量 HObject -> HImage
  40. /// 原文:[url=https://blog.csdn.net/pudongdong/article/details/53574742]https://blog.csdn.net/pudongdong/article/details/53574742[/url]
  41. /// </summary>
  42. public HImage HObject2HImage(Hobject hobject)
  43. {
  44.     HImage image;
  45.     HTuple type, width, height, pointer;
  46.     gHOperatorSet.GetImagePointer1(ho, out pointer, out type, out width, out height);
  47.     image.GenImage1(type, width, height, pointer);
  48.     return image;
  49. }
复制代码
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
Criss 发表于 2019-1-17 11:37:14 | 显示全部楼层
代码初步看了下,应该没有问题大问题,核心就是图片的数据指针,然后对应大小转换即可!
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
he98168 发表于 2019-10-23 17:05:13 | 显示全部楼层
CopyMemory这个是什么方法,在哪里定义的 ,这个PixelFormat.Format8bppIndexed像素格式又是那个的格式,可以说下吗
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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