public static bool createCircleMetrology(paramCircle param, ref HTuple handle) { HTuple index; try { if (null != handle && 0 < handle.Length) { HOperatorSet.ClearMetrologyModel(handle); } HOperatorSet.CreateMetrologyModel(out handle); HOperatorSet.AddMetrologyObjectCircleMeasure(handle, param.circleRow, param.circleColumn, param.circleRadius, param.MeasureLenght1, param.MeasureLenght2, 1, param.MeasureThreshold, "measure_transition", param.MeasureTransition, out index); HOperatorSet.SetMetrologyObjectParam(handle, "all", "num_measures", param.NumMeasures); HOperatorSet.SetMetrologyObjectParam(handle, "all", "num_instances", param.NumInstances); HOperatorSet.SetMetrologyObjectParam(handle, "all", "min_score", param.MinScore); if (0 < handle.Length) { return(true); } } catch (HalconException ex) { string msg = ex.GetErrorMessage(); } return(false); }
private void ExitApp(object sender, EventArgs e) { if (this.StartTest_button.Text == "Start") { StartTest(null, null); } Thread.Sleep(2000); IO_ThreadStop = false; AcquireImageStop = false; Thread.Sleep(2000); HOperatorSet.CloseFramegrabber(hv_AcqHandle); //关闭相机 ho_Image.Dispose(); //释放对象 IO_Thread.Abort(); AcquireImage.Abort(); HOperatorSet.ClearMetrologyModel(hv_MetrologyHandle); HOperatorSet.ClearShapeModel(hv_ModelID); #if (NormalTest) MotionObject.CloseMotionCard(); #endif System.Environment.Exit(0); }
///<summary> ///创建直线检测模板(仿射变换) ///</summary> public void CreateLineModel(HTuple hv_HomMat2D, HTuple hv_MeasureRow1, HTuple hv_MeasureColumn1, HTuple hv_MeasureRow2, HTuple hv_MeasureColumn2, HTuple hv_MeasureLength1, HTuple hv_MeasureLength2, HTuple hv_MeasureSigma, HTuple hv_MeasureThreshold, out HTuple hv_MetrologyHandle, out HTuple hv_Index) { HTuple mhv_Row1; HTuple mhv_Column1; HTuple mhv_Row2; HTuple mhv_Column2; HTuple mhv_MetrologyHandle = new HTuple(); hv_Index = new HTuple(); hv_MetrologyHandle = new HTuple(); try { HOperatorSet.ClearMetrologyModel(mhv_MetrologyHandle); HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle); HOperatorSet.AffineTransPixel(hv_HomMat2D, hv_MeasureRow1, hv_MeasureColumn1, out mhv_Row1, out mhv_Column1); HOperatorSet.AffineTransPixel(hv_HomMat2D, hv_MeasureRow2, hv_MeasureColumn2, out mhv_Row2, out mhv_Column2); HOperatorSet.DistancePp(mhv_Row1, mhv_Column1, mhv_Row2, mhv_Column2, out HTuple DistancePp); hv_MeasureLength2 = DistancePp / 2; HOperatorSet.AddMetrologyObjectLineMeasure(mhv_MetrologyHandle, mhv_Row1, mhv_Column1, mhv_Row2, mhv_Column2, hv_MeasureLength1, hv_MeasureLength2, hv_MeasureSigma, hv_MeasureThreshold, new HTuple(), new HTuple(), out hv_Index); hv_MetrologyHandle = mhv_MetrologyHandle; return; } catch (Exception) { return; } }
///<summary> ///创建直线检测模板(无仿射变换) ///</summary> public void CreateLineModel(HTuple hv_MeasureRow1, HTuple hv_MeasureColumn1, HTuple hv_MeasureRow2, HTuple hv_MeasureColumn2, HTuple hv_MeasureLength1, HTuple hv_MeasureLength2, HTuple hv_MeasureSigma, HTuple hv_MeasureThreshold, out HTuple hv_MetrologyHandle, out HTuple hv_Index) { HTuple mhv_MetrologyHandle = new HTuple(); hv_MetrologyHandle = new HTuple(); hv_Index = new HTuple(); hv_MetrologyHandle = new HTuple(); try { HOperatorSet.ClearMetrologyModel(mhv_MetrologyHandle); HOperatorSet.CreateMetrologyModel(out mhv_MetrologyHandle); //HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); HOperatorSet.AddMetrologyObjectLineMeasure(mhv_MetrologyHandle, hv_MeasureRow1, hv_MeasureColumn1, hv_MeasureRow2, hv_MeasureColumn2, hv_MeasureLength1, hv_MeasureLength2, hv_MeasureSigma, hv_MeasureThreshold, new HTuple(), new HTuple(), out hv_Index); hv_MetrologyHandle = mhv_MetrologyHandle; return; } catch (Exception) { return; } }
public bool CreateLineModel() { try { HOperatorSet.ClearMetrologyModel(hv_MetrologyHandle); HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle); if (ho_Image == null) { return(false); } HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); if (hv_Row1.Length < 1) { return(false); } HOperatorSet.AddMetrologyObjectLineMeasure(hv_MetrologyHandle, hv_Row1, hv_Column1, hv_Row2, hv_Column2, hv_MeasureLength1, hv_MeasureLength2, hv_MeasureSigma, hv_MeasureThreshold, hv_GenParamName, hv_GenParamValue, out hv_Index); errorFlag = false; return(true); } catch (Exception exc) { errorFlag = true; return(false); } }
///<summary> ///直线拟合(仿射变换) ///</summary> public void FindLine(HTuple hv_HomMat2DPix, HObject ho_Image, HTuple hv_MetrologyHandle, HTuple hv_Index, HTuple hv_MeasureNumInstances, HTuple hv_MeasureDistThreshold, HTuple hv_MeasureTransition, HTuple hv_MeasureSelect, HTuple hv_MeasurePointsNum, HTuple hv_MeasureMinScore, HTuple hv_MaxNumIterations, HTuple hv_MeasureInterpolation, HTuple hv_MeasureIORegions, out HTuple hv_CrossRow, out HTuple hv_CrossColumn, out HTuple hv_LineRowBegin_Real, out HTuple hv_LineColumnBegin_Real, out HTuple hv_LineRowEnd_Real, out HTuple hv_LineColumnEnd_Real) { HObject ho_Contours = new HObject(); HTuple hv_LineRowBegin = new HTuple(); HTuple hv_LineColumnBegin = new HTuple(); HTuple hv_LineRowEnd = new HTuple(); HTuple hv_LineColumnEnd = new HTuple(); hv_CrossRow = new HTuple(); hv_CrossColumn = new HTuple(); hv_LineRowBegin_Real = new HTuple(); hv_LineColumnBegin_Real = new HTuple(); hv_LineRowEnd_Real = new HTuple(); hv_LineColumnEnd_Real = new HTuple(); try { //1.直线拟合数量:默认值: 1 值的列表: 1,2,3,4 HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "num_instances", hv_MeasureNumInstances); //2.距离剔除:默认值: 3.5 值的列表: 0,1.0,2.0, 3.5,5.0 HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "distance_threshold", hv_MeasureDistThreshold); //3.明暗方向:值列表: 'all','negative', 'positive','uniform' HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "measure_transition", hv_MeasureTransition); //4.边缘选取:'all','first','last' HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "measure_select", hv_MeasureSelect); //5.Roi数量:'num_measures' HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "num_measures", hv_MeasurePointsNum); //6.最小得分:'min_score' HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "min_score", hv_MeasureMinScore); //7.迭代次数: 默认值:-1 值的列表: 10,100,1000 HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "max_num_iterations", hv_MaxNumIterations); //8.插值类型:nearest_neighbor'灰度值是从最近像素的灰度值获得;'bilinear',使用双线性插值 ;'bicubic',则使用双三次插值。 HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "measure_interpolation", hv_MeasureInterpolation); //9.指定对测量结果的验证:默认值: “ false” 值列表: 'true','false' HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "instances_outside_measure_regions", hv_MeasureIORegions); HOperatorSet.ApplyMetrologyModel(ho_Image, hv_MetrologyHandle); HOperatorSet.GetMetrologyObjectMeasures(out ho_Contours, hv_MetrologyHandle, "all", "all", out hv_CrossRow, out hv_CrossColumn); HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type", "row_begin", out hv_LineRowBegin); HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type", "column_begin", out hv_LineColumnBegin); HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type", "row_end", out hv_LineRowEnd); HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type", "column_end", out hv_LineColumnEnd); HOperatorSet.AffineTransPixel(hv_HomMat2DPix, hv_LineRowBegin, hv_LineColumnBegin, out hv_LineRowBegin_Real, out hv_LineColumnBegin_Real); HOperatorSet.AffineTransPixel(hv_HomMat2DPix, hv_LineRowEnd, hv_LineColumnEnd, out hv_LineRowEnd_Real, out hv_LineColumnEnd_Real); //清除测量模型 ho_Contours.Dispose(); HOperatorSet.ClearMetrologyModel(hv_MetrologyHandle); return; } catch (Exception) { return; } }
private void btnSaveCornerParams_Click(object sender, EventArgs e) { try { HOperatorSet.ClearMetrologyModel(cornerParams.hv_MetrologyHandle); HOperatorSet.CreateMetrologyModel(out cornerParams.hv_MetrologyHandle); if (cornerParams.ho_Image == null) { MessageBox.Show("图像为空!"); return; } HOperatorSet.GetImageSize(cornerParams.ho_Image, out cornerParams.hv_Width, out cornerParams.hv_Height); HOperatorSet.SetMetrologyModelImageSize(cornerParams.hv_MetrologyHandle, cornerParams.hv_Width, cornerParams.hv_Height); if (cornerParams.hv_Row1_Horizon.Length < 1) { MessageBox.Show("水平直线模型为空!"); return; } //if (cornerParams.hv_Row1_Vertical.Length < 1) //{ // MessageBox.Show("竖直直线模型为空!"); // return; //} HTuple hv_Row1 = null; HTuple hv_Column1 = null; HTuple hv_Row2 = null; HTuple hv_Column2 = null; HOperatorSet.TupleInsert(cornerParams.hv_Row1_Horizon, 0, cornerParams.hv_Row1_Vertical, out hv_Row1); HOperatorSet.TupleInsert(cornerParams.hv_Column1_Horizon, 0, cornerParams.hv_Column1_Vertical, out hv_Column1); HOperatorSet.TupleInsert(cornerParams.hv_Row2_Horizon, 0, cornerParams.hv_Row2_Vertical, out hv_Row2); HOperatorSet.TupleInsert(cornerParams.hv_Column2_Horizon, 0, cornerParams.hv_Column2_Vertical, out hv_Column2); HOperatorSet.AddMetrologyObjectLineMeasure(cornerParams.hv_MetrologyHandle, hv_Row1, hv_Column1, hv_Row2, hv_Column2, cornerParams.hv_MeasureLength1, cornerParams.hv_MeasureLength2, cornerParams.hv_MeasureSigma, cornerParams.hv_MeasureThreshold, cornerParams.hv_GenParamName, cornerParams.hv_GenParamValue, out cornerParams.hv_Index); if (!FindCorner()) { MessageBox.Show("直线模型查找失败!"); return; } MessageBox.Show("保存直线模型成功!"); } catch (Exception exc) { MessageBox.Show("保存直线模型失败!" + exc.ToString()); } }
public static bool clearMetrology(HTuple handle) { try { if (null != handle && 0 < handle.Length) { HOperatorSet.ClearMetrologyModel(handle); } return(true); } catch (HalconException ex) { string msg = ex.GetErrorMessage(); } return(false); }
public static bool findLine(HObject image, HTuple handle, out HTuple row1, out HTuple col1, out HTuple row2, out HTuple col2, out HObject obj, out HObject contours, out HObject ho_Cross, out HTuple phi) { row1 = null; col1 = null; row2 = null; col2 = null; phi = null; HTuple hv_Row1, hv_Column1; HOperatorSet.GenEmptyObj(out obj); HOperatorSet.GenEmptyObj(out contours); HOperatorSet.GenEmptyObj(out ho_Cross); HTuple parameter; try { HOperatorSet.ApplyMetrologyModel(image, handle); HOperatorSet.GetMetrologyObjectResult(handle, 0, "all", "result_type", "all_param", out parameter); HOperatorSet.GetMetrologyObjectMeasures(out contours, handle, "all", "all", out hv_Row1, out hv_Column1); HOperatorSet.GenCrossContourXld(out ho_Cross, hv_Row1, hv_Column1, 6, 0.785398); if (4 == parameter.Length) { row1 = parameter[0]; col1 = parameter[1]; row2 = parameter[2]; col2 = parameter[3]; HOperatorSet.GenContourPolygonXld(out obj, row1.TupleConcat(row2), col1.TupleConcat(col2)); HOperatorSet.LineOrientation(row1, col1, row2, col2, out phi); HOperatorSet.ClearMetrologyObject(handle, "all"); HOperatorSet.ClearMetrologyModel(handle); return(true); } } catch (HalconException ex) { string msg = ex.GetErrorMessage(); } return(false); }
private void btnSaveLineParams_Click(object sender, EventArgs e) { try { HOperatorSet.ClearMetrologyModel(lineParams.hv_MetrologyHandle); HOperatorSet.CreateMetrologyModel(out lineParams.hv_MetrologyHandle); if (lineParams.ho_Image == null) { MessageBox.Show("图像为空!"); return; } HOperatorSet.GetImageSize(lineParams.ho_Image, out lineParams.hv_Width, out lineParams.hv_Height); HOperatorSet.SetMetrologyModelImageSize(lineParams.hv_MetrologyHandle, lineParams.hv_Width, lineParams.hv_Height); if (lineParams.hv_Row1.Length != 1) { MessageBox.Show("直线模型错误!"); return; } HOperatorSet.AddMetrologyObjectLineMeasure(lineParams.hv_MetrologyHandle, lineParams.hv_Row1, lineParams.hv_Column1, lineParams.hv_Row2, lineParams.hv_Column2, lineParams.hv_MeasureLength1, lineParams.hv_MeasureLength2, lineParams.hv_MeasureSigma, lineParams.hv_MeasureThreshold, lineParams.hv_GenParamName, lineParams.hv_GenParamValue, out lineParams.hv_Index); if (!FindCorner()) { MessageBox.Show("直线模型查找失败!"); return; } MessageBox.Show("保存直线模型成功!"); } catch (Exception exc) { MessageBox.Show("保存直线模型失败!" + exc.ToString()); } }
private void tsmiClearROI_Click(object sender, EventArgs e) { cornerParams.hv_Row1_Horizon = new HTuple(); cornerParams.hv_Column1_Horizon = new HTuple(); cornerParams.hv_Row2_Horizon = new HTuple(); cornerParams.hv_Column2_Horizon = new HTuple(); cornerParams.hv_Row1_Vertical = new HTuple(); cornerParams.hv_Column1_Vertical = new HTuple(); cornerParams.hv_Row2_Vertical = new HTuple(); cornerParams.hv_Column2_Vertical = new HTuple(); cornerParams.hv_LineRowBegin = null; cornerParams.hv_LineColumnBegin = null; cornerParams.hv_LineRowEnd = null; cornerParams.hv_LineColumnEnd = null; HOperatorSet.ClearMetrologyModel(cornerParams.hv_MetrologyHandle); HOperatorSet.CreateMetrologyModel(out cornerParams.hv_MetrologyHandle); MessageBox.Show("清除直线模型成功!"); }
private double ImageAlgorithmII(HObject ho_ResultImage) { // Local iconic variables HObject ho_ResultRegion = null; HObject ho_ResultConnectedRegions = null, ho_ResultSelectedRegions = null; HObject ho_ResultCircle = null, ho_ResultContour = null; // Local control variables HTuple hv_Width = null, hv_Height = null; HTuple hv_MetrologyHandle = null, hv_CircleRadiusTolerance = null; HTuple hv_NewRadius = null; HTuple hv_ResultArea = new HTuple(), hv_ResultRow = new HTuple(); HTuple hv_ResultColumn = new HTuple(), hv_MetrologyCircleIndice = new HTuple(); HTuple hv_CircleRadiusResult = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_ResultRegion); HOperatorSet.GenEmptyObj(out ho_ResultConnectedRegions); HOperatorSet.GenEmptyObj(out ho_ResultSelectedRegions); HOperatorSet.GenEmptyObj(out ho_ResultCircle); HOperatorSet.GenEmptyObj(out ho_ResultContour); HOperatorSet.GetImageSize(ho_ResultImage, out hv_Width, out hv_Height); //创建测量模型 HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle); //设置测量对象图像大小 HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle, hv_Width, hv_Height); // hv_CircleRadiusTolerance = 100; //圆半径 hv_NewRadius = 324; //阈值图像 ho_ResultRegion.Dispose(); HOperatorSet.Threshold(ho_ResultImage, out ho_ResultRegion, 0, 60); //区域连通处理 ho_ResultConnectedRegions.Dispose(); HOperatorSet.Connection(ho_ResultRegion, out ho_ResultConnectedRegions); //根据面积和圆度来过滤想要的圆区域 ho_ResultSelectedRegions.Dispose(); HOperatorSet.SelectShape(ho_ResultConnectedRegions, out ho_ResultSelectedRegions, (new HTuple("area")).TupleConcat("roundness"), "and", (new HTuple(250000)).TupleConcat( 0.9), (new HTuple(320000)).TupleConcat(1)); //获取圆的中心坐标和面积 HOperatorSet.AreaCenter(ho_ResultSelectedRegions, out hv_ResultArea, out hv_ResultRow, out hv_ResultColumn); HOperatorSet.SetColor(WindowID, "green"); //设置区域的填充模式 HOperatorSet.SetDraw(WindowID, "margin"); //显示图像 HOperatorSet.DispObj(ho_ResultImage, WindowID); if (hv_ResultArea.Length != 0) { //生成圆 ho_ResultCircle.Dispose(); HOperatorSet.GenCircle(out ho_ResultCircle, hv_ResultRow, hv_ResultColumn, hv_NewRadius); //添加圆到测量模型里 HOperatorSet.AddMetrologyObjectCircleMeasure(hv_MetrologyHandle, hv_ResultRow, hv_ResultColumn, hv_NewRadius, hv_CircleRadiusTolerance, 5, 1.5, 30, (new HTuple("measure_transition")).TupleConcat( "min_score"), (new HTuple("all")).TupleConcat(0.4), out hv_MetrologyCircleIndice); //测量并拟合几何形状 HOperatorSet.ApplyMetrologyModel(ho_ResultImage, hv_MetrologyHandle); //获取测量模型里的测量轮廓 ho_ResultContour.Dispose(); HOperatorSet.GetMetrologyObjectResultContour(out ho_ResultContour, hv_MetrologyHandle, "all", "all", 1.5); //获取测量模型里的测量结果 HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_MetrologyCircleIndice, "all", "result_type", "radius", out hv_CircleRadiusResult); //显示测量轮廓 HOperatorSet.DispObj(ho_ResultContour, WindowID); //设置文字显示位置 HOperatorSet.SetTposition(WindowID, 0, 0); //* 在窗口指定位置输出文字信息 HOperatorSet.WriteString(WindowID, "圆外径:" + hv_CircleRadiusResult); //清除测量模型 HOperatorSet.ClearMetrologyModel(hv_MetrologyHandle); ho_ResultRegion.Dispose(); ho_ResultConnectedRegions.Dispose(); ho_ResultSelectedRegions.Dispose(); ho_ResultCircle.Dispose(); ho_ResultContour.Dispose(); return(hv_CircleRadiusResult.D); } else { ho_ResultRegion.Dispose(); ho_ResultConnectedRegions.Dispose(); ho_ResultSelectedRegions.Dispose(); ho_ResultCircle.Dispose(); ho_ResultContour.Dispose(); return(0); } }
private void action() { // Stack for temporary objects HObject[] OTemp = new HObject[20]; // Local iconic variables HObject ho_Rectangle, ho_Rectangle1, ho_UnionContours1; HObject ho_Region, ho_ContoursL = null, ho_Contour1 = null; HObject ho_ContoursSplit2 = null, ho_RegionLines2 = null, ho_Contour = null; HObject ho_ImageReduced1 = null, ho_Edges1 = null, ho_ContoursSplit1 = null; HObject ho_UnionContours = null, ho_RegionLines1 = null, ho_Contour2 = null; // Local control variables HTuple hv_Width = new HTuple(); HTuple hv_Height = new HTuple(), hv_shapeParam = new HTuple(); HTuple hv_MetrologyHandleL = new HTuple(), hv_Index = new HTuple(); HTuple hv_RowL = new HTuple(), hv_ColumnL = new HTuple(); HTuple hv_RowBegin2 = new HTuple(), hv_ColBegin2 = new HTuple(); HTuple hv_RowEnd2 = new HTuple(), hv_ColEnd2 = new HTuple(); HTuple hv_Nr2 = new HTuple(), hv_Nc2 = new HTuple(), hv_Dist2 = new HTuple(); HTuple hv_dis2 = new HTuple(), hv_Indices2 = new HTuple(); HTuple hv_R = new HTuple(), hv_C = new HTuple(), hv_RowBegin1 = new HTuple(); HTuple hv_ColBegin1 = new HTuple(), hv_RowEnd1 = new HTuple(); HTuple hv_ColEnd1 = new HTuple(), hv_Nr1 = new HTuple(); HTuple hv_Nc1 = new HTuple(), hv_Dist1 = new HTuple(); HTuple hv_dis1 = new HTuple(), hv_Indices1 = new HTuple(); HTuple hv_R1 = new HTuple(), hv_C1 = new HTuple(), hv_DistanceMin1 = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_Rectangle1); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_ContoursL); HOperatorSet.GenEmptyObj(out ho_UnionContours1); HOperatorSet.GenEmptyObj(out ho_Contour1); HOperatorSet.GenEmptyObj(out ho_ContoursSplit2); HOperatorSet.GenEmptyObj(out ho_RegionLines2); HOperatorSet.GenEmptyObj(out ho_Contour); HOperatorSet.GenEmptyObj(out ho_ImageReduced1); HOperatorSet.GenEmptyObj(out ho_Edges1); HOperatorSet.GenEmptyObj(out ho_ContoursSplit1); HOperatorSet.GenEmptyObj(out ho_UnionContours); HOperatorSet.GenEmptyObj(out ho_Contour2); HOperatorSet.GenEmptyObj(out ho_RegionLines1); HOperatorSet.Union1(algorithm.Region, out RegionToDisp); try { HOperatorSet.GetImageSize(Image, out hv_Width, out hv_Height); ho_Rectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle, DRow1m, DCol1m, DRow2m, DCol2m); ho_Rectangle1.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle1, DRow1n, DCol1n, DRow2n, DCol2n); //* //找左侧点 hv_shapeParam = new HTuple(); hv_shapeParam = hv_shapeParam.TupleConcat(DRow1m); hv_shapeParam = hv_shapeParam.TupleConcat((DCol1m + DCol2m) / 2); hv_shapeParam = hv_shapeParam.TupleConcat(DRow2m); hv_shapeParam = hv_shapeParam.TupleConcat((DCol1m + DCol2m) / 2); HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandleL); HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandleL, hv_Width, hv_Height); HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandleL, "line", hv_shapeParam, (DCol2m - DCol1m) / 2, 1, 1, 50, new HTuple(), new HTuple(), out hv_Index); //设置参数 HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "measure_transition", "positive"); HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "num_measures", 100); HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "num_instances", 1); HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "measure_select", "last"); //开始找边缘 HOperatorSet.ApplyMetrologyModel(Image, hv_MetrologyHandleL); ho_ContoursL.Dispose(); HOperatorSet.GetMetrologyObjectMeasures(out ho_ContoursL, hv_MetrologyHandleL, "all", "all", out hv_RowL, out hv_ColumnL); //gen_cross_contour_xld (CrossL, RowL, ColumnL, 6, 0) ///释放测量句柄 HOperatorSet.ClearMetrologyModel(hv_MetrologyHandleL); ho_Contour1.Dispose(); HOperatorSet.GenContourPolygonXld(out ho_Contour1, hv_RowL, hv_ColumnL); ho_ContoursSplit2.Dispose(); HOperatorSet.SegmentContoursXld(ho_Contour1, out ho_ContoursSplit2, "lines_circles", 5, 4, 2); ho_UnionContours1.Dispose(); HOperatorSet.UnionCollinearContoursXld(ho_ContoursSplit2, out ho_UnionContours1, 10000, 10000, 2, 0.1, "attr_keep"); HOperatorSet.FitLineContourXld(ho_UnionContours1, "tukey", -1, 0, 5, 2, out hv_RowBegin2, out hv_ColBegin2, out hv_RowEnd2, out hv_ColEnd2, out hv_Nr2, out hv_Nc2, out hv_Dist2); hv_dis2 = ((((hv_RowBegin2 - hv_RowEnd2) * (hv_RowBegin2 - hv_RowEnd2)) + ((hv_ColBegin2 - hv_ColEnd2) * (hv_ColBegin2 - hv_ColEnd2)))).TupleSqrt() ; HOperatorSet.TupleFind(hv_dis2, hv_dis2.TupleMax(), out hv_Indices2); ho_RegionLines2.Dispose(); HOperatorSet.GenRegionLine(out ho_RegionLines2, hv_RowBegin2.TupleSelect(hv_Indices2), hv_ColBegin2.TupleSelect(hv_Indices2), hv_RowEnd2.TupleSelect(hv_Indices2), hv_ColEnd2.TupleSelect(hv_Indices2)); hv_R = new HTuple(); hv_R = hv_R.TupleConcat(hv_RowBegin2.TupleSelect( hv_Indices2)); hv_R = hv_R.TupleConcat(hv_RowEnd2.TupleSelect(hv_Indices2)); hv_C = new HTuple(); hv_C = hv_C.TupleConcat(hv_ColBegin2.TupleSelect( hv_Indices2)); hv_C = hv_C.TupleConcat(hv_ColEnd2.TupleSelect(hv_Indices2)); ho_Contour.Dispose(); HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_R, hv_C); //2 ho_ImageReduced1.Dispose(); HOperatorSet.ReduceDomain(Image, ho_Rectangle1, out ho_ImageReduced1); ho_Edges1.Dispose(); HOperatorSet.EdgesSubPix(ho_ImageReduced1, out ho_Edges1, "canny", 1, 20, 40); ho_ContoursSplit1.Dispose(); HOperatorSet.SegmentContoursXld(ho_Edges1, out ho_ContoursSplit1, "lines_circles", 5, 4, 2); ho_UnionContours.Dispose(); HOperatorSet.UnionCollinearContoursXld(ho_ContoursSplit1, out ho_UnionContours, 10000, 10000, 2, 0.1, "attr_keep"); HOperatorSet.FitLineContourXld(ho_UnionContours, "tukey", -1, 0, 5, 2, out hv_RowBegin1, out hv_ColBegin1, out hv_RowEnd1, out hv_ColEnd1, out hv_Nr1, out hv_Nc1, out hv_Dist1); hv_dis1 = ((((hv_RowBegin1 - hv_RowEnd1) * (hv_RowBegin1 - hv_RowEnd1)) + ((hv_ColBegin1 - hv_ColEnd1) * (hv_ColBegin1 - hv_ColEnd1)))).TupleSqrt() ; HOperatorSet.TupleFind(hv_dis1, hv_dis1.TupleMax(), out hv_Indices1); ho_RegionLines1.Dispose(); HOperatorSet.GenRegionLine(out ho_RegionLines1, hv_RowBegin1.TupleSelect(hv_Indices1), hv_ColBegin1.TupleSelect(hv_Indices1), hv_RowEnd1.TupleSelect(hv_Indices1), hv_ColEnd1.TupleSelect(hv_Indices1)); //distance_cc_min (UnionContours, UnionContours, 'fast_point_to_segment', DistanceMin) //distance_pl ((RowBegin2[Indices2]+RowEnd2[Indices2])/2, (ColBegin2[Indices2]+ColEnd2[Indices2]), RowBegin1[Indices1], ColBegin1[Indices1], RowEnd1[Indices1], ColEnd1[Indices1], Distance) hv_R1 = new HTuple(); hv_R1 = hv_R1.TupleConcat(hv_RowBegin1.TupleSelect( hv_Indices1)); hv_R1 = hv_R1.TupleConcat(hv_RowEnd1.TupleSelect(hv_Indices1)); hv_C1 = new HTuple(); hv_C1 = hv_C1.TupleConcat(hv_ColBegin1.TupleSelect( hv_Indices1)); hv_C1 = hv_C1.TupleConcat(hv_ColEnd1.TupleSelect(hv_Indices1)); ho_Contour2.Dispose(); HOperatorSet.GenContourPolygonXld(out ho_Contour2, hv_R1, hv_C1); HOperatorSet.DistanceCcMin(ho_Contour, ho_Contour2, "fast_point_to_segment", out hv_DistanceMin1); { HObject ExpTmpOutVar_0; HOperatorSet.Union2(ho_Region, ho_RegionLines1, out ExpTmpOutVar_0); ho_Region.Dispose(); ho_Region = ExpTmpOutVar_0; } { HObject ExpTmpOutVar_0; HOperatorSet.Union2(ho_Region, ho_RegionLines2, out ExpTmpOutVar_0); ho_Region.Dispose(); ho_Region = ExpTmpOutVar_0; } HOperatorSet.Union1(ho_Region, out RegionToDisp); HTuple hv_result = GetHv_result(); hv_result = hv_result.TupleConcat("高度"); hv_result = hv_result.TupleConcat(hv_DistanceMin1.D * pixeldist); result = hv_result.Clone(); ho_Rectangle.Dispose(); ho_Rectangle1.Dispose(); ho_Region.Dispose(); ho_ContoursL.Dispose(); ho_Contour1.Dispose(); ho_ContoursSplit2.Dispose(); ho_UnionContours1.Dispose(); ho_RegionLines2.Dispose(); ho_Contour.Dispose(); ho_ImageReduced1.Dispose(); ho_Edges1.Dispose(); ho_ContoursSplit1.Dispose(); ho_UnionContours.Dispose(); ho_RegionLines1.Dispose(); ho_Contour2.Dispose(); algorithm.Region.Dispose(); } catch { HTuple hv_result = GetHv_result(); hv_result = hv_result.TupleConcat("高度"); hv_result = hv_result.TupleConcat(0); result = hv_result.Clone(); ho_Rectangle.Dispose(); ho_Rectangle1.Dispose(); ho_Region.Dispose(); ho_ContoursL.Dispose(); ho_Contour1.Dispose(); ho_ContoursSplit2.Dispose(); ho_UnionContours1.Dispose(); ho_RegionLines2.Dispose(); ho_Contour.Dispose(); ho_ImageReduced1.Dispose(); ho_Edges1.Dispose(); ho_ContoursSplit1.Dispose(); ho_UnionContours.Dispose(); ho_RegionLines1.Dispose(); ho_Contour2.Dispose(); algorithm.Region.Dispose(); } finally { ho_Rectangle.Dispose(); ho_Rectangle1.Dispose(); ho_Region.Dispose(); ho_ContoursL.Dispose(); ho_Contour1.Dispose(); ho_ContoursSplit2.Dispose(); ho_UnionContours1.Dispose(); ho_RegionLines2.Dispose(); ho_Contour.Dispose(); ho_ImageReduced1.Dispose(); ho_Edges1.Dispose(); ho_ContoursSplit1.Dispose(); ho_UnionContours.Dispose(); ho_RegionLines1.Dispose(); ho_Contour2.Dispose(); algorithm.Region.Dispose(); } }
private void action() { // Stack for temporary objects HObject[] OTemp = new HObject[20]; // Local iconic variables HObject ho_Rectangle, ho_ImageReduced; HObject ho_Region, ho_RegionLines, ho_RegionUnion1, ho_RegionUnion2; HObject ho_RegionUnion3, ho_Region1, ho_ConnectedRegions; HObject ho_RegionFillUp, ho_SelectedRegions, ho_SortedRegions; HObject ho_ObjectSelected = null, ho_Rectangle1 = null, ho_ImageReduced1 = null; HObject ho_Circle1 = null, ho_Circle2 = null, ho_Circle3 = null; HObject ho_Circle4 = null, ho_RegionUnionc1 = null, ho_RegionUnionc2 = null; HObject ho_RegionUnionc3 = null, ho_ConnectedRegionsc = null; HObject ho_SortedRegions1 = null, ho_ContoursL = null, ho_RegionLinesL = null; HObject ho_RegionLinesL1 = null, ho_RegionUnionL = null, ho_ConnectedRegionsL = null; HObject ho_SelectedRegionsL = null, ho_RegionFillUpL = null; HObject ho_ContoursSplitL = null, ho_SelectedContoursL = null; HObject ho_UnionContoursL = null, ho_RegionL = null, ho_RegionDifferenceL = null; HObject ho_ContoursR = null, ho_RegionLinesR = null, ho_RegionLinesR1 = null; HObject ho_RegionUnionR = null, ho_ConnectedRegionsR = null; HObject ho_SelectedRegionsR = null, ho_RegionFillUpR = null; HObject ho_ContoursSplitR = null, ho_SelectedContoursR = null; HObject ho_UnionContoursR = null, ho_RegionR = null, ho_RegionDifferenceR = null; HObject ho_ConnectedRegions1; // Local control variables HTuple hv_Width = null, hv_Height = null, hv_Row11 = null; HTuple hv_Column11 = null, hv_Row21 = null, hv_Column21 = null; HTuple hv_Row12 = null, hv_Column12 = null, hv_Row22 = null; HTuple hv_Column22 = null, hv_Number = null, hv_h = null; HTuple hv_w = null, hv_i = null, hv_Row = new HTuple(); HTuple hv_Column = new HTuple(), hv_Phi = new HTuple(); HTuple hv_Length1 = new HTuple(), hv_Length2 = new HTuple(); HTuple hv_Cos = new HTuple(), hv_Sin = new HTuple(), hv_RT_X = new HTuple(); HTuple hv_RT_Y = new HTuple(), hv_AX = new HTuple(), hv_AY = new HTuple(); HTuple hv_RB_X = new HTuple(), hv_RB_Y = new HTuple(); HTuple hv_BX = new HTuple(), hv_BY = new HTuple(), hv_LB_X = new HTuple(); HTuple hv_LB_Y = new HTuple(), hv_CX = new HTuple(), hv_CY = new HTuple(); HTuple hv_LT_X = new HTuple(), hv_LT_Y = new HTuple(); HTuple hv_DX = new HTuple(), hv_DY = new HTuple(), hv_Number1 = new HTuple(); HTuple hv_Area = new HTuple(), hv_Row1 = new HTuple(); HTuple hv_Column1 = new HTuple(), hv_Distance = new HTuple(); HTuple hv_Distance1 = new HTuple(), hv_shapeParam = new HTuple(); HTuple hv_MetrologyHandleL = new HTuple(), hv_Index = new HTuple(); HTuple hv_RowL = new HTuple(), hv_ColumnL = new HTuple(); HTuple hv_l = new HTuple(), hv_NumberL = new HTuple(); HTuple hv_MetrologyHandleR = new HTuple(), hv_RowR = new HTuple(); HTuple hv_ColumnR = new HTuple(), hv_r = new HTuple(); HTuple hv_NumberR = new HTuple(), hv_AreaL = new HTuple(); HTuple hv_AreaR = new HTuple(), hv_Area1 = null, hv_Row2 = null; HTuple hv_Column2 = null, hv_Area2 = null, hv_Row3 = null; HTuple hv_Column3 = null, hv_hm = null, hv_wm = null, hv_Row13 = null; HTuple hv_Column13 = null, hv_Row23 = null, hv_Column23 = null; HTuple hv_zh = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_RegionLines); HOperatorSet.GenEmptyObj(out ho_RegionUnion1); HOperatorSet.GenEmptyObj(out ho_RegionUnion2); HOperatorSet.GenEmptyObj(out ho_RegionUnion3); HOperatorSet.GenEmptyObj(out ho_Region1); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_RegionFillUp); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_SortedRegions); HOperatorSet.GenEmptyObj(out ho_ObjectSelected); HOperatorSet.GenEmptyObj(out ho_Rectangle1); HOperatorSet.GenEmptyObj(out ho_ImageReduced1); HOperatorSet.GenEmptyObj(out ho_Circle1); HOperatorSet.GenEmptyObj(out ho_Circle2); HOperatorSet.GenEmptyObj(out ho_Circle3); HOperatorSet.GenEmptyObj(out ho_Circle4); HOperatorSet.GenEmptyObj(out ho_RegionUnionc1); HOperatorSet.GenEmptyObj(out ho_RegionUnionc2); HOperatorSet.GenEmptyObj(out ho_RegionUnionc3); HOperatorSet.GenEmptyObj(out ho_ConnectedRegionsc); HOperatorSet.GenEmptyObj(out ho_SortedRegions1); HOperatorSet.GenEmptyObj(out ho_ContoursL); HOperatorSet.GenEmptyObj(out ho_RegionLinesL); HOperatorSet.GenEmptyObj(out ho_RegionLinesL1); HOperatorSet.GenEmptyObj(out ho_RegionUnionL); HOperatorSet.GenEmptyObj(out ho_ConnectedRegionsL); HOperatorSet.GenEmptyObj(out ho_SelectedRegionsL); HOperatorSet.GenEmptyObj(out ho_RegionFillUpL); HOperatorSet.GenEmptyObj(out ho_ContoursSplitL); HOperatorSet.GenEmptyObj(out ho_SelectedContoursL); HOperatorSet.GenEmptyObj(out ho_UnionContoursL); HOperatorSet.GenEmptyObj(out ho_RegionL); HOperatorSet.GenEmptyObj(out ho_RegionDifferenceL); HOperatorSet.GenEmptyObj(out ho_ContoursR); HOperatorSet.GenEmptyObj(out ho_RegionLinesR); HOperatorSet.GenEmptyObj(out ho_RegionLinesR1); HOperatorSet.GenEmptyObj(out ho_RegionUnionR); HOperatorSet.GenEmptyObj(out ho_ConnectedRegionsR); HOperatorSet.GenEmptyObj(out ho_SelectedRegionsR); HOperatorSet.GenEmptyObj(out ho_RegionFillUpR); HOperatorSet.GenEmptyObj(out ho_ContoursSplitR); HOperatorSet.GenEmptyObj(out ho_SelectedContoursR); HOperatorSet.GenEmptyObj(out ho_UnionContoursR); HOperatorSet.GenEmptyObj(out ho_RegionR); HOperatorSet.GenEmptyObj(out ho_RegionDifferenceR); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1); HOperatorSet.Union1(algorithm.Region, out RegionToDisp); try { HOperatorSet.GenRectangle1(out ho_Rectangle, DRow1m, DCol1m, DRow2m, DCol2m); ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(Image, ho_Rectangle, out ho_ImageReduced); ho_Region.Dispose(); HOperatorSet.GetImageSize(Image, out hv_Width, out hv_Height); HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, 128, 255); HOperatorSet.SmallestRectangle1(ho_Region, out hv_Row12, out hv_Column12, out hv_Row22, out hv_Column22); ho_RegionLines.Dispose(); HOperatorSet.GenRegionLine(out ho_RegionLines, (hv_Row12 + hv_Row22) / 2, hv_Column12, ((hv_Row12 + hv_Row22) / 2) + 1, hv_Column12); ho_RegionUnion1.Dispose(); HOperatorSet.Union1(ho_RegionLines, out ho_RegionUnion1); ho_RegionUnion2.Dispose(); HOperatorSet.Union1(ho_RegionLines, out ho_RegionUnion2); ho_RegionUnion3.Dispose(); HOperatorSet.Union1(ho_RegionLines, out ho_RegionUnion3); ho_Region1.Dispose(); HOperatorSet.Threshold(ho_ImageReduced, out ho_Region1, 128, 255); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_Region1, out ho_ConnectedRegions); ho_RegionFillUp.Dispose(); HOperatorSet.FillUp(ho_ConnectedRegions, out ho_RegionFillUp); ho_SelectedRegions.Dispose(); HOperatorSet.SelectShape(ho_RegionFillUp, out ho_SelectedRegions, "area", "and", 500, 99999000); ho_SortedRegions.Dispose(); HOperatorSet.SortRegion(ho_SelectedRegions, out ho_SortedRegions, "upper_left", "true", "column"); HOperatorSet.CountObj(ho_SortedRegions, out hv_Number); // stop(); only in hdevelop hv_h = new HTuple(); hv_w = new HTuple(); HTuple end_val36 = hv_Number - 1; HTuple step_val36 = 1; for (hv_i = 2; hv_i.Continue(end_val36, step_val36); hv_i = hv_i.TupleAdd(step_val36)) { ho_ObjectSelected.Dispose(); HOperatorSet.SelectObj(ho_SortedRegions, out ho_ObjectSelected, hv_i); HOperatorSet.SmallestRectangle2(ho_ObjectSelected, out hv_Row, out hv_Column, out hv_Phi, out hv_Length1, out hv_Length2); ho_Rectangle1.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle1, hv_Row, hv_Column, hv_Phi, hv_Length1 * 0.85, hv_Length2); ho_ImageReduced1.Dispose(); HOperatorSet.ReduceDomain(ho_ImageReduced, ho_Rectangle1, out ho_ImageReduced1 ); //*矩形4个角点 HOperatorSet.TupleCos(hv_Phi, out hv_Cos); HOperatorSet.TupleSin(hv_Phi, out hv_Sin); //*dev_set_color('green') hv_RT_X = (((-hv_Length1) * 0.85) * hv_Cos) - (hv_Length2 * hv_Sin); hv_RT_Y = (((-hv_Length1) * 0.85) * hv_Sin) + (hv_Length2 * hv_Cos); ho_Circle1.Dispose(); HOperatorSet.GenCircle(out ho_Circle1, hv_Row - hv_RT_Y, hv_Column + hv_RT_X, 0.5); //*最小矩形的顶点A hv_AX = (((-hv_Length1) * 0.85) * hv_Cos) - (hv_Length2 * hv_Sin); hv_AY = (((-hv_Length1) * 0.85) * hv_Sin) + (hv_Length2 * hv_Cos); //*dev_set_color('red') hv_RB_X = ((hv_Length1 * 0.85) * hv_Cos) - (hv_Length2 * hv_Sin); hv_RB_Y = ((hv_Length1 * 0.85) * hv_Sin) + (hv_Length2 * hv_Cos); ho_Circle2.Dispose(); HOperatorSet.GenCircle(out ho_Circle2, hv_Row - hv_RB_Y, hv_Column + hv_RB_X, 0.5); //*最小矩形的顶点B hv_BX = ((hv_Length1 * 0.85) * hv_Cos) - (hv_Length2 * hv_Sin); hv_BY = ((hv_Length1 * 0.85) * hv_Sin) + (hv_Length2 * hv_Cos); //*dev_set_color('yellow') hv_LB_X = ((hv_Length1 * 0.9) * hv_Cos) + (hv_Length2 * hv_Sin); hv_LB_Y = ((hv_Length1 * 0.9) * hv_Sin) - (hv_Length2 * hv_Cos); ho_Circle3.Dispose(); HOperatorSet.GenCircle(out ho_Circle3, hv_Row - hv_LB_Y, hv_Column + hv_LB_X, 0.5); //*最小矩形的顶点C hv_CX = ((hv_Length1 * 0.85) * hv_Cos) + (hv_Length2 * hv_Sin); hv_CY = ((hv_Length1 * 0.85) * hv_Sin) - (hv_Length2 * hv_Cos); //*dev_set_color('pink') hv_LT_X = (((-hv_Length1) * 0.85) * hv_Cos) + (hv_Length2 * hv_Sin); hv_LT_Y = (((-hv_Length1) * 0.85) * hv_Sin) - (hv_Length2 * hv_Cos); ho_Circle4.Dispose(); HOperatorSet.GenCircle(out ho_Circle4, hv_Row - hv_LT_Y, hv_Column + hv_LT_X, 0.5); //*最小矩形的顶点D hv_DX = (((-hv_Length1) * 0.85) * hv_Cos) + (hv_Length2 * hv_Sin); hv_DY = (((-hv_Length1) * 0.85) * hv_Sin) - (hv_Length2 * hv_Cos); //stop () ho_RegionUnionc1.Dispose(); HOperatorSet.Union2(ho_Circle1, ho_Circle2, out ho_RegionUnionc1); ho_RegionUnionc2.Dispose(); HOperatorSet.Union2(ho_RegionUnionc1, ho_Circle3, out ho_RegionUnionc2); ho_RegionUnionc3.Dispose(); HOperatorSet.Union2(ho_RegionUnionc2, ho_Circle4, out ho_RegionUnionc3); ho_ConnectedRegionsc.Dispose(); HOperatorSet.Connection(ho_RegionUnionc3, out ho_ConnectedRegionsc); HOperatorSet.CountObj(ho_RegionUnionc3, out hv_Number1); ho_SortedRegions1.Dispose(); HOperatorSet.SortRegion(ho_ConnectedRegionsc, out ho_SortedRegions1, "upper_left", "true", "row"); HOperatorSet.AreaCenter(ho_SortedRegions1, out hv_Area, out hv_Row1, out hv_Column1); HOperatorSet.DistancePp(hv_Row1.TupleSelect(0), hv_Column1.TupleSelect(0), hv_Row1.TupleSelect(1), hv_Column1.TupleSelect(1), out hv_Distance); HOperatorSet.DistancePl(hv_Row1.TupleSelect(0), hv_Column1.TupleSelect(0), hv_Row1.TupleSelect(2), hv_Column1.TupleSelect(2), hv_Row1.TupleSelect( 3), hv_Column1.TupleSelect(3), out hv_Distance1); hv_h = hv_h.TupleConcat((hv_Distance1 * 1) / 0.9); hv_w = hv_w.TupleConcat(hv_Distance); //gen_region_line (RegionLines2, (Row1[0]+Row1[1])/2, (Column1[0]+Column1[1])/2, (Row1[2]+Row1[3])/2, (Column1[2]+Column1[3])/2) hv_shapeParam = new HTuple(); hv_shapeParam = hv_shapeParam.TupleConcat(((hv_Row1.TupleSelect( 0)) + (hv_Row1.TupleSelect(1))) / 2); hv_shapeParam = hv_shapeParam.TupleConcat(((hv_Column1.TupleSelect( 0)) + (hv_Column1.TupleSelect(1))) / 2); hv_shapeParam = hv_shapeParam.TupleConcat(((hv_Row1.TupleSelect( 2)) + (hv_Row1.TupleSelect(3))) / 2); hv_shapeParam = hv_shapeParam.TupleConcat(((hv_Column1.TupleSelect( 2)) + (hv_Column1.TupleSelect(3))) / 2); //shapeParam := [Row12,(Column22+Column12)/2,Row22,(Column22+Column12)/2] //找左侧点 HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandleL); HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandleL, hv_Width, hv_Height); HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandleL, "line", hv_shapeParam, hv_Distance / 2, 1, 1, 60, new HTuple(), new HTuple(), out hv_Index); //设置参数 HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "measure_transition", "negative"); HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "num_measures", 100); HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "num_instances", 1); HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "measure_select", "last"); //开始找边缘 HOperatorSet.ApplyMetrologyModel(ho_ImageReduced1, hv_MetrologyHandleL); ho_ContoursL.Dispose(); HOperatorSet.GetMetrologyObjectMeasures(out ho_ContoursL, hv_MetrologyHandleL, "all", "all", out hv_RowL, out hv_ColumnL); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red"); } HOperatorSet.DispCross(3600, hv_RowL, hv_ColumnL, 6, hv_Phi); //左侧点,用一Row,只取最左边点 //gen_cross_contour_xld (CrossL, RowL, ColumnL, 6, 0) ///释放测量句柄 HOperatorSet.ClearMetrologyModel(hv_MetrologyHandleL); //gen_region_line (RegionLines, Row11, Column11, Row11+1, Column11) //union1 (RegionLines, RegionUnion1) for (hv_l = 0; (int)hv_l <= (int)((new HTuple(hv_RowL.TupleLength())) - 2); hv_l = (int)hv_l + 1) { ho_RegionLinesL.Dispose(); HOperatorSet.GenRegionLine(out ho_RegionLinesL, hv_RowL.TupleSelect(hv_l), hv_ColumnL.TupleSelect(hv_l), hv_RowL.TupleSelect(hv_l + 1), hv_ColumnL.TupleSelect( hv_l + 1)); { HObject ExpTmpOutVar_0; HOperatorSet.Union2(ho_RegionUnion1, ho_RegionLinesL, out ExpTmpOutVar_0 ); ho_RegionUnion1.Dispose(); ho_RegionUnion1 = ExpTmpOutVar_0; } } if (HDevWindowStack.IsOpen()) { //dev_set_color ('blue') } //gen_contour_polygon_xld (Contour, RowL, ColumnL) //stop () //disp_obj (Image, 3600) ho_RegionLinesL1.Dispose(); HOperatorSet.GenRegionLine(out ho_RegionLinesL1, hv_RowL.TupleSelect(0), hv_ColumnL.TupleSelect(0), hv_RowL.TupleSelect((new HTuple(hv_RowL.TupleLength() )) - 1), hv_ColumnL.TupleSelect((new HTuple(hv_RowL.TupleLength())) - 1)); ho_RegionUnionL.Dispose(); HOperatorSet.Union2(ho_RegionUnion1, ho_RegionLinesL1, out ho_RegionUnionL ); ho_ConnectedRegionsL.Dispose(); HOperatorSet.Connection(ho_RegionUnionL, out ho_ConnectedRegionsL); ho_SelectedRegionsL.Dispose(); HOperatorSet.SelectShapeStd(ho_ConnectedRegionsL, out ho_SelectedRegionsL, "max_area", 70); ho_RegionFillUpL.Dispose(); HOperatorSet.FillUp(ho_SelectedRegionsL, out ho_RegionFillUpL); ho_ContoursL.Dispose(); HOperatorSet.GenContourRegionXld(ho_RegionFillUpL, out ho_ContoursL, "border"); ho_ContoursSplitL.Dispose(); HOperatorSet.SegmentContoursXld(ho_ContoursL, out ho_ContoursSplitL, "lines_circles", 5, 8, 3.5); ho_SelectedContoursL.Dispose(); HOperatorSet.SelectContoursXld(ho_ContoursSplitL, out ho_SelectedContoursL, "contour_length", 70, 2000, -0.5, 0.5); //*** ho_UnionContoursL.Dispose(); HOperatorSet.UnionAdjacentContoursXld(ho_SelectedContoursL, out ho_UnionContoursL, 10, 1, "attr_keep"); ho_RegionL.Dispose(); HOperatorSet.GenRegionContourXld(ho_UnionContoursL, out ho_RegionL, "filled"); ho_RegionDifferenceL.Dispose(); HOperatorSet.Difference(ho_RegionFillUpL, ho_RegionL, out ho_RegionDifferenceL ); HOperatorSet.CountObj(ho_RegionDifferenceL, out hv_NumberL); //* //找右侧点 HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandleR); HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandleR, hv_Width, hv_Height); HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandleR, "line", hv_shapeParam, hv_Distance / 2, 1, 1, 60, new HTuple(), new HTuple(), out hv_Index); //设置参数 HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleR, "all", "measure_transition", "positive"); HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleR, "all", "num_measures", 100); HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleR, "all", "num_instances", 1); HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleR, "all", "measure_select", "first"); //开始找边缘 HOperatorSet.ApplyMetrologyModel(ho_ImageReduced1, hv_MetrologyHandleR); ho_ContoursR.Dispose(); HOperatorSet.GetMetrologyObjectMeasures(out ho_ContoursR, hv_MetrologyHandleR, "all", "all", out hv_RowR, out hv_ColumnR); //disp_obj (Image, 3600) if (HDevWindowStack.IsOpen()) { //dev_set_color ('red') } //disp_cross (3600, RowR, ColumnR, 6, Phi) //*右侧点,用一Row,只取最右边点 //gen_cross_contour_xld (CrossR, RowR, ColumnR, 6, 0) ///释放测量句柄 HOperatorSet.ClearMetrologyModel(hv_MetrologyHandleR); //close_measure (MeasureHandle1) //stop () //gen_region_line (RegionLines, Row11, Column11, Row11+1, Column11) //union1 (RegionLines, RegionUnion1) for (hv_r = 0; (int)hv_r <= (int)((new HTuple(hv_RowR.TupleLength())) - 2); hv_r = (int)hv_r + 1) { ho_RegionLinesR.Dispose(); HOperatorSet.GenRegionLine(out ho_RegionLinesR, hv_RowR.TupleSelect(hv_r), hv_ColumnR.TupleSelect(hv_r), hv_RowR.TupleSelect(hv_r + 1), hv_ColumnR.TupleSelect( hv_r + 1)); { HObject ExpTmpOutVar_0; HOperatorSet.Union2(ho_RegionUnion2, ho_RegionLinesR, out ExpTmpOutVar_0 ); ho_RegionUnion2.Dispose(); ho_RegionUnion2 = ExpTmpOutVar_0; } } ho_RegionLinesR1.Dispose(); HOperatorSet.GenRegionLine(out ho_RegionLinesR1, hv_RowR.TupleSelect(0), hv_ColumnR.TupleSelect(0), hv_RowR.TupleSelect((new HTuple(hv_RowR.TupleLength() )) - 1), hv_ColumnR.TupleSelect((new HTuple(hv_RowR.TupleLength())) - 1)); ho_RegionUnionR.Dispose(); HOperatorSet.Union2(ho_RegionUnion2, ho_RegionLinesR1, out ho_RegionUnionR ); ho_ConnectedRegionsR.Dispose(); HOperatorSet.Connection(ho_RegionUnionR, out ho_ConnectedRegionsR); ho_SelectedRegionsR.Dispose(); HOperatorSet.SelectShapeStd(ho_ConnectedRegionsR, out ho_SelectedRegionsR, "max_area", 70); ho_RegionFillUpR.Dispose(); HOperatorSet.FillUp(ho_SelectedRegionsR, out ho_RegionFillUpR); ho_ContoursR.Dispose(); HOperatorSet.GenContourRegionXld(ho_RegionFillUpR, out ho_ContoursR, "border"); ho_ContoursSplitR.Dispose(); HOperatorSet.SegmentContoursXld(ho_ContoursR, out ho_ContoursSplitR, "lines_circles", 5, 8, 3.5); ho_SelectedContoursR.Dispose(); HOperatorSet.SelectContoursXld(ho_ContoursSplitR, out ho_SelectedContoursR, "contour_length", 70, 2000, -0.5, 0.5); ho_UnionContoursR.Dispose(); HOperatorSet.UnionAdjacentContoursXld(ho_SelectedContoursR, out ho_UnionContoursR, 10, 1, "attr_keep"); ho_RegionR.Dispose(); HOperatorSet.GenRegionContourXld(ho_UnionContoursR, out ho_RegionR, "filled"); ho_RegionDifferenceR.Dispose(); HOperatorSet.Difference(ho_RegionFillUpR, ho_RegionR, out ho_RegionDifferenceR ); HOperatorSet.CountObj(ho_RegionDifferenceR, out hv_NumberR); //stop () if ((int)(new HTuple(hv_NumberL.TupleGreater(0))) != 0) { HOperatorSet.AreaCenter(ho_RegionDifferenceL, out hv_AreaL, out hv_RowL, out hv_ColumnL); { HObject ExpTmpOutVar_0; HOperatorSet.Union2(ho_RegionUnion3, ho_RegionDifferenceL, out ExpTmpOutVar_0 ); ho_RegionUnion3.Dispose(); ho_RegionUnion3 = ExpTmpOutVar_0; } } if ((int)(new HTuple(hv_NumberR.TupleGreater(0))) != 0) { HOperatorSet.AreaCenter(ho_RegionDifferenceR, out hv_AreaR, out hv_RowR, out hv_ColumnR); { HObject ExpTmpOutVar_0; HOperatorSet.Union2(ho_RegionUnion3, ho_RegionDifferenceR, out ExpTmpOutVar_0 ); ho_RegionUnion3.Dispose(); ho_RegionUnion3 = ExpTmpOutVar_0; } } //union2 (RegionDifferenceL, RegionDifferenceR, RegionUnionLR) //union2 (RegionUnion3, RegionUnionLR, RegionUnion3) } HOperatorSet.AreaCenter(ho_RegionUnion3, out hv_Area1, out hv_Row2, out hv_Column2); ho_ConnectedRegions1.Dispose(); HOperatorSet.Connection(ho_RegionUnion3, out ho_ConnectedRegions1); HOperatorSet.AreaCenter(ho_ConnectedRegions1, out hv_Area2, out hv_Row3, out hv_Column3); hv_hm = (hv_h.TupleMin()) / (hv_h.TupleMean()); hv_wm = (hv_w.TupleMax()) / (hv_w.TupleMean()); ho_ObjectSelected.Dispose(); HOperatorSet.SelectObj(ho_SortedRegions, out ho_ObjectSelected, hv_Number); HOperatorSet.SmallestRectangle1(ho_ObjectSelected, out hv_Row13, out hv_Column13, out hv_Row23, out hv_Column23); hv_zh = hv_Column23 - hv_Column13; HOperatorSet.Union1(ho_ConnectedRegions1, out RegionToDisp); HTuple hv_result = GetHv_result(); hv_result = hv_result.TupleConcat("最短比值"); hv_result = hv_result.TupleConcat(hv_hm.D); hv_result = hv_result.TupleConcat("最宽比值"); hv_result = hv_result.TupleConcat(hv_wm.D); hv_result = hv_result.TupleConcat("缺陷总面积"); hv_result = hv_result.TupleConcat(hv_Area1.D); hv_result = hv_result.TupleConcat("最大缺陷面积"); hv_result = hv_result.TupleConcat(hv_Area2.D); hv_result = hv_result.TupleConcat("宽度"); hv_result = hv_result.TupleConcat(hv_zh.D); result = hv_result.Clone(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Region.Dispose(); ho_RegionLines.Dispose(); ho_RegionUnion1.Dispose(); ho_RegionUnion2.Dispose(); ho_RegionUnion3.Dispose(); ho_Region1.Dispose(); ho_ConnectedRegions.Dispose(); ho_RegionFillUp.Dispose(); ho_SelectedRegions.Dispose(); ho_SortedRegions.Dispose(); ho_ObjectSelected.Dispose(); ho_Rectangle1.Dispose(); ho_ImageReduced1.Dispose(); ho_Circle1.Dispose(); ho_Circle2.Dispose(); ho_Circle3.Dispose(); ho_Circle4.Dispose(); ho_RegionUnionc1.Dispose(); ho_RegionUnionc2.Dispose(); ho_RegionUnionc3.Dispose(); ho_ConnectedRegionsc.Dispose(); ho_SortedRegions1.Dispose(); ho_ContoursL.Dispose(); ho_RegionLinesL.Dispose(); ho_RegionLinesL1.Dispose(); ho_RegionUnionL.Dispose(); ho_ConnectedRegionsL.Dispose(); ho_SelectedRegionsL.Dispose(); ho_RegionFillUpL.Dispose(); ho_ContoursSplitL.Dispose(); ho_SelectedContoursL.Dispose(); ho_UnionContoursL.Dispose(); ho_RegionL.Dispose(); ho_RegionDifferenceL.Dispose(); ho_ContoursR.Dispose(); ho_RegionLinesR.Dispose(); ho_RegionLinesR1.Dispose(); ho_RegionUnionR.Dispose(); ho_ConnectedRegionsR.Dispose(); ho_SelectedRegionsR.Dispose(); ho_RegionFillUpR.Dispose(); ho_ContoursSplitR.Dispose(); ho_SelectedContoursR.Dispose(); ho_UnionContoursR.Dispose(); ho_RegionR.Dispose(); ho_RegionDifferenceR.Dispose(); ho_ConnectedRegions1.Dispose(); algorithm.Region.Dispose(); } catch { HTuple hv_result = GetHv_result(); hv_result = hv_result.TupleConcat("最短比值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("最宽比值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("缺陷总面积"); hv_result = hv_result.TupleConcat(999999); hv_result = hv_result.TupleConcat("最大缺陷面积"); hv_result = hv_result.TupleConcat(999999); hv_result = hv_result.TupleConcat("宽度"); hv_result = hv_result.TupleConcat(999999); result = hv_result.Clone(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Region.Dispose(); ho_RegionLines.Dispose(); ho_RegionUnion1.Dispose(); ho_RegionUnion2.Dispose(); ho_RegionUnion3.Dispose(); ho_Region1.Dispose(); ho_ConnectedRegions.Dispose(); ho_RegionFillUp.Dispose(); ho_SelectedRegions.Dispose(); ho_SortedRegions.Dispose(); ho_ObjectSelected.Dispose(); ho_Rectangle1.Dispose(); ho_ImageReduced1.Dispose(); ho_Circle1.Dispose(); ho_Circle2.Dispose(); ho_Circle3.Dispose(); ho_Circle4.Dispose(); ho_RegionUnionc1.Dispose(); ho_RegionUnionc2.Dispose(); ho_RegionUnionc3.Dispose(); ho_ConnectedRegionsc.Dispose(); ho_SortedRegions1.Dispose(); ho_ContoursL.Dispose(); ho_RegionLinesL.Dispose(); ho_RegionLinesL1.Dispose(); ho_RegionUnionL.Dispose(); ho_ConnectedRegionsL.Dispose(); ho_SelectedRegionsL.Dispose(); ho_RegionFillUpL.Dispose(); ho_ContoursSplitL.Dispose(); ho_SelectedContoursL.Dispose(); ho_UnionContoursL.Dispose(); ho_RegionL.Dispose(); ho_RegionDifferenceL.Dispose(); ho_ContoursR.Dispose(); ho_RegionLinesR.Dispose(); ho_RegionLinesR1.Dispose(); ho_RegionUnionR.Dispose(); ho_ConnectedRegionsR.Dispose(); ho_SelectedRegionsR.Dispose(); ho_RegionFillUpR.Dispose(); ho_ContoursSplitR.Dispose(); ho_SelectedContoursR.Dispose(); ho_UnionContoursR.Dispose(); ho_RegionR.Dispose(); ho_RegionDifferenceR.Dispose(); ho_ConnectedRegions1.Dispose(); algorithm.Region.Dispose(); } finally { ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Region.Dispose(); ho_RegionLines.Dispose(); ho_RegionUnion1.Dispose(); ho_RegionUnion2.Dispose(); ho_RegionUnion3.Dispose(); ho_Region1.Dispose(); ho_ConnectedRegions.Dispose(); ho_RegionFillUp.Dispose(); ho_SelectedRegions.Dispose(); ho_SortedRegions.Dispose(); ho_ObjectSelected.Dispose(); ho_Rectangle1.Dispose(); ho_ImageReduced1.Dispose(); ho_Circle1.Dispose(); ho_Circle2.Dispose(); ho_Circle3.Dispose(); ho_Circle4.Dispose(); ho_RegionUnionc1.Dispose(); ho_RegionUnionc2.Dispose(); ho_RegionUnionc3.Dispose(); ho_ConnectedRegionsc.Dispose(); ho_SortedRegions1.Dispose(); ho_ContoursL.Dispose(); ho_RegionLinesL.Dispose(); ho_RegionLinesL1.Dispose(); ho_RegionUnionL.Dispose(); ho_ConnectedRegionsL.Dispose(); ho_SelectedRegionsL.Dispose(); ho_RegionFillUpL.Dispose(); ho_ContoursSplitL.Dispose(); ho_SelectedContoursL.Dispose(); ho_UnionContoursL.Dispose(); ho_RegionL.Dispose(); ho_RegionDifferenceL.Dispose(); ho_ContoursR.Dispose(); ho_RegionLinesR.Dispose(); ho_RegionLinesR1.Dispose(); ho_RegionUnionR.Dispose(); ho_ConnectedRegionsR.Dispose(); ho_SelectedRegionsR.Dispose(); ho_RegionFillUpR.Dispose(); ho_ContoursSplitR.Dispose(); ho_SelectedContoursR.Dispose(); ho_UnionContoursR.Dispose(); ho_RegionR.Dispose(); ho_RegionDifferenceR.Dispose(); ho_ConnectedRegions1.Dispose(); algorithm.Region.Dispose(); } }
// Main procedure private void action() { // Local iconic variables HObject ho_Image, ho_Rectangle, ho_ImageReduced; HObject ho_ModelContours, ho_ShowContours, ho_ModelContour; HObject ho_MeasureContour, ho_ResultContours = null, ho_Contour = null; HObject ho_UsedEdges = null, ho_Cross = null; // Local control variables HTuple hv_ImageFiles = null, hv_Width = null; HTuple hv_Height = null, hv_WindowHandle = new HTuple(); HTuple hv_Area = null, hv_RowRefer = null, hv_ColRefer = null; HTuple hv_ModelID = null, hv_HomMat2D = null, hv_MetrologyHandle = null; HTuple hv_Line = null, hv_LineIndices = null, hv_Row = null; HTuple hv_Column = null, hv_i = null, hv_RowFound = new HTuple(); HTuple hv_ColFound = new HTuple(), hv_AngleFound = new HTuple(); HTuple hv_ScoreFound = new HTuple(), hv_UsedRow = new HTuple(); HTuple hv_UsedColumn = new HTuple(), hv_Angle = new HTuple(); HTuple hv_Degree = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_ModelContours); HOperatorSet.GenEmptyObj(out ho_ShowContours); HOperatorSet.GenEmptyObj(out ho_ModelContour); HOperatorSet.GenEmptyObj(out ho_MeasureContour); HOperatorSet.GenEmptyObj(out ho_ResultContours); HOperatorSet.GenEmptyObj(out ho_Contour); HOperatorSet.GenEmptyObj(out ho_UsedEdges); HOperatorSet.GenEmptyObj(out ho_Cross); //dev_close_window(...); //**读取图片所在路径** list_image_files("Image", "default", new HTuple(), out hv_ImageFiles); ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, hv_ImageFiles.TupleSelect(0)); HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); //dev_open_window(...); HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "margin"); HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 2); HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle); //创建模板 ho_Rectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle, 400, 570, 550, 700); ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_Rectangle, out ho_ImageReduced); HOperatorSet.AreaCenter(ho_ImageReduced, out hv_Area, out hv_RowRefer, out hv_ColRefer); HOperatorSet.CreateShapeModel(ho_ImageReduced, "auto", (new HTuple(-10)).TupleRad() , (new HTuple(20)).TupleRad(), "auto", "auto", "use_polarity", "auto", "auto", out hv_ModelID); ho_ModelContours.Dispose(); HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelID, 1); HOperatorSet.VectorAngleToRigid(0, 0, 0, hv_RowRefer, hv_ColRefer, 0, out hv_HomMat2D); ho_ShowContours.Dispose(); HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_ShowContours, hv_HomMat2D); HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle); HOperatorSet.DispObj(ho_ShowContours, hv_ExpDefaultWinHandle); //创建测量模板 HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle); HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle, hv_Width, hv_Height); //从左向右在矩形上边缘画一条直线* hv_Line = new HTuple(); hv_Line[0] = 445; hv_Line[1] = 600; hv_Line[2] = 445; hv_Line[3] = 670; HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle, "line", hv_Line, 25, 5, 1, 30, new HTuple(), new HTuple(), out hv_LineIndices); ho_ModelContour.Dispose(); HOperatorSet.GetMetrologyObjectModelContour(out ho_ModelContour, hv_MetrologyHandle, "all", 1.5); ho_MeasureContour.Dispose(); HOperatorSet.GetMetrologyObjectMeasures(out ho_MeasureContour, hv_MetrologyHandle, "all", "all", out hv_Row, out hv_Column); //把测量的位置和模板的位置关联起来 HOperatorSet.SetMetrologyModelParam(hv_MetrologyHandle, "reference_system", ((hv_RowRefer.TupleConcat( hv_ColRefer))).TupleConcat(0)); HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_LineIndices, "measure_transition", "positive"); HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_LineIndices, "measure_select", "first"); HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_LineIndices, "measure_length1", 25); HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_LineIndices, "measure_length2", 5); HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_LineIndices, "measure_threshold", 15); HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_LineIndices, "min_score", 0.3); for (hv_i = 0; (int)hv_i <= (int)((new HTuple(hv_ImageFiles.TupleLength())) - 1); hv_i = (int)hv_i + 1) { ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, hv_ImageFiles.TupleSelect(hv_i)); HOperatorSet.FindShapeModel(ho_Image, hv_ModelID, (new HTuple(-10)).TupleRad() , (new HTuple(20)).TupleRad(), 0.3, 1, 0.5, "least_squares", 0, 0.75, out hv_RowFound, out hv_ColFound, out hv_AngleFound, out hv_ScoreFound); if ((int)(new HTuple((new HTuple(1)).TupleEqual(new HTuple(hv_RowFound.TupleLength() )))) != 0) { HOperatorSet.HomMat2dIdentity(out hv_HomMat2D); HOperatorSet.HomMat2dRotate(hv_HomMat2D, hv_AngleFound, 0, 0, out hv_HomMat2D); HOperatorSet.HomMat2dTranslate(hv_HomMat2D, hv_RowFound - 0, hv_ColFound - 0, out hv_HomMat2D); ho_ResultContours.Dispose(); HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_ResultContours, hv_HomMat2D); HOperatorSet.AlignMetrologyModel(hv_MetrologyHandle, hv_RowFound, hv_ColFound, hv_AngleFound); //应用测量 HOperatorSet.ApplyMetrologyModel(ho_Image, hv_MetrologyHandle); //获取结果 ho_Contour.Dispose(); HOperatorSet.GetMetrologyObjectMeasures(out ho_Contour, hv_MetrologyHandle, "all", "all", out hv_Row, out hv_Column); HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "used_edges", "row", out hv_UsedRow); HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "used_edges", "column", out hv_UsedColumn); ho_UsedEdges.Dispose(); HOperatorSet.GenCrossContourXld(out ho_UsedEdges, hv_UsedRow, hv_UsedColumn, 10, (new HTuple(45)).TupleRad()); ho_ResultContours.Dispose(); HOperatorSet.GetMetrologyObjectResultContour(out ho_ResultContours, hv_MetrologyHandle, "all", "all", 1.5); ho_Cross.Dispose(); HOperatorSet.GenCrossContourXld(out ho_Cross, hv_RowFound, hv_ColFound, 40, hv_AngleFound); HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle); HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green"); HOperatorSet.DispObj(ho_Cross, hv_ExpDefaultWinHandle); HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green"); HOperatorSet.DispObj(ho_ResultContours, hv_ExpDefaultWinHandle); HOperatorSet.AngleLx(hv_UsedRow.TupleSelect(1), hv_UsedColumn.TupleSelect( 1), hv_UsedRow.TupleSelect(6), hv_UsedColumn.TupleSelect(6), out hv_Angle); HOperatorSet.TupleDeg(hv_Angle, out hv_Degree); disp_message(hv_ExpDefaultWinHandle, "定位成功!", "window", 12, 12, "black", "true"); disp_message(hv_ExpDefaultWinHandle, ((((new HTuple("定位区域中心坐标") + "(") + hv_RowFound) + new HTuple(",")) + hv_ColFound) + ")", "window", 32, 12, "black", "true"); HOperatorSet.TupleDeg(hv_AngleFound, out hv_Degree); disp_message(hv_ExpDefaultWinHandle, ("相对模板角度为" + hv_Degree) + "°", "window", 52, 12, "black", "true"); } else { disp_message(hv_ExpDefaultWinHandle, "没有找到模板", "window", hv_Row, hv_Column, "black", "true"); } HDevelopStop(); } HOperatorSet.ClearMetrologyModel(hv_MetrologyHandle); HOperatorSet.ClearShapeModel(hv_ModelID); ho_Image.Dispose(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_ModelContours.Dispose(); ho_ShowContours.Dispose(); ho_ModelContour.Dispose(); ho_MeasureContour.Dispose(); ho_ResultContours.Dispose(); ho_Contour.Dispose(); ho_UsedEdges.Dispose(); ho_Cross.Dispose(); }
// Main procedure private void action() { // Local iconic variables HObject ho_Image, ho_Rectangle, ho_ImageReduced; HObject ho_Region, ho_ResultContour = null, ho_ContCircle = null; HObject ho_Contour = null, ho_Cross = null; // Local control variables HTuple hv_WindowHandle = null, hv_Width = null; HTuple hv_Height = null, hv_MetrologyHandle = null, hv_LineRow1 = null; HTuple hv_LineColumn1 = null, hv_LineRow2 = null, hv_LineColumn2 = null; HTuple hv_Tolerance = null, hv_Index1 = null, hv_Rows = null; HTuple hv_Columns = null, hv_I = null, hv_LineParameter = new HTuple(); HTuple hv_Angle = new HTuple(), hv_Row = new HTuple(); HTuple hv_Column = new HTuple(), hv_IsOverlapping1 = new HTuple(); HTuple hv_Orientation1 = new HTuple(), hv_Orientation2 = new HTuple(); HTuple hv_MRow = new HTuple(), hv_MColumn = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_ResultContour); HOperatorSet.GenEmptyObj(out ho_ContCircle); HOperatorSet.GenEmptyObj(out ho_Contour); HOperatorSet.GenEmptyObj(out ho_Cross); try { //This program shows how to detect the edges of a diamond //with subpixel accuracy and calculate the angle between them. // //In contrast to the example measure_diamond.hdev, //this example uses a metrology model to measure the edges. // //First, the top of the diamond is roughly segmented //to align the metrology objects. //Then, the metrology model is applied and returns the //parameters of the fitted lines. //Finally, the angle between the two lines is computed. // //Display initializations dev_update_off(); if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, "diamond/diamond_01"); dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle); set_display_font(hv_WindowHandle, 16, "mono", "true", "false"); HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); // //Create the metrology model data structure HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle); //The image size is set in advance to speed up the //first call of apply_metrology_model. HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle, hv_Width, hv_Height); //Define the parameters of the metrology line objects hv_LineRow1 = new HTuple(); hv_LineRow1[0] = 155; hv_LineRow1[1] = 155; hv_LineColumn1 = new HTuple(); hv_LineColumn1[0] = 400; hv_LineColumn1[1] = 400; hv_LineRow2 = new HTuple(); hv_LineRow2[0] = 290; hv_LineRow2[1] = 290; hv_LineColumn2 = new HTuple(); hv_LineColumn2[0] = 230; hv_LineColumn2[1] = 570; hv_Tolerance = 20; // //Create two metrology line objects and set parameters HOperatorSet.AddMetrologyObjectLineMeasure(hv_MetrologyHandle, hv_LineRow1, hv_LineColumn1, hv_LineRow2, hv_LineColumn2, hv_Tolerance, 10, 1, 20, new HTuple(), new HTuple(), out hv_Index1); //Create region of interest for the alignment ho_Rectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle, (hv_LineRow1.TupleSelect(0)) - 40, (hv_LineColumn1.TupleSelect(0)) - 50, (hv_LineRow1.TupleSelect(0)) + 20, (hv_LineColumn1.TupleSelect( 0)) + 50); //Change the reference coordinate system in which the //metrology model is given to be situated at the top of the diamond ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_Rectangle, out ho_ImageReduced); ho_Region.Dispose(); HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, 128, 255); HOperatorSet.GetRegionPoints(ho_Region, out hv_Rows, out hv_Columns); HOperatorSet.SetMetrologyModelParam(hv_MetrologyHandle, "reference_system", ((((hv_Rows.TupleSelect(0))).TupleConcat(hv_Columns.TupleSelect(0)))).TupleConcat( 0)); // //Main loop // for (hv_I = 1; (int)hv_I <= 5; hv_I = (int)hv_I + 1) { ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, "diamond/diamond_" + (hv_I.TupleString( "02"))); //Roughly segment the diamond's position ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_Rectangle, out ho_ImageReduced); ho_Region.Dispose(); HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, 128, 255); //Extract the top of the diamond HOperatorSet.GetRegionPoints(ho_Region, out hv_Rows, out hv_Columns); // //Use the top of the diamond to align the metrology model in //the current image // HOperatorSet.AlignMetrologyModel(hv_MetrologyHandle, hv_Rows.TupleSelect( 0), hv_Columns.TupleSelect(0), 0); // // //Perform the measurement for both lines in one call // HOperatorSet.ApplyMetrologyModel(ho_Image, hv_MetrologyHandle); // //Access results // HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type", "all_param", out hv_LineParameter); HOperatorSet.AngleLl(hv_LineParameter.TupleSelect(0), hv_LineParameter.TupleSelect( 1), hv_LineParameter.TupleSelect(2), hv_LineParameter.TupleSelect(3), hv_LineParameter.TupleSelect(4), hv_LineParameter.TupleSelect(5), hv_LineParameter.TupleSelect( 6), hv_LineParameter.TupleSelect(7), out hv_Angle); hv_Angle = hv_Angle.TupleDeg(); // //Display results // //Create line contours ho_ResultContour.Dispose(); HOperatorSet.GetMetrologyObjectResultContour(out ho_ResultContour, hv_MetrologyHandle, "all", "all", 1.5); HOperatorSet.IntersectionLines(hv_LineParameter.TupleSelect(0), hv_LineParameter.TupleSelect( 1), hv_LineParameter.TupleSelect(2), hv_LineParameter.TupleSelect(3), hv_LineParameter.TupleSelect(4), hv_LineParameter.TupleSelect(5), hv_LineParameter.TupleSelect( 6), hv_LineParameter.TupleSelect(7), out hv_Row, out hv_Column, out hv_IsOverlapping1); //Calculate the orientation of the two lines HOperatorSet.LineOrientation(hv_LineParameter.TupleSelect(0), hv_LineParameter.TupleSelect( 1), hv_LineParameter.TupleSelect(2), hv_LineParameter.TupleSelect(3), out hv_Orientation1); if ((int)(new HTuple(hv_Orientation1.TupleGreater(0))) != 0) { hv_Orientation1 = hv_Orientation1 - ((new HTuple(180)).TupleRad()); } HOperatorSet.LineOrientation(hv_LineParameter.TupleSelect(4), hv_LineParameter.TupleSelect( 5), hv_LineParameter.TupleSelect(6), hv_LineParameter.TupleSelect(7), out hv_Orientation2); // //Visualize the angle between the lines ho_ContCircle.Dispose(); HOperatorSet.GenCircleContourXld(out ho_ContCircle, hv_Row, hv_Column, 100, hv_Orientation1, hv_Orientation2, "positive", 1); //Get the used measure regions and the measured points //for visualization ho_Contour.Dispose(); HOperatorSet.GetMetrologyObjectMeasures(out ho_Contour, hv_MetrologyHandle, "all", "all", out hv_MRow, out hv_MColumn); ho_Cross.Dispose(); HOperatorSet.GenCrossContourXld(out ho_Cross, hv_MRow, hv_MColumn, 6, (new HTuple(45)).TupleRad() ); //Display everything if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 1); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "yellow"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Contour, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Cross, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 2); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_ResultContour, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "blue"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_ContCircle, HDevWindowStack.GetActive()); } disp_message(hv_WindowHandle, ("Angle = " + (hv_Angle.TupleString(".5"))) + "бу", "window", 12, 12, "black", "true"); if ((int)(new HTuple(hv_I.TupleLess(5))) != 0) { disp_continue_message(hv_WindowHandle, "black", "true"); } // stop(); only in hdevelop } //Clean up memory HOperatorSet.ClearMetrologyModel(hv_MetrologyHandle); } catch (HalconException HDevExpDefaultException) { ho_Image.Dispose(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Region.Dispose(); ho_ResultContour.Dispose(); ho_ContCircle.Dispose(); ho_Contour.Dispose(); ho_Cross.Dispose(); throw HDevExpDefaultException; } ho_Image.Dispose(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Region.Dispose(); ho_ResultContour.Dispose(); ho_ContCircle.Dispose(); ho_Contour.Dispose(); ho_Cross.Dispose(); }
public void Run(SoftwareRunState softwareRunState) { Stopwatch sw = new Stopwatch(); sw.Restart(); HTuple homMat2DArrow = null; HObject arrow = null, arrowTrans = null; HObject drawLine = null, imageReducedLine; if (inputImage == null) { FormFindLine.Instance.SetToolStatus("工具输入图像为空", ToolRunStatu.Not_Input_Image); return; } try { UpdateImage(); if (inputPose != null) { HTuple Row = inputPose.X - templatePose.X; HTuple Col = inputPose.Y - templatePose.Y; HTuple angle = inputPose.U - templatePose.U; HTuple _homMat2D; HOperatorSet.HomMat2dIdentity(out _homMat2D); HOperatorSet.HomMat2dRotate(_homMat2D, (HTuple)(angle), (HTuple)templatePose.X, (HTuple)templatePose.Y, out _homMat2D); HOperatorSet.HomMat2dTranslate(_homMat2D, (HTuple)(Row), (HTuple)(Col), out _homMat2D); //对预期线的起始点做放射变换 HOperatorSet.AffineTransPixel(_homMat2D, (HTuple)expectLineStartRow, (HTuple)expectLineStartCol, out newExpectLineStartRow, out newExpectLineStartCol); HOperatorSet.AffineTransPixel(_homMat2D, (HTuple)expectLineEndRow, (HTuple)expectLineEndCol, out newExpectLineEndRow, out newExpectLineEndCol); } else { newExpectLineStartRow = expectLineStartRow; newExpectLineStartCol = expectLineStartCol; newExpectLineEndRow = expectLineEndRow; newExpectLineEndCol = expectLineEndCol; } HTuple handleID; HOperatorSet.CreateMetrologyModel(out handleID); HTuple width, height; HOperatorSet.GetImageSize(inputImage, out width, out height); HOperatorSet.SetMetrologyModelImageSize(handleID, width[0], height[0]); HTuple index; HOperatorSet.AddMetrologyObjectLineMeasure(handleID, newExpectLineStartRow, newExpectLineStartCol, newExpectLineEndRow, newExpectLineEndCol, new HTuple(50), new HTuple(20), new HTuple(1), new HTuple(30), new HTuple(), new HTuple(), out index); //参数在这里设置 HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_transition"), new HTuple(polarity)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("num_measures"), new HTuple(cliperNum)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_length1"), new HTuple(length)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_length2"), new HTuple(weidth)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_threshold"), new HTuple(threshold)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_select"), new HTuple(edgeSelect)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_sigma"), new HTuple(sigma)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("min_score"), new HTuple(minScore)); HOperatorSet.ApplyMetrologyModel(inputImage, handleID); //显示所有卡尺 HTuple pointRow, pointCol; HOperatorSet.GetMetrologyObjectMeasures(out contoursDisp, handleID, new HTuple("all"), new HTuple("all"), out pointRow, out pointCol); //显示指示找线方向的箭头 #region 测试箭头 HTuple arrowRow = null, arrowColumn = null; HOperatorSet.GenRegionLine(out drawLine, newExpectLineStartRow, newExpectLineStartCol, newExpectLineEndRow, newExpectLineEndCol); HOperatorSet.ReduceDomain(inputImage, drawLine, out imageReducedLine); HOperatorSet.GetRegionPoints(imageReducedLine, out arrowRow, out arrowColumn); if (arrowRow.Length < 200) { CommonMethods.CommonMethods.gen_arrow_contour_xld(out arrow, arrowRow[0], arrowColumn[0], arrowRow[arrowRow.Length - 1], arrowColumn[arrowRow.Length - 1], 20, 20); } else { CommonMethods.CommonMethods.gen_arrow_contour_xld(out arrow, arrowRow[0], arrowColumn[0], arrowRow[200], arrowColumn[200], 20, 20); } HOperatorSet.VectorAngleToRigid(newExpectLineStartRow, newExpectLineStartCol, 0, (newExpectLineStartRow + newExpectLineEndRow) / 2, (newExpectLineStartCol + newExpectLineEndCol) / 2, new HTuple(-90).TupleRad(), out homMat2DArrow); HOperatorSet.AffineTransContourXld(arrow, out arrowDisp, homMat2DArrow); #endregion //把点显示出来 HOperatorSet.GenCrossContourXld(out crossDisp, pointRow, pointCol, new HTuple(12), new HTuple(0)); //得到所找到的线 HTuple parameter; HOperatorSet.GetMetrologyObjectResult(handleID, new HTuple("all"), new HTuple("all"), new HTuple("result_type"), new HTuple("all_param"), out parameter); HOperatorSet.GetMetrologyObjectResultContour(out LineDisp, handleID, new HTuple("all"), new HTuple("all"), new HTuple(1.5)); if (parameter.Length >= 4) { ResultLineStartRow = parameter[0]; ResultLineStartCol = parameter[1]; ResultLineEndRow = parameter[2]; ResultLineEndCol = parameter[3]; Point start = new Point() { Row = ResultLineStartRow, Col = ResultLineStartCol }; Point end = new Point() { Row = ResultLineEndRow, Col = ResultLineEndCol }; resultLine = new Line() { StartPoint = start, EndPoint = end }; } HOperatorSet.AngleLx(ResultLineStartRow, ResultLineStartCol, ResultLineEndRow, ResultLineEndCol, out _angle); if (softwareRunState == SoftwareRunState.Debug) { DispMainWindow(FormFindLine.Instance.myHwindow); FormFindLine.Instance.tbx_resultStartRow.Text = ResultLineStartRow.ToString(); FormFindLine.Instance.tbx_resultStartCol.Text = ResultLineEndCol.ToString(); FormFindLine.Instance.tbx_resultEndRow.Text = ResultLineEndRow.ToString(); FormFindLine.Instance.tbx_resultEndCol.Text = ResultLineEndCol.ToString(); } HOperatorSet.ClearMetrologyModel(handleID); // 参数传递 ParamsTrans(); sw.Stop(); runTime = $"运行时间: {sw.ElapsedMilliseconds} ms"; FormFindLine.Instance.SetToolStatus("工具运行成功!", ToolRunStatu.Succeed); } catch (Exception ex) { FormFindLine.Instance.SetToolStatus($"工具运行异常,异常原因: {ex}", ToolRunStatu.Tool_Run_Error); } finally { //homMat2DArrow.Dispose(); //arrow.Dispose(); //arrowTrans.Dispose(); } }
private void action() { // Stack for temporary objects HObject[] OTemp = new HObject[20]; // Local iconic variables HObject ho_Rectangle, ho_ImageReduced; HObject ho_Region, ho_ConnectedRegions1, ho_SelectedRegions1; HObject ho_ContoursL, ho_RegionLines, ho_RegionUnion1, ho_RegionLines1 = null; HObject ho_RegionLines2, ho_RegionUnion2, ho_ConnectedRegions; HObject ho_SelectedRegions, ho_RegionFillUp, ho_Contours; HObject ho_ContoursSplit, ho_SelectedContours, ho_UnionContours; HObject ho_RegionDifference; // Local control variables HTuple hv_Width = null, hv_Height = null, hv_Row11 = null; HTuple hv_Column11 = null, hv_Row21 = null, hv_Column21 = null; HTuple hv_Mean = null, hv_Deviation = null, hv_Row12 = null; HTuple hv_Column12 = null, hv_Row22 = null, hv_Column22 = null; HTuple hv_shapeParam = null, hv_MetrologyHandleL = null; HTuple hv_Index = null, hv_RowL = null, hv_ColumnL = null; HTuple hv_cm = null, hv_Reducedc = null, hv_Reducedr = null; HTuple hv_j = null, hv_i = null, hv_a = null, hv_Area = null; HTuple hv_Row = null, hv_Column = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1); HOperatorSet.GenEmptyObj(out ho_SelectedRegions1); HOperatorSet.GenEmptyObj(out ho_ContoursL); HOperatorSet.GenEmptyObj(out ho_RegionLines); HOperatorSet.GenEmptyObj(out ho_RegionUnion1); HOperatorSet.GenEmptyObj(out ho_RegionLines1); HOperatorSet.GenEmptyObj(out ho_RegionLines2); HOperatorSet.GenEmptyObj(out ho_RegionUnion2); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_RegionFillUp); HOperatorSet.GenEmptyObj(out ho_Contours); HOperatorSet.GenEmptyObj(out ho_ContoursSplit); HOperatorSet.GenEmptyObj(out ho_SelectedContours); HOperatorSet.GenEmptyObj(out ho_UnionContours); HOperatorSet.GenEmptyObj(out ho_RegionDifference); HOperatorSet.Union1(algorithm.Region, out RegionToDisp); try { ho_Rectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle, DRow1m, DCol1m, DRow2m, DCol2m); ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(Image, ho_Rectangle, out ho_ImageReduced); ho_Region.Dispose(); HOperatorSet.GetImageSize(Image, out hv_Width, out hv_Height); HOperatorSet.Intensity(ho_Rectangle, ho_ImageReduced, out hv_Mean, out hv_Deviation); ho_Region.Dispose(); HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, hv_Mean + (hv_Deviation / 2), 255); ho_ConnectedRegions1.Dispose(); HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions1); ho_SelectedRegions1.Dispose(); HOperatorSet.SelectShapeStd(ho_ConnectedRegions1, out ho_SelectedRegions1, "max_area", 70); HOperatorSet.SmallestRectangle1(ho_SelectedRegions1, out hv_Row12, out hv_Column12, out hv_Row22, out hv_Column22); //找左侧点 hv_shapeParam = new HTuple(); hv_shapeParam = hv_shapeParam.TupleConcat(hv_Row12); hv_shapeParam = hv_shapeParam.TupleConcat((hv_Column22 + hv_Column12) / 2); hv_shapeParam = hv_shapeParam.TupleConcat(hv_Row22); hv_shapeParam = hv_shapeParam.TupleConcat((hv_Column22 + hv_Column12) / 2); HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandleL); HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandleL, hv_Width, hv_Height); HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandleL, "line", hv_shapeParam, ((hv_Column22 - hv_Column12) / 2) + 1, 1, 1, 30, new HTuple(), new HTuple(), out hv_Index); //设置参数 HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "measure_transition", "negative"); HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "num_measures", 100); HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "num_instances", 1); HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "measure_select", "first"); //开始找边缘 HOperatorSet.ApplyMetrologyModel(ho_ImageReduced, hv_MetrologyHandleL); ho_ContoursL.Dispose(); HOperatorSet.GetMetrologyObjectMeasures(out ho_ContoursL, hv_MetrologyHandleL, "all", "all", out hv_RowL, out hv_ColumnL); //gen_cross_contour_xld (CrossL, RowL, ColumnL, 6, 0) // stop(); only in hdevelop HOperatorSet.ClearMetrologyModel(hv_MetrologyHandleL); ho_RegionLines.Dispose(); HOperatorSet.GenRegionLine(out ho_RegionLines, hv_Row12, hv_Column12, hv_Row12 + 1, hv_Column12); ho_RegionUnion1.Dispose(); HOperatorSet.Union1(ho_RegionLines, out ho_RegionUnion1); hv_cm = (((hv_ColumnL.TupleSelect(0)) + (hv_ColumnL.TupleSelect((new HTuple(hv_RowL.TupleLength() )) - 1))) / 2) + 1; hv_Reducedc = new HTuple(); hv_Reducedr = new HTuple(); for (hv_j = 0; (int)hv_j <= (int)((new HTuple(hv_RowL.TupleLength())) - 1); hv_j = (int)hv_j + 1) { if ((int)(new HTuple(((hv_ColumnL.TupleSelect(hv_j))).TupleLessEqual(hv_cm))) != 0) { hv_Reducedc = hv_Reducedc.TupleConcat(hv_ColumnL.TupleSelect(hv_j)); hv_Reducedr = hv_Reducedr.TupleConcat(hv_RowL.TupleSelect(hv_j)); } } //gen_cross_contour_xld (Cross, Reducedr, Reducedc, 6, 0.785398) // stop(); only in hdevelop for (hv_i = 0; (int)hv_i <= (int)((new HTuple(hv_Reducedr.TupleLength())) - 2); hv_i = (int)hv_i + 1) { ho_RegionLines1.Dispose(); HOperatorSet.GenRegionLine(out ho_RegionLines1, hv_Reducedr.TupleSelect(hv_i), hv_Reducedc.TupleSelect(hv_i), hv_Reducedr.TupleSelect(hv_i + 1), hv_Reducedc.TupleSelect( hv_i + 1)); { HObject ExpTmpOutVar_0; HOperatorSet.Union2(ho_RegionUnion1, ho_RegionLines1, out ExpTmpOutVar_0); ho_RegionUnion1.Dispose(); ho_RegionUnion1 = ExpTmpOutVar_0; } } // stop(); only in hdevelop ho_RegionLines2.Dispose(); HOperatorSet.GenRegionLine(out ho_RegionLines2, hv_RowL.TupleSelect(0), hv_ColumnL.TupleSelect( 0), hv_RowL.TupleSelect((new HTuple(hv_RowL.TupleLength())) - 1), hv_ColumnL.TupleSelect( (new HTuple(hv_RowL.TupleLength())) - 1)); ho_RegionUnion2.Dispose(); HOperatorSet.Union2(ho_RegionUnion1, ho_RegionLines2, out ho_RegionUnion2); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_RegionUnion2, out ho_ConnectedRegions); ho_SelectedRegions.Dispose(); HOperatorSet.SelectShapeStd(ho_ConnectedRegions, out ho_SelectedRegions, "max_area", 70); ho_RegionFillUp.Dispose(); HOperatorSet.FillUp(ho_SelectedRegions, out ho_RegionFillUp); ho_Contours.Dispose(); HOperatorSet.GenContourRegionXld(ho_RegionFillUp, out ho_Contours, "border"); ho_ContoursSplit.Dispose(); HOperatorSet.SegmentContoursXld(ho_Contours, out ho_ContoursSplit, "lines_circles", 5, 8, 3.5); ho_SelectedContours.Dispose(); HOperatorSet.SelectContoursXld(ho_ContoursSplit, out ho_SelectedContours, "contour_length", 70, 2000, -0.5, 0.5); ho_UnionContours.Dispose(); HOperatorSet.UnionAdjacentContoursXld(ho_SelectedContours, out ho_UnionContours, 10, 1, "attr_keep"); ho_Region.Dispose(); HOperatorSet.GenRegionContourXld(ho_UnionContours, out ho_Region, "filled"); ho_RegionDifference.Dispose(); HOperatorSet.Difference(ho_RegionFillUp, ho_Region, out ho_RegionDifference); hv_a = 0; HOperatorSet.AreaCenter(ho_RegionDifference, out hv_Area, out hv_Row, out hv_Column); if ((int)(new HTuple((new HTuple(hv_Area.TupleLength())).TupleGreater(0))) != 0) { hv_a = hv_Area.TupleSum(); HOperatorSet.Union1(ho_RegionDifference, out RegionToDisp); } else { hv_a = 0; } HTuple hv_result = GetHv_result(); hv_result = hv_result.TupleConcat("缺陷面积"); hv_result = hv_result.TupleConcat(hv_a.D); result = hv_result.Clone(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Region.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions1.Dispose(); ho_ContoursL.Dispose(); ho_RegionLines.Dispose(); ho_RegionUnion1.Dispose(); ho_RegionLines1.Dispose(); ho_RegionLines2.Dispose(); ho_RegionUnion2.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_RegionFillUp.Dispose(); ho_Contours.Dispose(); ho_ContoursSplit.Dispose(); ho_SelectedContours.Dispose(); ho_UnionContours.Dispose(); ho_RegionDifference.Dispose(); algorithm.Region.Dispose(); } catch { HTuple hv_result = GetHv_result(); hv_result = hv_result.TupleConcat("缺陷面积"); hv_result = hv_result.TupleConcat(0); result = hv_result.Clone(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Region.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions1.Dispose(); ho_ContoursL.Dispose(); ho_RegionLines.Dispose(); ho_RegionUnion1.Dispose(); ho_RegionLines1.Dispose(); ho_RegionLines2.Dispose(); ho_RegionUnion2.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_RegionFillUp.Dispose(); ho_Contours.Dispose(); ho_ContoursSplit.Dispose(); ho_SelectedContours.Dispose(); ho_UnionContours.Dispose(); ho_RegionDifference.Dispose(); algorithm.Region.Dispose(); } finally { ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Region.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions1.Dispose(); ho_ContoursL.Dispose(); ho_RegionLines.Dispose(); ho_RegionUnion1.Dispose(); ho_RegionLines1.Dispose(); ho_RegionLines2.Dispose(); ho_RegionUnion2.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_RegionFillUp.Dispose(); ho_Contours.Dispose(); ho_ContoursSplit.Dispose(); ho_SelectedContours.Dispose(); ho_UnionContours.Dispose(); ho_RegionDifference.Dispose(); algorithm.Region.Dispose(); } }
public bool Find_halcon_line(ExecuteBuffer _executeBuffer, HWndCtrl hWndCtrl, 模板GVName_halcon Model_result, Dictionary <int, PointName> Point_temp_result, out string result_info, out HTuple Row1, out HTuple Column1, out HTuple Row2, out HTuple Column2, bool show_info) { HTuple MetrologyHandle; HTuple pic_wid, pic_height; Row1 = new HTuple(); Column1 = new HTuple(); Row2 = new HTuple(); Column2 = new HTuple(); result_info = ""; HOperatorSet.CreateMetrologyModel(out MetrologyHandle); if (!_executeBuffer.imageBuffer.ContainsKey(this.Threshold_image.SelectedItem.ToString() + ".img")) { // MessageBox.Show("查找直线: 输入图像已经不存在,请重置设置输入图像"); result_info = " 查找直线: 输入图像已经不存在,请重置设置输入图像"; return(false); } if (_executeBuffer.imageBuffer[this.Threshold_image.SelectedItem.ToString() + ".img"] == null) { // MessageBox.Show("查找直线: image参数为空或者未赋值"); result_info = " 查找直线: 输入图像已经不存在,请重置设置输入图像"; return(false); } HObject imagein = _executeBuffer.imageBuffer[this.Threshold_image.SelectedItem.ToString() + ".img"]; HOperatorSet.GetImageSize(imagein, out pic_wid, out pic_height); HOperatorSet.SetMetrologyModelImageSize(MetrologyHandle, pic_wid, pic_height); HTuple Index; HTuple trow1, trow2, tcol1, tcol2; if (checkBox_line.Checked) { if (xrow1 == null || xrow2 == null || xcol1 == null || xcol2 == null) { // MessageBox.Show("查找直线:未确认线位置"); result_info = " 查找直线: 未确认线位置"; return(false); } else { trow1 = xrow1; tcol1 = xcol1; trow2 = xrow2; tcol2 = xcol2; } } else { if (this.cbb_source1.SelectedIndex == 0) { if (Model_result == null) { // MessageBox.Show("查找直线:匹配列表为空,请设置"); result_info = " 查找直线: 匹配列表为空,请设置"; return(false); } if (Model_result.点X.TupleLength() < 1) { // MessageBox.Show("查找直线:匹配列表为空,请设置"); result_info = " 查找直线: 匹配列表为空,请设置"; return(false); } trow1 = Model_result.点Y[0]; tcol1 = Model_result.点X[0]; } else { int number1 = Convert.ToInt32(this.tb_value1.Text); if (!Point_temp_result.ContainsKey(number1)) { // MessageBox.Show("查找直线:全局列表点无此点,请设置"); result_info = " 查找直线: 全局列表无此点,请设置"; return(false); } else { trow1 = Point_temp_result[number1].点Y; tcol1 = Point_temp_result[number1].点X; } } int number2 = Convert.ToInt32(this.tb_value2.Text); if (!Point_temp_result.ContainsKey(number2)) { // MessageBox.Show("查找直线:全局列表点无此点,请设置"); result_info = " 查找直线: 全局列表无此点,请设置"; return(false); } else { trow2 = Point_temp_result[number2].点Y; tcol2 = Point_temp_result[number2].点X; } } HOperatorSet.AddMetrologyObjectLineMeasure(MetrologyHandle, trow1, tcol1, trow2, tcol2, Convert.ToInt32(this.measure_length1.Text.ToString()), Convert.ToInt32(this.measure_length2.Text.ToString()), 1, Convert.ToInt32(this.measure_threshold.Text.ToString()), new HTuple(), new HTuple(), out Index); HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "measure_transition", this.cbb_transition.SelectedItem.ToString()); HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "num_measures", Convert.ToInt32(this.num_measure.Text.ToString())); HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "num_instances", 40); HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "measure_sigma", 1); //HOperatorSet.SetMetrologyObjectParam(MetrologyHandle,"all","measure_threshold",50); HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "measure_interpolation", "bicubic"); HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "measure_select", this.cbb_measure_select.SelectedItem.ToString()); HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "min_score", Convert.ToDouble(this.min_score.Text)); HOperatorSet.ApplyMetrologyModel(imagein, MetrologyHandle); HObject Contours, Cross; HTuple Rowx, Columnx; HOperatorSet.GetMetrologyObjectMeasures(out Contours, MetrologyHandle, "all", "all", out Rowx, out Columnx); HOperatorSet.GenCrossContourXld(out Cross, Rowx, Columnx, 6, 0.785398); HTuple hv_Parameter = null; //得到线的起点和终点坐标并显示出来 HOperatorSet.GetMetrologyObjectResult(MetrologyHandle, "all", "all", "result_type", "all_param", out hv_Parameter); if (hv_Parameter.TupleLength() == 0) { result_info = " 查找直线: 在指定位置,找直线失败"; // MessageBox.Show("未找到直线"); return(false); } int line_count = hv_Parameter.TupleLength() / 4; double[] Row_1 = new double[line_count]; double[] Column_1 = new double[line_count]; double[] Row_2 = new double[line_count]; double[] Column_2 = new double[line_count]; for (int imx = 0; imx < line_count; imx++) { Row_1[imx] = hv_Parameter[imx * 4 + 1].D; Column_1[imx] = hv_Parameter[imx * 4].D; Row_2[imx] = hv_Parameter[imx * 4 + 3].D; Column_2[imx] = hv_Parameter[imx * 4 + 2].D; } Row1 = (HTuple)Row_1; Column1 = (HTuple)Column_1; Row2 = (HTuple)Row_2; Column2 = (HTuple)Column_2; HObject ho_Contour; HOperatorSet.GenEmptyObj(out ho_Contour); ho_Contour.Dispose(); HOperatorSet.GetMetrologyObjectResultContour(out ho_Contour, MetrologyHandle, "all", "all", 1.5); HOperatorSet.ClearMetrologyModel(MetrologyHandle); if (show_info) { hWndCtrl.changeGraphicSettings(GraphicsContext.GC_COLOR, this.comboBox1.SelectedItem.ToString()); hWndCtrl.addIconicVar(Cross); hWndCtrl.addIconicVar(Contours); } hWndCtrl.changeGraphicSettings(GraphicsContext.GC_COLOR, this.comboBox1.SelectedItem.ToString()); hWndCtrl.addIconicVar(ho_Contour); hWndCtrl.repaint(); return(true); }
public void Run() { if (inputImage == null) { FormFindLine.Instance.TextBoxMessageDisp("图像为空", System.Drawing.Color.Red); toolRunStatu = ToolRunStatu.Not_Input_Image; return; } try { UpdateImage(); if (inputPose != null) { HTuple Row = inputPose.X - templatePose.X; HTuple Col = inputPose.Y - templatePose.Y; HTuple angle = inputPose.U - templatePose.U; HTuple _homMat2D; HOperatorSet.HomMat2dIdentity(out _homMat2D); HOperatorSet.HomMat2dRotate(_homMat2D, (HTuple)(angle), (HTuple)templatePose.X, (HTuple)templatePose.Y, out _homMat2D); HOperatorSet.HomMat2dTranslate(_homMat2D, (HTuple)(Row), (HTuple)(Col), out _homMat2D); //对预期线的起始点做放射变换 HOperatorSet.AffineTransPixel(_homMat2D, (HTuple)expectLineStartRow, (HTuple)expectLineStartCol, out newExpectLineStartRow, out newExpectLineStartCol); HOperatorSet.AffineTransPixel(_homMat2D, (HTuple)expectLineEndRow, (HTuple)expectLineEndCol, out newExpectLineEndRow, out newExpectLineEndCol); } else { newExpectLineStartRow = expectLineStartRow; newExpectLineStartCol = expectLineStartCol; newExpectLineEndRow = expectLineEndRow; newExpectLineEndCol = expectLineEndCol; } HTuple handleID; HOperatorSet.CreateMetrologyModel(out handleID); HTuple width, height; HOperatorSet.GetImageSize(inputImage, out width, out height); HOperatorSet.SetMetrologyModelImageSize(handleID, width[0], height[0]); HTuple index; HOperatorSet.AddMetrologyObjectLineMeasure(handleID, newExpectLineStartRow, newExpectLineStartCol, newExpectLineEndRow, newExpectLineEndCol, new HTuple(50), new HTuple(20), new HTuple(1), new HTuple(30), new HTuple(), new HTuple(), out index); //参数在这里设置 HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_transition"), new HTuple(polarity)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("num_measures"), new HTuple(cliperNum)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_length1"), new HTuple(length)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_length2"), new HTuple(weidth)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_threshold"), new HTuple(threshold)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_select"), new HTuple(edgeSelect)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_sigma"), new HTuple(sigma)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("min_score"), new HTuple(minScore)); HOperatorSet.ApplyMetrologyModel(inputImage, handleID); //显示所有卡尺 HTuple pointRow, pointCol; HOperatorSet.GetMetrologyObjectMeasures(out contours, handleID, new HTuple("all"), new HTuple("all"), out pointRow, out pointCol); HOperatorSet.SetColor(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, new HTuple("blue")); HOperatorSet.DispObj(contours, FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow); FormFindLine.Instance.myHwindow.DispObj(contours, "blue"); //显示指示找线方向的箭头 HTuple arrowAngle; HOperatorSet.AngleLx(newExpectLineStartRow, newExpectLineStartCol, newExpectLineEndRow, newExpectLineEndCol, out arrowAngle); #region 测试箭头 #endregion arrowAngle = arrowAngle + Math.PI / 2; arrowAngle = arrowAngle.TupleDeg(); HTuple row = (newExpectLineStartRow + newExpectLineEndRow) / 2; HTuple column = (newExpectLineStartCol + newExpectLineEndCol) / 2; double drow, dcolumn; double arrowLength = length + 100; if (0 <= arrowAngle && arrowAngle <= 90) { drow = Math.Abs(arrowLength * Math.Sin(((HTuple)arrowAngle).TupleRad())); dcolumn = Math.Abs(arrowLength * Math.Cos(((HTuple)arrowAngle).TupleRad())); HOperatorSet.DispArrow(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, row, column, row - drow, column + dcolumn, 5.0); } else if (arrowAngle > 90 && arrowAngle <= 180) { drow = arrowLength * Math.Sin(((HTuple)(180 - arrowAngle)).TupleRad()); dcolumn = arrowLength * Math.Cos(((HTuple)(180 - arrowAngle)).TupleRad()); HOperatorSet.DispArrow(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, row, column, row - drow, column - dcolumn, 5.0); } else if (arrowAngle < 0 && arrowAngle >= -90) { drow = arrowLength * Math.Sin(((HTuple)arrowAngle * -1).TupleRad()); dcolumn = arrowLength * Math.Cos(((HTuple)arrowAngle * -1).TupleRad()); HOperatorSet.DispArrow(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, row, column, row + drow, column + dcolumn, 5.0); } else if (arrowAngle < -90 && arrowAngle >= -180) { drow = Math.Abs(arrowLength * Math.Sin(((HTuple)arrowAngle + 180).TupleRad())); dcolumn = Math.Abs(arrowLength * Math.Cos(((HTuple)arrowAngle + 180).TupleRad())); HOperatorSet.DispArrow(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, row, column, row + drow, column - dcolumn, 5.0); } //把点显示出来 HObject cross; HOperatorSet.GenCrossContourXld(out cross, pointRow, pointCol, new HTuple(12), new HTuple(0)); HOperatorSet.SetColor(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, new HTuple("orange")); HOperatorSet.DispObj(cross, FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow); FormFindLine.Instance.myHwindow.DispObj(cross, "orange"); //得到所找到的线 HTuple parameter; HObject line; HOperatorSet.GetMetrologyObjectResult(handleID, new HTuple("all"), new HTuple("all"), new HTuple("result_type"), new HTuple("all_param"), out parameter); HOperatorSet.GetMetrologyObjectResultContour(out line, handleID, new HTuple("all"), new HTuple("all"), new HTuple(1.5)); if (parameter.Length >= 4) { ResultLineStartRow = parameter[0]; ResultLineStartCol = parameter[1]; ResultLineEndRow = parameter[2]; ResultLineEndCol = parameter[3]; Point start = new Point() { Row = ResultLineStartRow, Col = ResultLineStartCol }; Point end = new Point() { Row = ResultLineEndRow, Col = ResultLineEndCol }; resultLine = new Line() { StartPoint = start, EndPoint = end }; //显示找到的线 HOperatorSet.SetColor(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, new HTuple("green")); // HOperatorSet.DispObj(line, GetWindowHandle(jobName)); FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow.SetLineWidth(2); FormFindLine.Instance.myHwindow.DispObj(line, "green"); } HOperatorSet.AngleLx(ResultLineStartRow, ResultLineStartCol, ResultLineEndRow, ResultLineEndCol, out _angle); HOperatorSet.ClearMetrologyModel(handleID); FormFindLine.Instance.tbx_resultStartRow.Text = ResultLineStartRow.ToString(); FormFindLine.Instance.tbx_resultStartCol.Text = ResultLineEndCol.ToString(); FormFindLine.Instance.tbx_resultEndRow.Text = ResultLineEndRow.ToString(); FormFindLine.Instance.tbx_resultEndCol.Text = ResultLineEndCol.ToString(); FormFindLine.Instance.TextBoxMessageDisp("运行成功", System.Drawing.Color.Green); toolRunStatu = ToolRunStatu.Succeed; } catch (Exception ex) { FormFindLine.Instance.TextBoxMessageDisp("工具运行异常" + ex.Message, System.Drawing.Color.Red); } }
public bool CreateLineModel(HTuple hv_HomMat2D) { try { HOperatorSet.ClearMetrologyModel(hv_MetrologyHandle); HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle); if (ho_Image == null) { return(false); } HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); if (hv_Row1_Horizon.Length < 1) { return(false); } if (hv_Row1_Vertical.Length < 1) { return(false); } HTuple hv_Row1 = null; HTuple hv_Column1 = null; HTuple hv_Row2 = null; HTuple hv_Column2 = null; HTuple mhv_Row1_Horizon; //水平线 HTuple mhv_Column1_Horizon; HTuple mhv_Row2_Horizon; HTuple mhv_Column2_Horizon; HTuple mhv_Row1_Vertical; //竖直线 HTuple mhv_Column1_Vertical; HTuple mhv_Row2_Vertical; HTuple mhv_Column2_Vertical; HOperatorSet.AffineTransPixel(hv_HomMat2D, hv_Row1_Horizon, hv_Column1_Horizon, out mhv_Row1_Horizon, out mhv_Column1_Horizon); HOperatorSet.AffineTransPixel(hv_HomMat2D, hv_Row2_Horizon, hv_Column2_Horizon, out mhv_Row2_Horizon, out mhv_Column2_Horizon); HOperatorSet.AffineTransPixel(hv_HomMat2D, hv_Row1_Vertical, hv_Column1_Vertical, out mhv_Row1_Vertical, out mhv_Column1_Vertical); HOperatorSet.AffineTransPixel(hv_HomMat2D, hv_Row2_Vertical, hv_Column2_Vertical, out mhv_Row2_Vertical, out mhv_Column2_Vertical); HOperatorSet.TupleInsert(mhv_Row1_Horizon, 0, mhv_Row1_Vertical, out hv_Row1); HOperatorSet.TupleInsert(mhv_Column1_Horizon, 0, mhv_Column1_Vertical, out hv_Column1); HOperatorSet.TupleInsert(mhv_Row2_Horizon, 0, mhv_Row2_Vertical, out hv_Row2); HOperatorSet.TupleInsert(mhv_Column2_Horizon, 0, mhv_Column2_Vertical, out hv_Column2); HOperatorSet.AddMetrologyObjectLineMeasure(hv_MetrologyHandle, hv_Row1, hv_Column1, hv_Row2, hv_Column2, hv_MeasureLength1, hv_MeasureLength2, hv_MeasureSigma, hv_MeasureThreshold, hv_GenParamName, hv_GenParamValue, out hv_Index); errorFlag = false; return(true); } catch (Exception exc) { errorFlag = true; return(false); } }
public bool Find_halcon_circle(ExecuteBuffer _executeBuffer, HWndCtrl hWndCtrl, 模板GVName_halcon Model_result, Dictionary <int, PointName> Point_temp_result, out string result_info, out HTuple Row, out HTuple Column, out HTuple Radius, bool show_info) { HTuple MetrologyHandle; HTuple pic_wid, pic_height; Row = new HTuple(); Column = new HTuple(); Radius = new HTuple(); result_info = ""; HOperatorSet.CreateMetrologyModel(out MetrologyHandle); if (!_executeBuffer.imageBuffer.ContainsKey(this.Threshold_image.SelectedItem.ToString() + ".img")) { MessageBox.Show("查找圆: 输入图像已经不存在,请重置设置输入图像"); result_info = " 查找圆: 输入图像已经不存在,请重置设置输入图像"; return(false); } if (_executeBuffer.imageBuffer[this.Threshold_image.SelectedItem.ToString() + ".img"] == null) { MessageBox.Show("查找圆: image参数为空或者未赋值"); result_info = " 查找圆: 输入图像已经不存在,请重置设置输入图像"; return(false); } /* * if (xrow == null || xcol == null || xradius == null) * { * MessageBox.Show("查找圆: 未确定圆位置"); * result_info = " 查找圆: 未确定圆位置"; * return false; * }*/ HObject imagein = _executeBuffer.imageBuffer[this.Threshold_image.SelectedItem.ToString() + ".img"]; HOperatorSet.GetImageSize(imagein, out pic_wid, out pic_height); HOperatorSet.SetMetrologyModelImageSize(MetrologyHandle, pic_wid, pic_height); HTuple Index; HTuple trow, tcol, tradius; if (this.checkBox_circle.Checked) { if (xrow == null || xcol == null || xradius == null) { MessageBox.Show("查找直线:未确认线位置"); result_info = " 查找直线: 未确认线位置"; return(false); } else if (xrow.D == 0 && xcol.D == 0 && xradius.D == 0) { MessageBox.Show("查找直线:未确认线位置"); result_info = " 查找直线: 未确认线位置"; return(false); } else { trow = xrow; tcol = xcol; tradius = xradius; } } else { tradius = Convert.ToDouble(this.textBox_radius.Text); if (this.cbb_source1.SelectedIndex == 0) { if (Model_result == null) { MessageBox.Show("查找圆:匹配列表为空,请设置"); result_info = " 查找圆: 匹配列表为空,请设置"; return(false); } if (Model_result.点X.TupleLength() < 1) { MessageBox.Show("查找圆:匹配列表为空,请设置"); result_info = " 查找圆: 匹配列表为空,请设置"; return(false); } trow = Model_result.点Y[0]; tcol = Model_result.点X[0]; } else { int number1 = Convert.ToInt32(this.tb_value1.Text); if (!Point_temp_result.ContainsKey(number1)) { MessageBox.Show("查找圆:全局列表点无此点,请设置"); result_info = " 查找圆: 全局列表无此点,请设置"; return(false); } else { trow = Point_temp_result[number1].点Y; tcol = Point_temp_result[number1].点X; } } } HOperatorSet.AddMetrologyObjectCircleMeasure(MetrologyHandle, trow, tcol, tradius, Convert.ToInt32(this.measure_length1.Text.ToString()), Convert.ToInt32(this.measure_length2.Text.ToString()), 1, Convert.ToInt32(this.measure_threshold.Text.ToString()), new HTuple(), new HTuple(), out Index); // HOperatorSet.SetMetrologyObjectParam(MetrologyHandle,"all","measure_transition","negative"); HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "measure_transition", this.cbb_transition.SelectedItem.ToString()); HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "measure_select", this.cbb_measure_select.SelectedItem.ToString()); HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "num_measures", Convert.ToInt32(this.num_measure.Text.ToString())); HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "num_instances", 40); HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "measure_sigma", 1); HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "measure_interpolation", "bicubic"); //HOperatorSet.SetMetrologyObjectParam(MetrologyHandle,"all","measure_select","all"); HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "min_score", 0.4); HOperatorSet.ApplyMetrologyModel(imagein, MetrologyHandle); HObject Contours, Cross; HOperatorSet.GetMetrologyObjectMeasures(out Contours, MetrologyHandle, "all", "all", out Row, out Column); HOperatorSet.GenCrossContourXld(out Cross, Row, Column, 6, 0.785398); HTuple hv_Parameter = null; //得到线的起点和终点坐标并显示出来 HOperatorSet.GetMetrologyObjectResult(MetrologyHandle, "all", "all", "result_type", "all_param", out hv_Parameter); if (hv_Parameter.TupleLength() == 0) { result_info = " 查找圆: 在指定位置,找圆失败"; MessageBox.Show("未找到圆"); return(false); } int Circle_number = hv_Parameter.TupleLength() / 3; double[] Row_out = new double[Circle_number]; double[] Column_out = new double[Circle_number]; double[] Radius_out = new double[Circle_number]; for (int i = 0; i < Circle_number; i++) { Row_out[i] = hv_Parameter[i * 3 + 1].D; Column_out[i] = hv_Parameter[i * 3].D; Radius_out[i] = hv_Parameter[i * 3 + 2].D; } Row = (HTuple)Row_out; Column = (HTuple)Column_out; Radius = (HTuple)Radius_out; HObject ho_Contour; HOperatorSet.GenEmptyObj(out ho_Contour); ho_Contour.Dispose(); HOperatorSet.GetMetrologyObjectResultContour(out ho_Contour, MetrologyHandle, "all", "all", 1.5); HOperatorSet.ClearMetrologyModel(MetrologyHandle); if (show_info) { hWndCtrl.changeGraphicSettings(GraphicsContext.GC_COLOR, this.comboBox1.SelectedItem.ToString()); hWndCtrl.addIconicVar(Cross); hWndCtrl.addIconicVar(Contours); } hWndCtrl.changeGraphicSettings(GraphicsContext.GC_COLOR, this.comboBox1.SelectedItem.ToString()); hWndCtrl.addIconicVar(ho_Contour); hWndCtrl.repaint(); return(true); }