HObject和Bitmap和Byte[]互转方法统计

        图像处理用到的图片对象格式一版有HObject(Halcon),Mat(Opencv),Bitmap(过程中用到或显示),Byte[](过程中用到),首先有两点建议:
1. 如果视觉框架使用到了几种图片对象格式,建议封装一个基于Byte[]的图像类,内部封装方法有选择性的生成HObject,Mat等

2.不建议使用Bitmap,包括处理和显示,多线程使用同一个实例容易报错

下面是主要写的内容,关于HObject和Bitmap和Byte[]互转方法,以下数据均是离线测试结果,如果哪里有问题,还请指正

 1. Hobject<->Bitmap(2000万像素彩色图片测试)HObject和Bitmap和Byte[]互转方法统计_第1张图片

 2. Hobject<->Bitmap(2000万像素黑白图片测试)HObject和Bitmap和Byte[]互转方法统计_第2张图片

 3. byte[]转Hobject(2000万像素彩色图片测试)

4. byte[]转Hobject(2000万像素黑白图片测试)

注:第3和第4byte[]转Hobject存在Y镜像和X偏移情况,后续修复后再更新

测试源码如下:

HOperatorSet.GenEmptyObj(out HObject hObject);

Stopwatch sw = new Stopwatch();
double t = sw.ElapsedMilliseconds;
sw.Start();

//------------------------//1.1Hobject ->Bitmap(2000万像素彩色图片测试)-------------------------------
HOperatorSet.ReadImage(out hObject, "color.bmp");
Bitmap outBitmapShow = ConvertBase.HObjectToBitmapWithOpenCv(hObject);
//------------------------//1.2Hobject <-Bitmap(2000万像素彩色图片测试)-------------------------------
hObject?.Dispose();
hObject = ConvertBase.BitmapToHObjectWithOpenCv(outBitmapShow);
HOperatorSet.WriteImage(hObject, "bmp", 0, "result1.bmp");


//------------------------2.1Hobject -> Bitmap(2000万像素h黑白图片测试)-------------------------------
HOperatorSet.ReadImage(out hObject, "gray.bmp");
outBitmapShow = ConvertBase.HObjectToBitmapWithOpenCv(hObject);
//------------------------//2.2Hobject <-Bitmap(2000万像素黑白图片测试)-------------------------------
hObject?.Dispose();
hObject = ConvertBase.BitmapToHObjectWithOpenCv(outBitmapShow);
HOperatorSet.WriteImage(hObject, "bmp", 0, "result2.bmp");



//------------------------//3. byte[]转Hobject(2000万像素彩色图片测试)-------------------------------
Bitmap bitmap = new Bitmap(@"./color.bmp");
//bitmap转byte[]
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] bytes = ms.GetBuffer();
//byte[] bytes = ms.ToArray(); 这两句都可以,至于区别么,下面有解释
ms.Close();
//byte[]转bitmap
byte[] bytelist = bytes;
MemoryStream ms1 = new MemoryStream(bytelist);
Bitmap outBitmap = (Bitmap)System.Drawing.Image.FromStream(ms1);
ms1.Close();
int width = outBitmap.Width;
int height = outBitmap.Height;
string pFrameInfoEnPixelType = "PixelType_Gvsp_RGB8_Packed";//24位
hObject?.Dispose();
hObject = ConvertBase.BytesToHObject(bytes, width, height, pFrameInfoEnPixelType);
HOperatorSet.WriteImage(hObject, "bmp", 0, "result3.bmp");

//------------------------//4. byte[] 转Hobject(2000万像素黑白图片测试)-------------------------------
bitmap = new Bitmap(@"./gray.bmp");
//bitmap转byte[]
ms = new MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
bytes = ms.GetBuffer();
//byte[] bytes = ms.ToArray(); 这两句都可以,至于区别么,下面有解释
ms.Close();
//byte[]转bitmap
bytelist = bytes;
ms1 = new MemoryStream(bytelist);
outBitmap = (Bitmap)System.Drawing.Image.FromStream(ms1);
ms1.Close();
width = outBitmap.Width;
height = outBitmap.Height;
pFrameInfoEnPixelType = "PixelType_Gvsp_Mono8";//8位
hObject?.Dispose();
hObject = ConvertBase.BytesToHObject(bytes, width, height, pFrameInfoEnPixelType);
HOperatorSet.WriteImage(hObject, "bmp", 0, "result4.bmp");

t = sw.ElapsedMilliseconds;
sw.Stop();

 篇幅有限,转换Wpf源码和统计表格详见CSDN资源:(156条消息) HObject和Bitmap&Byte互转方法统计-C#文档类资源-CSDN文库

你可能感兴趣的:(工业机器视觉-传统算法应用,数据处理算法,计算机视觉,图像处理)