Halcon图像处理入门篇之C++
上一篇说到过halcon的特点在于它实现了从halcon自创语言到C、C++、Java、Python、C#语言的转换,从而为基于图像类的开发节省了大量的宝贵时间。本篇开篇将以C++为例阐述Halcon从验证脚本和C++的无缝连接。
1、开发环境,VS2015,Halcon20.11
2、测试项目
开发环境搭建
1、关于halcon 的License,详情请见4y9b提取码:4y9b
2、halcon转换为C++
3、VS配置
// Local iconic variables
HObject ho_Image, ho_ThreImage, ho_ConnectImage;
HObject ho_SelectImage, ho_GetRec, ho_ROIImage, ho_ImageLaplace;
HObject ho_Border, ho_ObjectSelected, ho_SmoothedContours;
// Local control variables
HTuple hv_R1, hv_C1, hv_R2, hv_C2, hv_Row1, hv_Col1;
HTuple hv_Row2, hv_Col2, hv_errdist, hv_distRow, hv_a;
HTuple hv_e, hv_g, hv_KeyValue, hv_i, hv_result;
ReadImage(&ho_Image, "C:/test.jpg");
Threshold(ho_Image, &ho_ThreImage, 0, 4);
Connection(ho_ThreImage, &ho_ConnectImage);
SelectShape(ho_ConnectImage, &ho_SelectImage, ((HTuple("area").Append("inner_height")).Append("rect2_len1")),
"and", ((HTuple(2000).Append(30)).Append(20)), ((HTuple(10000).Append(100)).Append(60)));
SmallestRectangle1(ho_SelectImage, &hv_R1, &hv_C1, &hv_R2, &hv_C2);
if (0 != (int((hv_R1.TupleLength())==0)))
{
}
GenRectangle1(&ho_GetRec, hv_R1-10, hv_C1, hv_R2, hv_C2);
ReduceDomain(ho_Image, ho_GetRec, &ho_ROIImage);
LaplaceOfGauss(ho_ROIImage, &ho_ImageLaplace, 0.5);
ThresholdSubPix(ho_ImageLaplace, &ho_Border, 1);
SelectObj(ho_Border, &ho_ObjectSelected, 1);
SmoothContoursXld(ho_ObjectSelected, &ho_SmoothedContours, 15);
GetContourXld(ho_SmoothedContours, &hv_Row1, &hv_Col1);
GetContourXld(ho_ObjectSelected, &hv_Row2, &hv_Col2);
hv_errdist = 0.5;
hv_distRow = (((hv_Row1-hv_Row2)*(hv_Row1-hv_Row2))+((hv_Col1-hv_Col2)*(hv_Col1-hv_Col2))).TupleSqrt();
hv_a = (hv_distRow.TupleGreaterElem(hv_errdist)).TupleFind(1);
hv_e = HTuple(hv_Row2[hv_a]);
hv_g = HTuple(hv_Col2[hv_a]);
hv_KeyValue = 0;
{
HTuple end_val23 = (hv_e.TupleLength())-1;
HTuple step_val23 = 1;
for (hv_i=0; hv_i.Continue(end_val23, step_val23); hv_i += step_val23)
{
hv_KeyValue += HTuple(hv_e[hv_i]);
}
}
hv_result = (hv_KeyValue/(220*(hv_e.TupleLength())))+3.2;
输入图像:
效果
结果: