本文主要是介绍halcon,快速hobject转bitmap格式(20ms以下),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言
在实际项目中经常会将halcon的图像格式转化为bitmap的格式。例如在载入tensorflow的训练模型时,一般不支持对hobject格式的输入,包括很多程序。
所以就需要一个具有较快速度的转化方式。
本文中的转化方式,在24位彩色图中平均在15ms(4096*2160),32位彩色图18ms,8位灰度图,10ms。
1.C#程序
/// <summary>/// hobject彩色图24位转bitmap/// </summary>/// <param name="ho_image"></param>/// <param name="res24"></param>public void HobjectToBitmap24(HObject ho_image, out Bitmap res24){HTuple type, width, height;//创建交错格式图像HOperatorSet.InterleaveChannels(ho_image, out HObject InterImage, "rgb", "match", 255); //获取交错格式图像指针HOperatorSet.GetImagePointer1(InterImage, out HTuple Pointer, out type, out width, out height);IntPtr ptr = Pointer;res24 = new Bitmap(width / 3, height, width, PixelFormat.Format24bppRgb, ptr); }/// <summary>/// hobject彩色图32位转bitmap/// </summary>/// <param name="ho_image"></param>/// <param name="res32"></param>public void HobjectToBitmap32(HObject ho_image, out Bitmap res32){HTuple type, width, height;//创建交错格式图像HOperatorSet.InterleaveChannels(ho_image, out HObject InterImage, "argb", "match", 255);//获取交错格式图像指针HOperatorSet.GetImagePointer1(InterImage, out HTuple Pointer, out type, out width, out height);IntPtr ptr = Pointer;res32 = new Bitmap(width / 4, height, width, PixelFormat.Format32bppRgb, ptr);}/// <summary>/// hobject灰度8位转bitmap/// </summary>/// <param name="ho_image"></param>/// <param name="res8"></param>public void HobjectToBitmap8(HObject ho_image, out Bitmap res8){HTuple type, width, height;HOperatorSet.GetImagePointer1(ho_image, out HTuple Pointer, out type, out width, out height);IntPtr ptr = Pointer;res8 = new Bitmap(width, height, width, PixelFormat.Format8bppIndexed, ptr);//设置灰度调色板ColorPalette cp = res8.Palette;for (int i = 0; i < 256; i++){cp.Entries[i] = Color.FromArgb(i, i, i);}res8.Palette = cp;}
这篇关于halcon,快速hobject转bitmap格式(20ms以下)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!