C#联合halcon完成直线查找
一、前语
C#之Winform + Halcon完成图画直线查找功用
1、创立计量模型 HMetrologyModel
2、增加计量目标到线丈量
3、设置计量目标参数
4、设置查找参数:边际阈值、边际挑选、过度办法
5、显现丈量点
二、运转成果
三、代码
double r1 = 50.00, c1 = 300.0;
double r2 = 150.0, c2 = 300.0;
double measureLength1 = 10, measureLength2 = 10;
double measureSigma = 1, measureThreshold = 10;
HTuple genParamName = new HTuple();
HTuple genParamValue = new HTuple();
//*创立计量模型
HMetrologyModel metrologyHandle =new HMetrologyModel();
//*增加计量目标到线丈量()
metrologyHandle.AddMetrologyObjectLineMeasure(r1,c1,r2,c2,
measureLength1, measureLength2, measureSigma, measureThreshold,
genParamName, genParamValue);
//*设置计量目标参数
metrologyHandle.SetMetrologyObjectParam( 0, "measure_length1",100);
metrologyHandle.SetMetrologyObjectParam(0, "measure_length2",20);
metrologyHandle.SetMetrologyObjectParam(0, "measure_distance",10);
//*边际阈值
metrologyHandle.SetMetrologyObjectParam(0, "measure_threshold",10);
//*边际挑选
metrologyHandle.SetMetrologyObjectParam(0, "measure_select","first");
//*过度办法
metrologyHandle.SetMetrologyObjectParam(0, "measure_transition","positive");
//ho_image为自己创立的HImage目标实例 HImage ho_image = new HImage()
//
metrologyHandle.ApplyMetrologyModel(ho_image);
//*显现丈量点
HXLDCont contours = new HXLDCont();
contours = metrologyHandle.GetMetrologyObjectMeasures(0, "all", out HTuple row, out HTuple column);
HXLDCont xldCont = new HXLDCont();
xldCont.GenCrossContourXld(row, column, 10, 0.785398);
HTuple result = metrologyHandle.GetMetrologyObjectResult(0, "all", "result_type",
new HTuple(new string[] { "row_begin", "column_begin", "row_end", "column_end" }));
//*取得丈量直线
double row_begin = result[0];
double column_begin = result[1];
double row_end = result[2];
double column_end = result[3];
HXLDCont xldLine = new HXLDCont();
xldLine.GenContourPolygonXld(
new HTuple(new double[] { row_begin, row_end }),
new HTuple(new double[] { column_begin, column_end })
);
//设置色彩并显现
//自己封装的办法
hWTool.DispObj(contours,"blue");//丈量的直线规模
hWTool.DispObj(xldLine, "blue");//丈量出来的直线
hWTool.DispObj(xldCont, "green");//丈量出来的点
//hWindowControl1为HWindowControl目标实例
//hWindowControl1.HalconWindow.SetColor("blue");
//hWindowControl1.HalconWindow.DispObj(xldLine);//丈量出来的直线
//hWindowControl1.HalconWindow.SetColor("red");
//hWindowControl1.HalconWindow.DispObj(contours);//丈量的直线规模
// hWindowControl1.HalconWindow.SetColor("green");
//hWindowControl1.HalconWindow.DispObj(xldCont);//丈量出来的点