有很多学员有疑惑:我们不是一直学labview,为什么突然变成了c#+halcon?其实大家不必疑惑,工业视觉涉及的领域和软件很广,我们多了解一些不同的算法平台,将来有一天说不定能用得上,最终目标都是用labview这个平台来进行调用。
目前已知的labview调用第三方平台的方式有:1.调用exe,2,调用dll库(c++),3.调用.net库,4.调用ActiveX控件,5调用 windows api (.net)。
他山之石,可以攻玉。对于我们工业视觉领域,能用得上的地方有:labview .net调用halcon,labview通过c++封装库dll后调用dll,labview通过c#封装类库后调用dll(例如激光打标行业中金橙子打标软件labview二次开发),labview调用c#版的opencv做计算机视觉,labview调用tensorflow或caffe封装后的库做深度学习。
因此学会不同的库的使用,对将来突破你的编程瓶颈有极大的帮助。
下面开始halcon的编程:
1.新建相关变量
HObject himage, ho_ModelContours, ho_TransContours;
HTuple hv_Width, hv_Height;
HTuple hv_HomMat2D, modelID;
HTuple hv_Row1, hv_Column1, hv_Row2, hv_Column2, hv_RefRow, hv_RefColumn;
HTuple hv_Row, hv_Column, hv_Angle, hv_Score, hv_I;
2.打开图片
openFileDialog1.Filter = "JPEG文件|*.jpg|BMP文件|*.bmp|png文件|*.png";//过滤文件格式
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
HOperatorSet.ReadImage(out himage, (HTuple)openFileDialog1.FileName);
HOperatorSet.GetImageSize(himage, out hv_Width, out hv_Height);//获取图像尺寸
HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, hv_Height - 1, hv_Width - 1);//设置显示控件尺寸
HOperatorSet.DispObj(himage, hWindowControl1.HalconWindow);
}
3.加载并显示模板
openFileDialog2.Filter = "形状匹配模板文件|*.shm";//过滤文件格式
if (openFileDialog2.ShowDialog() == DialogResult.OK)
{
HOperatorSet.ReadShapeModel((HTuple)openFileDialog2.FileName, out modelID);
HOperatorSet.GetShapeModelContours(out ho_ModelContours, modelID, 1);
//HOperatorSet.GetImageSize(ho_ModelContours, out hv_Width, out hv_Height);//获取图像尺寸
//HOperatorSet.SetPart(hWindowControl2.HalconWindow, 0, 0, hv_Height - 1, hv_Width - 1);//设置显示控件尺寸
HOperatorSet.SmallestRectangle1Xld(ho_ModelContours, out hv_Row1, out hv_Column1,out hv_Row2,
out hv_Column2);
hv_RefRow = ((hv_Row2.TupleMax()) - (hv_Row1.TupleMin())) / 2;
hv_RefColumn = ((hv_Column2.TupleMax()) - (hv_Column1.TupleMin())) / 2;
HOperatorSet.VectorAngleToRigid(0, 0, 0, hv_RefRow, hv_RefColumn, 0, out hv_HomMat2D);
HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_TransContours, hv_HomMat2D);
HOperatorSet.SetColor(hWindowControl2.HalconWindow, "green");
HOperatorSet.DispObj(ho_TransContours, hWindowControl2.HalconWindow);
}
4.匹配并显示结果
HOperatorSet.FindShapeModel(himage, modelID, (new HTuple(0)).TupleRad()
, (new HTuple(360)).TupleRad(), 0.5, 1, 0.5, "least_squares", (new HTuple(4)).TupleConcat(
1), 0.75, out hv_Row, out hv_Column, out hv_Angle, out hv_Score);
for (hv_I = 0; (int)hv_I <= (int)((new HTuple(hv_Score.TupleLength())) - 1); hv_I = (int)hv_I + 1)
{
HOperatorSet.HomMat2dIdentity(out hv_HomMat2D);
{
HTuple ExpTmpOutVar_0;
HOperatorSet.HomMat2dRotate(hv_HomMat2D, hv_Angle.TupleSelect(hv_I), 0, 0,
out ExpTmpOutVar_0);
hv_HomMat2D.Dispose();
hv_HomMat2D = ExpTmpOutVar_0;
}
{
HTuple ExpTmpOutVar_0;
HOperatorSet.HomMat2dTranslate(hv_HomMat2D, hv_Row.TupleSelect(hv_I), hv_Column.TupleSelect(
hv_I), out ExpTmpOutVar_0);
hv_HomMat2D.Dispose();
hv_HomMat2D = ExpTmpOutVar_0;
}
ho_TransContours.Dispose();
HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_TransContours,
hv_HomMat2D);
HOperatorSet.SetColor(hWindowControl1.HalconWindow, "green");
HOperatorSet.DispObj(ho_TransContours, hWindowControl1.HalconWindow);
}
龙哥手把手教您LabVIEW视觉设计课程火热上线!!详情可点击下方链接进行查看:http://t.elecfans.com/c801.html