该用户从未签到


- 积分
- 14
|
51Halcon诚邀您的加入,专注于机器视觉开发与应用技术,我们一直都在努力!
您需要 登录 才可以下载或查看,没有帐号?会员注册
x
本帖最后由 王耀辉 于 2020-10-28 10:07 编辑
- 本文代码部分来此其他网站的copy,但是我在使用过程中,发现存在以下两个问题:
1.hobject 灰度图转bitmap 灰度图方法,在X64平台上报错。
2. 原版代码HObject2Bpp32 这个方法,我在方法内用 Graphics 类把Format32bppArgb 重画为Format24bppRgb,
但是在连续调用用此方法时,会报内存错误。
- 上述两个问题,我在网上搜索了很久,都找不到解决方案。
- 之后我仔细研究研究转换原理,最后在原有代码基础上改造成功。
文中只贴出了bitmap 转Hobject代码(包括灰度图和彩图),附档中包含Hobject 转bitmap(包括灰度图和彩图),此部分有本人自创内容,完整代码请下载附档查看。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using HalconDotNet;
using System.IO;
using System.Drawing.Imaging;
using System.Runtime.InteropServices; // 必须要有
using System.Diagnostics;
public class bmp_halcon
{
[DllImport("kernel32.dll")]
public static extern void CopyMemory(int Destination, int add, int Length);
//彩色图转换
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;
}
}
// 32位平台可用,附档内有可用于64位平台可用的方法
public void HObject2Bpp8(HObject image, out Bitmap res)
{
HTuple hpoint, type, width, height;
const int Alpha = 255;
int[] ptr = new int[2];
//.....................................
}
// old way
public void HObject2Bpp32(HObject image, out Bitmap res)
{
HTuple hred, hgreen, hblue, type, width, height;
//.........................................................
}
// halcon 17 之后的版本可用,速度比上个方法(HObject2Bpp32 )快很多
public void HObject2Bpp24(HObject ho_image, out Bitmap res24)
{
HTuple width0, height0, type, width, height;
//获取图像尺寸
HOperatorSet.GetImageSize(ho_image, out width0, out height0);
//..........................................................
}
}
|
|