Onnx部署深度神经网络时,入口参数为System.Drawing.Image类
项目中采用halcon库来从相机采集图片
所以中间需要实现HImage和Image的转换
代码如下
public Bitmap HimageToImage(HImage hImage)
{
HTuple hred, hgreen, hblue, type, width, height;
if (int.Parse(hImage.CountChannels().ToString()) == 1)
{
hImage = hImage.Compose3(hImage, hImage);
}
try
{
HOperatorSet.GetImagePointer3(hImage, out hred, out hgreen, out hblue, out type, out width, out height);
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppRgb);
Rectangle rect = new Rectangle(0, 0, width, height);
BitmapData bitmapData = bitmap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
int imglength = width * height;
unsafe
{
byte* bptr = (byte*)bitmapData.Scan0;
byte* r = ((byte*)hred.L);
byte* g = ((byte*)hgreen.L);
byte* b = ((byte*)hblue.L);
for (int i = 0; i < imglength; i++)
{
bptr[i * 4] = r[i];
bptr[i * 4 + 1] = g[i];
bptr[i * 4 + 2] = b[i];
}
}
bitmap.UnlockBits(bitmapData);
return bitmap;
}
catch (Exception)
{
throw;
}
}
经过测试可以实现转换效果
单通道图片也可转换