.net 中Bitmap和Halcon中HObject的相互转换
来源:互联网 发布:博弈矩阵怎么看 编辑:程序博客网 时间:2024/06/01 23:52
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; } } 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; } }
实际使用时,假如原图8位灰度图,那么BitmapToHObjectBpp8 和BitmapToHObjectBpp24的结果是一样的。而为24位彩色图时,只能用BitmapToHObjectBpp24。
可先得到Bitmap 图的PixelFormat ,而后再进行转换。
以下HObject to Bitmap部分参考:http://blog.csdn.net/miehuo/article/details/48751353
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); } private void HObject2Bpp24(HObject image, out Bitmap res) { 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); unsafe { byte* bptr = (byte*)bitmapData.Scan0; byte* r = ((byte*)hred.I); byte* g = ((byte*)hgreen.I); byte* b = ((byte*)hblue.I); for (int i = 0; i < width * height; 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); }
阅读全文
0 0
- .net 中Bitmap和Halcon中HObject的相互转换
- Halcon对象Hobject转换为.net对象Bitmap
- Hobject 和 Mat 的相互转换
- OpenCV的Mat和Halcon的HObject类型互相转换
- Bitmap和Drawable之间的相互转换、Bitmap和 byte[] 之间的相互转换、从资源中获取Bitmap
- Hobject 与 OpenCV IplImage 和 Mat 的相互转换
- Android中Bitmap和Drawable相互转换
- Android中Bitmap和Drawable相互转换
- Android中 Bitmap和Drawable相互转换的方法
- Android中 Bitmap和Drawable相互转换的方法
- Android中 Bitmap和Drawable相互转换的方法
- Android中 Bitmap和Drawable相互转换的方法
- Android中 Bitmap和Drawable相互转换的方法
- Android中 Bitmap和Drawable相互转换的方法
- Android 中 Bitmap 和 Drawable 相互转换的方法
- Android中 Bitmap和Drawable相互转换的方法
- Android中 Bitmap和Drawable相互转换的方法
- Android中 Bitmap和Drawable相互转换的方法
- osgEarth地形压平案例 37. feature_elevation.earth
- Ubuntu 14配置网络
- 动态代理模式的两种实现 —— jdk实现 VS cglib实现
- c语言指针详解
- Git常用命令总结
- .net 中Bitmap和Halcon中HObject的相互转换
- QT开发 Model/View/delegate/selection
- iPhoneX下拉特效
- 博客使用到的公共图片
- CCF 201604-1 折点计数
- 从word中拷贝代码到vc中空格出现错误解决
- Java根据日期取得星期几
- 基于Puppeteer实现地图打印输出
- 银根收不收的逻辑