C#聯合halcon實現直線查詢

吾与谁归發表於2024-12-09

一、前言

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);//測量出來的點


相關文章