halcon格式的图像转位图 --Hobjct转Bitmap
#应用场景
主要用于halcon的Hobject类型的图像转换为Bitmap的格式,先把Himage的图像转为Hobject再转为Bitmap,
目前AI的算法主要是使用位图进行计算`
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HalconDotNet;
using System.Drawing;
using System.Drawing.Imaging;
namespace ConsoleApplication4
{
class Program
{
// Initialize local and output iconic variable
static void Main(string[] args)
{
Bitmap s;
HObject ho_Image1;
HOperatorSet.ReadImage(out ho_Image1, "C:/Users/Desktop/pps.jpg");
HObject2Bpp24Net45(ho_Image1, out s);
}
//net4.5及以上环境的hobject转bitmap24方法
/// <summary>
/// HObject转24位Bitmap,net4.5及以上版本
/// </summary>
/// <param name="image"></param>
/// <param name="res"></param>
public static void HObject2Bpp24Net45(HObject image, out Bitmap res)
{
try
{
HTuple hred, hgreen, hblue, type, width, height;
HOperatorSet.GetImagePointer3(image, out hred, out hgreen, out hblue, out type, out width, out height);
res = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
Rectangle rect = new Rectangle(0, 0, width, height);
BitmapData bitmapData = res.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] = (b)[i];
bptr[i * 4 + 1] = (g)[i];
bptr[i * 4 + 2] = (r)[i];
bptr[i * 4 + 3] = 255;
}
}
res.UnlockBits(bitmapData);
}
catch (Exception ex)
{
res = null;
throw ex;
}
}
}
}