Halcon——在C#中各数据类型的相互转换
- 前言
- 一、HObject to
- 1.HObject to HImage
- 二、HTuple to
- 1.HTuple to Int
- 2.HTuple to Double
- 3.HTuple to String
- 4.HTuple to long
- 5.HTuple to object
- 6.HTuple to Arr
- 总结
前言
用c#进行Halcon代码转换的时候,虽然有halcon自带的转换,但是当我们外部封装时也同样会用到Hobject ->HRegion,HTuple -> 标准数据类型(int,double,string。。。),在此写下记录,方便以后查看。
一、HObject to
1.HObject to HImage
// 转换成灰度图片
public static HImage HObject2HImage_Gray(HObject hObject)
{
HImage image = new HImage();
HTuple pointer, type, width, height;
HOperatorSet.GetImagePointer1(hObject, out pointer, out type, out width, out height);
image.GenImage1(type, width, height, pointer);
pointer.Dispose();
type.Dispose();
width.Dispose();
height.Dispose();
hObject.Dispose();
return image;
}
// 转换成彩色图片
public static HImage HObject2HImage_RGB(HObject hobject)
{
HImage image = new HImage();
HTuple pointerRed, pointerGreen, pointerBlue, type, width, height;
HOperatorSet.GetImagePointer3(hobject, out pointerRed, out pointerGreen, out pointerBlue, out type, out width, out height);
image.GenImage3(type, width, height, pointerRed, pointerGreen, pointerBlue);
pointerRed.Dispose();
pointerGreen.Dispose();
pointerBlue.Dispose();
type.Dispose();
width.Dispose();
height.Dispose();
return image;
}
二、HTuple to
1.HTuple to Int
代码如下(示例):
int val = htuple.I;
2.HTuple to Double
代码如下(示例):
double val = htuple.D;
3.HTuple to String
代码如下(示例):
string val = htuple.S;
4.HTuple to long
代码如下(示例):
long val = htuple.L;
5.HTuple to object
代码如下(示例):
object val = htuple.O;
6.HTuple to Arr
转换成上述1-5的各种形式的数组。
如下:
int[] iArr = htuple.IArr;
double[] iArr = htuple.DArr;
string[] iArr = htuple.SArr;
long[] iArr = htuple.LArr;
object[] iArr = htuple.OArr;
总结
想要转换首先得能够转换才行欧,整个HTuple中存了个double类型的数据,却用HTuple.I往整型转换是会报错失败的。所以转换前请确保原有数据格式的类型。