一直用opencv 做图像处理,最近接触到了halcon,发现使用halcon 实在太方便了。halcon 的代码可直接导出为C# 代码。由于我只是用halcon 实现图像算法功能,图像的显示还是用bitmap 格式,所以不可避免的要实现 bimtap 和hobject 互相转化的功能。
百度搜集的转换资料并不完整,经过几天的仔细研究,终于全部搞通。现分享出来,供有需要的同学参考。
1.彩色bitmap 转 彩色 Hobject
public void Bitmap2HObjectBpp24(Bitmap bmp, out HObject image)
{
try
{
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
HOperatorSet.GenImageInterleaved(out image, srcBmpData.Scan0, "bgr", bmp.Width, bmp.Height, 0, "byte", 0, 0, 0, 0, -1, 0);
bmp.UnlockBits(srcBmpData);
}
catch (Exception ex)
{
image = null;
}
}
2. 8位灰度bitmap 转 Hobject
public void Bitmap2HObjectBpp8(Bitmap bmp, out HObject image)
{
try
{
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
HOperatorSet.GenImage1(out image, "byte", bmp.Width, bmp.Height, srcBmpData.Scan0);
bmp.UnlockBits(srcBmpData);
}
catch (Exception ex)
{
image = null;
}
}
3. 彩色 Hobject 转 bitmap
老版halcon (10版) 采用此方法。
转出的bitmap 是32位,若需要24位rgb格式,需要通过 Graphics 类把32位图重画为24位。5
public void HObject2Bpp24_(HObject image, out Bitmap res24)
{
HTuple hred, hgreen, hblue, type, width, height;
HOperatorSet.GetImagePointer3(image, out hred, out hgreen, out hblue, out type, out width, out height);
Bitmap res32 = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
Rectangle rect = new Rectangle(0, 0, width, height);
BitmapData bitmapData = res32.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
unsafe
{
byte* bptr = (byte*)bitmapData.Scan0;
byte* r = ((byte*)hred.I);
byte* g = ((byte*)hgreen.I);
byte* b = ((byte*)hblue.I);
int lengh = width * height;
for (int i = 0; i < lengh; i++)
{
bptr[i * 4] = (b)[i];
bptr[i * 4 + 1] = (g)[i];
bptr[i * 4 + 2] = (r)[i];
bptr[i * 4 + 3] = 255;
}
}
res32.UnlockBits(bitmapData);
//32位Bitmap转24位
res24 = new Bitmap(res32.Width, res32.Height,PixelFormat.Format24bppRgb);
Graphics graphics = Graphics.FromImage(res24);
graphics.DrawImage(res32, new Rectangle(0, 0, res32.Width,res32.Height));
res32.Dispose();
}
新版halcon (17版之后),提供新的转化方法,速度更快。
public void HObject2Bpp24(HObject ho_image, out Bitmap res24)
{
//HOperatorSet.WriteImage(ho_image, "bmp", 0, @"E:\Dick\CASE\Type C R1.3&0.9\data\20200520\NG原图\CCD1\ex\xiaok1.bmp");
HTuple width0, height0, type, width, height;
//获取图像尺寸
HOperatorSet.GetImageSize(ho_image, out width0, out height0);
创建交错格式图像
HOperatorSet.InterleaveChannels(ho_image, out HObject InterImage, "argb", "match", 255); //"rgb", 4 * width0, 0 "argb", "match", 255
//获取交错格式图像指针
HOperatorSet.GetImagePointer1(InterImage, out HTuple Pointer, out type, out width, out height);
IntPtr ptr = Pointer;
//构建新Bitmap图像
Bitmap res32 = new Bitmap(width / 4, height, width, PixelFormat.Format32bppArgb, ptr); // Format32bppArgb Format24bppRgb
//32位Bitmap转24位
res24 = new Bitmap(res32.Width, res32.Height, PixelFormat.Format24bppRgb);
Graphics graphics = Graphics.FromImage(res24);
graphics.DrawImage(res32, new Rectangle(0, 0, res32.Width, res32.Height));
res32.Dispose();
}
4. 8灰度Hobject 转 bitmap
此方法,我在X64平台下使用存在问题,ptr[0] = bitmapData.Scan0.ToInt32(); 会报错。
private void HObject2Bpp8(HObject image, out Bitmap res)
{
HTuple hpoint, type, width, height;
const int Alpha = 255;
int[] ptr = new int[2];
HOperatorSet.GetImagePointer1(image, out hpoint, out type, out width, out height);
res = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
ColorPalette pal = res.Palette;
for (int i = 0; i <= 255; i++)
{
pal.Entries[i] = Color.FromArgb(Alpha, i, i, i);
}
res.Palette = pal;
Rectangle rect = new Rectangle(0, 0, width, height);
BitmapData bitmapData = res.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
int PixelSize = Bitmap.GetPixelFormatSize(bitmapData.PixelFormat) / 8;
ptr[0] = bitmapData.Scan0.ToInt32();
ptr[1] = hpoint.I;
if (width % 4 == 0)
CopyMemory(ptr[0], ptr[1], width * height * PixelSize);
else
{
for (int i = 0; i < height - 1; i++)
{
ptr[1] += width;
CopyMemory(ptr[0], ptr[1], width * PixelSize);
ptr[0] += bitmapData.Stride;
}
}
res.UnlockBits(bitmapData);
}
之后,我对代码进行如下改造,可使用
public void HObject2Bpp8_(HObject image, out Bitmap res)
{
HTuple hpoint, type, width, height;
const int Alpha = 255;
HOperatorSet.GetImagePointer1(image, out hpoint, out type, out width, out height);
res = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
ColorPalette pal = res.Palette;
for (int i = 0; i <= 255; i++)
{
pal.Entries[i] = Color.FromArgb(Alpha, i, i, i);
}
res.Palette = pal;
Rectangle rect = new Rectangle(0, 0, width, height);
BitmapData bitmapData = res.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
int PixelSize = Bitmap.GetPixelFormatSize(bitmapData.PixelFormat) / 8;
IntPtr ptr1 = bitmapData.Scan0;
IntPtr ptr2 = hpoint;
int bytes = width * height;
byte[] rgbvalues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr2, rgbvalues, 0, bytes);
System.Runtime.InteropServices.Marshal.Copy(rgbvalues, 0, ptr1, bytes);
res.UnlockBits(bitmapData);
}
第一次尝试写博客,希望可坚持下去。