string RunCalibDeviation(double[] Pix_X, double[] Pix_Y, out string Deviation) { Deviation = ""; try { HTuple real_x = new HTuple(); HTuple real_y = new HTuple(); HTuple real_z = new HTuple(); HTuple tx1 = 0; HOperatorSet.AffineTransPoint2d(HomMat2Dxy, Pix_X, Pix_Y, out real_x, out real_y); HTuple subx = AxisX - real_x; HTuple suby = AxisY - real_y; subx = subx.TupleAbs(); suby = suby.TupleAbs(); double max_x = subx.TupleMax(); double max_y = suby.TupleMax(); double min_x = subx.TupleMin(); double min_y = suby.TupleMin(); double Meanx = subx.TupleMean(); double Meany = suby.TupleMean(); max_x = Math.Round(max_x, 3); max_y = Math.Round(max_y, 3); min_x = Math.Round(min_x, 3); min_y = Math.Round(min_y, 3); Meanx = Math.Round(Meanx, 3); Meany = Math.Round(Meany, 3); Deviation = string.Format("最大误差x:{0};y: {1};\r\n最小误差x:{2};y: {3};\r\n平均误差x:{4};y: {5};", max_x.ToString(), max_y.ToString(), min_x.ToString(), min_y.ToString(), Meanx.ToString(), Meany.ToString()); return("OK"); } catch (Exception ex) { MessageBox.Show(ex.Message); return(ex.Message); } }
private static bool action(Bitmap bmp_with_no_glue, Bitmap bmp_with_glue, HWindow Window) { try { #region// 声明图像变量 HObject ho_ImageT, ho_ImageF, ho_TransImage; HObject ho_ImageSub, ho_ImageGauss, ho_Regions_R, ho_RegionClosing; HObject ho_RegionOpening1, ho_RegionUnion, ho_Rectangle; HObject ho_RegionDifference, ho_RegionOpening, ho_Regionc; HObject ho_ConnectedRegions, ho_SelectedRegions, ho_RegionFillUp1; HObject ho_RegionErosion, ho_Contours1, ho_Circle_mask_S = null; HObject ho_target_ring = null, ho_target_ring_scale = null; HObject ho_Regions = null, ho_RegionFillUp = null, ho_RegionTrans = null; HObject ho_Contours = null, ho_glueOutterContour = null, ho_glueInnerContour = null; HObject ho_Contour_I_sap = null, ho_Contour_O_sap = null, ho_ContCircle_outter = null; HObject ho_ContCircle_Inner = null, Regionoping = null; HObject SmoothedGlueOutterContour = null, SmoothedGlueInnerContour = null; #endregion #region//初始化图像变量 HOperatorSet.GenEmptyObj(out ho_ImageT); HOperatorSet.GenEmptyObj(out ho_ImageF); HOperatorSet.GenEmptyObj(out ho_TransImage); HOperatorSet.GenEmptyObj(out ho_ImageSub); HOperatorSet.GenEmptyObj(out ho_ImageGauss); HOperatorSet.GenEmptyObj(out ho_Regions_R); HOperatorSet.GenEmptyObj(out ho_RegionClosing); HOperatorSet.GenEmptyObj(out ho_RegionOpening1); HOperatorSet.GenEmptyObj(out ho_RegionUnion); HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_RegionDifference); HOperatorSet.GenEmptyObj(out ho_RegionOpening); HOperatorSet.GenEmptyObj(out ho_Regionc); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_RegionFillUp1); HOperatorSet.GenEmptyObj(out ho_RegionErosion); HOperatorSet.GenEmptyObj(out ho_Contours1); HOperatorSet.GenEmptyObj(out ho_Circle_mask_S); HOperatorSet.GenEmptyObj(out ho_target_ring); HOperatorSet.GenEmptyObj(out ho_target_ring_scale); HOperatorSet.GenEmptyObj(out ho_Regions); HOperatorSet.GenEmptyObj(out ho_RegionFillUp); HOperatorSet.GenEmptyObj(out ho_RegionTrans); HOperatorSet.GenEmptyObj(out ho_Contours); HOperatorSet.GenEmptyObj(out ho_glueOutterContour); HOperatorSet.GenEmptyObj(out ho_glueInnerContour); HOperatorSet.GenEmptyObj(out ho_Contour_I_sap); HOperatorSet.GenEmptyObj(out ho_Contour_O_sap); HOperatorSet.GenEmptyObj(out ho_ContCircle_outter); HOperatorSet.GenEmptyObj(out ho_ContCircle_Inner); HOperatorSet.GenEmptyObj(out Regionoping); #endregion #region// 初始化控制变量 // Local control variables HTuple hv_Width = null, hv_Height = null, hv_WindowHandle = new HTuple(); HTuple hv_RowsF = null, hv_ColsF = null, hv_RowsT = null; HTuple hv_ColsT = null, hv_HomMat2D = null, hv_Points1 = null; HTuple hv_Points2 = null, hv_Number1 = null, hv_Number2 = null; HTuple hv_Row = new HTuple(); HTuple hv_Column = new HTuple(), hv_Radius = new HTuple(); HTuple hv_StartPhi = new HTuple(), hv_EndPhi = new HTuple(); HTuple hv_PointOrder = new HTuple(); //HTuple hv_glueInnerCircle = new HTuple(), hv_glueWidth = new HTuple(); HTuple hv_glueOutterCircle = new HTuple(); HTuple hv_glueCenter = new HTuple(), hv_Row2 = new HTuple(); HTuple hv_Col1 = new HTuple(), hv_Row3 = new HTuple(); HTuple hv_Col2 = new HTuple(), hv_Row_Inner_sampling = new HTuple(); HTuple hv_Col_Inner_sampling = new HTuple(), hv_k = new HTuple(); HTuple hv_i = new HTuple(), hv_Row_Outter_sampling = new HTuple(); HTuple hv_Col_Outter_sampling = new HTuple(), hv_DistanceMin = new HTuple(); HTuple hv_DistanceMax = new HTuple(), hv_glueWidthMin = new HTuple(); HTuple hv_glueWidthMax = new HTuple(), hv_DistanceMin1 = new HTuple(); HTuple hv_DistanceMax1 = new HTuple(), hv_Distance_inner_min = new HTuple(); HTuple hv_Distance_inner_max = new HTuple(), hv_DistanceMin2 = new HTuple(); HTuple hv_DistanceMax2 = new HTuple(), hv_Distance_outter_min = new HTuple(); HTuple hv_Distance_outter_max = new HTuple(), hv_Row_Outter = new HTuple(); HTuple hv_Column_Outter = new HTuple(), hv_Radius_Outter = new HTuple(); HTuple hv_StartPhi1 = new HTuple(), hv_EndPhi1 = new HTuple(); HTuple hv_PointOrder1 = new HTuple(), hv_DistanceMin_Outter = new HTuple(); HTuple hv_DistanceMax_Outter = new HTuple(), hv_Ddistance_Outter = new HTuple(); HTuple hv_Sdistance_Outter = new HTuple(), hv_Row_Inner = new HTuple(); HTuple hv_Column_Inner = new HTuple(), hv_Radius_Inner = new HTuple(); HTuple hv_StartPhi2 = new HTuple(), hv_EndPhi2 = new HTuple(); HTuple hv_PointOrder2 = new HTuple(), hv_DistanceMin_Inner = new HTuple(); HTuple hv_DistanceMax_Inner = new HTuple(), hv_Ddistance_Inner = new HTuple(); HTuple hv_Sdistance_Inner = new HTuple(), hv_Row_glue = new HTuple(); HTuple hv_Col_glue = new HTuple(), hv_Distance_offset = new HTuple(); HTuple hv_Font = new HTuple(); #endregion is_qualified = true; ho_ImageT.Dispose(); Bitmap2HObject.Bitmap2HObj(bmp_with_no_glue, out ho_ImageT); bmp_with_no_glue = null; HObject img_old; HOperatorSet.GenEmptyObj(out img_old); HTuple htuple; //彩色变灰色 HOperatorSet.CountChannels(ho_ImageT, out htuple); img_old.Dispose(); if (htuple == 3) { HOperatorSet.Rgb1ToGray(ho_ImageT, out img_old); ho_ImageT.Dispose(); ho_ImageT = img_old.Clone(); } //CenterLocate.LastCenterLocateBMP.Dispose(); ho_ImageF.Dispose(); Bitmap2HObject.Bitmap2HObj(bmp_with_glue, out ho_ImageF); bmp_with_glue.Dispose(); //彩色变灰色 HOperatorSet.CountChannels(ho_ImageF, out htuple); img_old.Dispose(); if (htuple == 3) { HOperatorSet.Rgb1ToGray(ho_ImageF, out img_old); ho_ImageF.Dispose(); ho_ImageF = img_old.Clone(); } //获取图像的尺寸信息 HOperatorSet.GetImageSize(ho_ImageF, out hv_Width, out hv_Height); //*******************主逻辑 #region//提取特征点,根据特征点求两张图之间的仿射矩阵:HomMat2D,并对齐图像 HOperatorSet.PointsHarris(ho_ImageF, 2.5, 2, 0.08, 2500, out hv_RowsF, out hv_ColsF); HOperatorSet.PointsHarris(ho_ImageT, 2.5, 2, 0.08, 2500, out hv_RowsT, out hv_ColsT); //* 根据特征点求两张图之间的仿射矩阵:HomMat2D HOperatorSet.ProjMatchPointsRansac(ho_ImageT, ho_ImageF, hv_RowsT, hv_ColsT, hv_RowsF, hv_ColsF, "sad", 10, 0, 0, 100, 100, (new HTuple((new HTuple(-0.2)).TupleRad() )).TupleConcat((new HTuple(0.2)).TupleRad()), 10, "gold_standard", 0.2, 0, out hv_HomMat2D, out hv_Points1, out hv_Points2); //*将ImageT进行放射变换以对齐两张图片 ho_TransImage.Dispose(); HOperatorSet.ProjectiveTransImage(ho_ImageT, out ho_TransImage, hv_HomMat2D, "bilinear", "false", "false"); //ho_ImageT.Dispose(); #endregion #region//提取胶圈轮廓 ////将对齐后点胶之后的图片减去点胶之前图片 //ho_ImageSub.Dispose(); //HOperatorSet.SubImage(ho_ImageF, ho_TransImage, out ho_ImageSub, 0.95,110); ////**对相减得到的图片进行处理,提出胶圈轮廓 ////平滑图像 //ho_ImageGauss.Dispose(); ////HOperatorSet.GaussFilter(ho_ImageSub, out ho_ImageGauss, 9); //HOperatorSet.MedianImage(ho_ImageSub, out ho_ImageGauss,"circle", 2.5,"cyclic"); //ho_ImageSub.Dispose(); ////*阈值分割,采用区域生长法,控制参数tol=3, region阈值500000 //ho_Regions_R.Dispose(); //HOperatorSet.Regiongrowing(ho_ImageGauss, out ho_Regions_R, 4, 4, tol, area); //ho_ImageGauss.Dispose(); ////对region进行开运算和闭运算,并合并region //ho_RegionClosing.Dispose(); //HOperatorSet.ClosingCircle(ho_Regions_R, out ho_RegionClosing, 30); //ho_RegionOpening1.Dispose(); //HOperatorSet.OpeningCircle(ho_RegionClosing, out ho_RegionOpening1, 11); //ho_RegionUnion.Dispose(); //HOperatorSet.Union1(ho_RegionOpening1, out ho_RegionUnion); ho_ImageSub.Dispose(); //HOperatorSet.AbsDiffImage(ho_ImageF, ho_TransImage, out ho_ImageSub, GlueFindParam.Instance.abs_diff_image_value); HOperatorSet.SubImage(ho_ImageF, ho_TransImage, out ho_ImageSub, 0.95, 110); try { HOperatorSet.WriteImage(ho_ImageSub, "jpg", 0, "C:\\Sub.jpg"); } catch { } HObject hpowimage, hscareimage; HOperatorSet.GenEmptyObj(out hpowimage); HOperatorSet.GenEmptyObj(out hscareimage); hpowimage.Dispose(); HOperatorSet.PowImage(ho_ImageSub, out hpowimage, 1.0); hscareimage.Dispose(); HOperatorSet.ScaleImageMax(hpowimage, out hscareimage); //**对相减得到的图片进行处理,提出胶圈轮廓 //平滑图像 ho_ImageGauss.Dispose(); //HOperatorSet.GaussFilter(ho_ImageSub, out ho_ImageGauss, 11); HOperatorSet.MedianImage(hscareimage, out ho_ImageGauss, "circle", 2, "cyclic"); ho_ImageSub.Dispose(); //*阈值分割,采用区域生长法,控制参数tol=3, region阈值500000 ho_Regions_R.Dispose(); HOperatorSet.Regiongrowing(ho_ImageGauss, out ho_Regions_R, 4, 4, tol, area); //提取胶圈区域region ho_Rectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle, 0, 0, hv_Height, hv_Width); ho_RegionDifference.Dispose(); //HOperatorSet.Difference(ho_Rectangle, ho_RegionUnion, out ho_RegionDifference); HOperatorSet.Difference(ho_Rectangle, ho_Regions_R, out ho_RegionDifference); HOperatorSet.CountObj(ho_RegionDifference, out hv_Number1); ho_RegionOpening.Dispose(); HOperatorSet.OpeningCircle(ho_RegionDifference, out ho_RegionOpening, 30); ho_Regionc.Dispose(); HOperatorSet.ClosingCircle(ho_RegionOpening, out ho_Regionc, 30); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_Regionc, out ho_ConnectedRegions); ho_SelectedRegions.Dispose(); HOperatorSet.SelectShapeStd(ho_ConnectedRegions, out ho_SelectedRegions, "max_area", 70); ho_RegionFillUp1.Dispose(); HOperatorSet.FillUpShape(ho_SelectedRegions, out ho_RegionFillUp1, "area", 1, 500); ho_RegionErosion.Dispose(); HOperatorSet.ErosionCircle(ho_RegionFillUp1, out ho_RegionErosion, 2.5); ho_Contours1.Dispose(); HOperatorSet.GenContourRegionXld(ho_RegionErosion, out ho_Contours1, "border_holes"); HOperatorSet.CountObj(ho_Contours1, out hv_Number2); #endregion HOperatorSet.QueryFont(Window, out hv_Font); HOperatorSet.SetFont(Window, (hv_Font.TupleSelect(0)) + "-Bold-13"); #region//计算点胶区域内外圆 //*********************** //理论胶圈外圆 hv_glueOutterCircle = hv_glueInnerCircle + hv_glueWidth; //理论胶圈圆心 hv_glueCenter = new HTuple(2); hv_glueCenter[0] = CenterLocate.CenterLoc[1]; hv_glueCenter[1] = CenterLocate.CenterLoc[0]; HObject ho_Cross, ho_cir1, ho_cir2; HOperatorSet.GenEmptyObj(out ho_Cross); HOperatorSet.GenEmptyObj(out ho_cir1); HOperatorSet.GenEmptyObj(out ho_cir2); //xmz 显示 内外圆区域 HOperatorSet.SetColor(Window, "green"); HOperatorSet.SetDraw(Window, "margin");; ho_Cross.Dispose();; HOperatorSet.GenCrossContourXld(out ho_Cross, CenterLocate.CenterLoc[1], CenterLocate.CenterLoc[0], 180, 0.785398); HOperatorSet.DispObj(ho_ImageF, Window); HOperatorSet.DispObj(ho_Cross, Window); ho_cir1.Dispose(); HOperatorSet.GenCircle(out ho_cir1, CenterLocate.CenterLoc[1], CenterLocate.CenterLoc[0], hv_glueInnerCircle); HOperatorSet.DispObj(ho_cir1, Window); ho_cir2.Dispose(); HOperatorSet.GenCircle(out ho_cir2, CenterLocate.CenterLoc[1], CenterLocate.CenterLoc[0], hv_glueOutterCircle); HOperatorSet.DispObj(ho_cir2, Window); //*********************** #endregion #region//检测胶圈是否封闭 if ((int)((new HTuple((new HTuple(hv_Number2.TupleEqual(2))).TupleAnd(new HTuple(hv_Number1.TupleEqual( 1))))).TupleNot()) != 0) { is_qualified = false; //HOperatorSet.DispObj(ho_ImageF, Window); HOperatorSet.SetLineWidth(Window, 2); HOperatorSet.SetColor(Window, "red"); HOperatorSet.DispObj(ho_Contours1, Window); HOperatorSet.DispText(Window, "无胶或胶圈不完整NG", "window", 15, 480, "red", "box", "false"); SetString(Window, "NG", "red", 100); #region ho_ImageT.Dispose(); ho_ImageF.Dispose(); ho_TransImage.Dispose(); ho_ImageSub.Dispose(); ho_ImageGauss.Dispose(); ho_Regions_R.Dispose(); ho_RegionClosing.Dispose(); ho_RegionOpening1.Dispose(); ho_RegionUnion.Dispose(); ho_Rectangle.Dispose(); ho_RegionDifference.Dispose(); ho_RegionOpening.Dispose(); ho_Regionc.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_RegionFillUp1.Dispose(); ho_RegionErosion.Dispose(); ho_Contours1.Dispose(); ho_Circle_mask_S.Dispose(); ho_target_ring.Dispose(); ho_target_ring_scale.Dispose(); ho_Regions.Dispose(); ho_RegionFillUp.Dispose(); ho_RegionTrans.Dispose(); ho_Contours.Dispose(); ho_glueOutterContour.Dispose(); ho_glueInnerContour.Dispose(); ho_Contour_I_sap.Dispose(); ho_Contour_O_sap.Dispose(); ho_ContCircle_outter.Dispose(); ho_ContCircle_Inner.Dispose(); #endregion return(is_qualified); } #endregion #region//胶圈封闭则进一步检查其它指标是否合格 else { #region ho_RegionClosing.Dispose(); HOperatorSet.ClosingCircle(ho_Regions_R, out ho_RegionClosing, kernel); ho_RegionOpening1.Dispose(); HOperatorSet.OpeningCircle(ho_RegionClosing, out ho_RegionOpening1, 15); ho_RegionUnion.Dispose(); HOperatorSet.Union1(ho_RegionOpening1, out ho_RegionUnion); ho_Rectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle, 0, 0, hv_Height, hv_Width); ho_RegionDifference.Dispose(); HOperatorSet.Difference(ho_Rectangle, ho_RegionUnion, out ho_RegionDifference ); ho_RegionOpening.Dispose(); HOperatorSet.OpeningCircle(ho_RegionDifference, out ho_RegionOpening, 7); ho_Regionc.Dispose(); HOperatorSet.ClosingCircle(ho_RegionOpening, out ho_Regionc, 9); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_Regionc, out ho_ConnectedRegions); ho_SelectedRegions.Dispose(); HOperatorSet.SelectShapeStd(ho_ConnectedRegions, out ho_SelectedRegions, "max_area", 70); ho_RegionFillUp1.Dispose(); HOperatorSet.FillUpShape(ho_SelectedRegions, out ho_RegionFillUp1, "area", 1, 20000); ho_RegionErosion.Dispose(); HOperatorSet.ErosionCircle(ho_RegionFillUp1, out ho_RegionErosion, 2.5); ho_Contours.Dispose(); HOperatorSet.GenContourRegionXld(ho_RegionErosion, out ho_Contours, "border_holes"); ho_glueOutterContour.Dispose(); HOperatorSet.SelectObj(ho_Contours, out ho_glueOutterContour, 1); ho_glueInnerContour.Dispose(); HOperatorSet.SelectObj(ho_Contours, out ho_glueInnerContour, 2); HOperatorSet.GetContourXld(ho_glueInnerContour, out hv_Row2, out hv_Col1); HOperatorSet.GetContourXld(ho_glueOutterContour, out hv_Row3, out hv_Col2); hv_Row_Inner_sampling = new HTuple(); hv_Col_Inner_sampling = new HTuple(); hv_k = 0; for (hv_i = 0; (int)hv_i <= (int)((new HTuple(hv_Row2.TupleLength())) - 1); hv_i = (int)hv_i + 50) { if (hv_Row_Inner_sampling == null) { hv_Row_Inner_sampling = new HTuple(); } hv_Row_Inner_sampling[hv_k] = hv_Row2.TupleSelect(hv_i); if (hv_Col_Inner_sampling == null) { hv_Col_Inner_sampling = new HTuple(); } hv_Col_Inner_sampling[hv_k] = hv_Col1.TupleSelect(hv_i); hv_k = hv_k + 1; } if (hv_Row_Inner_sampling == null) { hv_Row_Inner_sampling = new HTuple(); } hv_Row_Inner_sampling[new HTuple(hv_Row_Inner_sampling.TupleLength())] = hv_Row_Inner_sampling.TupleSelect( 0); if (hv_Col_Inner_sampling == null) { hv_Col_Inner_sampling = new HTuple(); } hv_Col_Inner_sampling[new HTuple(hv_Col_Inner_sampling.TupleLength())] = hv_Col_Inner_sampling.TupleSelect( 0); hv_Row_Outter_sampling = new HTuple(); hv_Col_Outter_sampling = new HTuple(); hv_k = 0; for (hv_i = 0; (int)hv_i <= (int)((new HTuple(hv_Row3.TupleLength())) - 1); hv_i = (int)hv_i + 50) { if (hv_Row_Outter_sampling == null) { hv_Row_Outter_sampling = new HTuple(); } hv_Row_Outter_sampling[hv_k] = hv_Row3.TupleSelect(hv_i); if (hv_Col_Outter_sampling == null) { hv_Col_Outter_sampling = new HTuple(); } hv_Col_Outter_sampling[hv_k] = hv_Col2.TupleSelect(hv_i); hv_k = hv_k + 1; } if (hv_Row_Outter_sampling == null) { hv_Row_Outter_sampling = new HTuple(); } hv_Row_Outter_sampling[new HTuple(hv_Row_Outter_sampling.TupleLength())] = hv_Row_Outter_sampling.TupleSelect( 0); if (hv_Col_Outter_sampling == null) { hv_Col_Outter_sampling = new HTuple(); } hv_Col_Outter_sampling[new HTuple(hv_Col_Outter_sampling.TupleLength())] = hv_Col_Outter_sampling.TupleSelect( 0); ho_Contour_I_sap.Dispose(); HOperatorSet.GenContourPolygonXld(out ho_Contour_I_sap, hv_Row_Inner_sampling, hv_Col_Inner_sampling); ho_Contour_O_sap.Dispose(); HOperatorSet.GenContourPolygonXld(out ho_Contour_O_sap, hv_Row_Outter_sampling, hv_Col_Outter_sampling); ho_glueInnerContour.Dispose(); ho_glueInnerContour = ho_Contour_I_sap.CopyObj(1, -1); ho_glueOutterContour.Dispose(); ho_glueOutterContour = ho_Contour_O_sap.CopyObj(1, -1); //*最大最小胶宽 HOperatorSet.DistancePc(ho_glueOutterContour, hv_Row2, hv_Col1, out hv_DistanceMin, out hv_DistanceMax); HOperatorSet.TupleMin(hv_DistanceMin, out hv_glueWidthMin); HOperatorSet.TupleMax(hv_DistanceMin, out hv_glueWidthMax); //*胶水内轮廓离边界的距离最大值最小值 HOperatorSet.DistancePc(ho_glueInnerContour, hv_glueCenter.TupleSelect(0), hv_glueCenter.TupleSelect(1), out hv_DistanceMin1, out hv_DistanceMax1); hv_Distance_inner_min = hv_DistanceMin1 - hv_glueInnerCircle; hv_Distance_inner_max = hv_DistanceMax1 - hv_glueInnerCircle; //*胶水外轮廓离边界的距离最大最小值 HOperatorSet.DistancePc(ho_glueOutterContour, hv_glueCenter.TupleSelect(0), hv_glueCenter.TupleSelect(1), out hv_DistanceMin2, out hv_DistanceMax2); hv_Distance_outter_min = hv_DistanceMin2 - hv_glueOutterCircle; hv_Distance_outter_max = hv_DistanceMax2 - hv_glueOutterCircle; //*用胶轮廓与拟合圆间的最大距离判断缺胶溢胶情况 HOperatorSet.FitCircleContourXld(ho_glueOutterContour, "algebraic", -1, 0, 0, 3, 2, out hv_Row_Outter, out hv_Column_Outter, out hv_Radius_Outter, out hv_StartPhi1, out hv_EndPhi1, out hv_PointOrder1); ho_ContCircle_outter.Dispose(); HOperatorSet.GenCircleContourXld(out ho_ContCircle_outter, hv_Row_Outter, hv_Column_Outter, hv_Radius_Outter, 0, 6.28318, "positive", 1); HOperatorSet.DistancePc(ho_glueOutterContour, hv_Row_Outter, hv_Column_Outter, out hv_DistanceMin_Outter, out hv_DistanceMax_Outter); hv_Ddistance_Outter = hv_DistanceMax_Outter - hv_Radius_Outter; hv_Sdistance_Outter = hv_Radius_Outter - hv_DistanceMin_Outter; HOperatorSet.FitCircleContourXld(ho_glueInnerContour, "algebraic", -1, 0, 0, 3, 2, out hv_Row_Inner, out hv_Column_Inner, out hv_Radius_Inner, out hv_StartPhi2, out hv_EndPhi2, out hv_PointOrder2); ho_ContCircle_Inner.Dispose(); HOperatorSet.GenCircleContourXld(out ho_ContCircle_Inner, hv_Row_Inner, hv_Column_Inner, hv_Radius_Inner, 0, 6.28318, "positive", 1); HOperatorSet.DistancePc(ho_glueInnerContour, hv_Row_Inner, hv_Column_Inner, out hv_DistanceMin_Inner, out hv_DistanceMax_Inner); hv_Ddistance_Inner = hv_Radius_Inner - hv_DistanceMin_Inner; hv_Sdistance_Inner = hv_DistanceMax_Inner - hv_Radius_Inner; //*利用拟合内外胶的圆心的平均值与拟合基准圆的圆心坐标判断偏移情况 hv_Row_glue = (hv_Row_Outter + hv_Row_Inner) / 2; hv_Col_glue = (hv_Column_Outter + hv_Column_Inner) / 2; HOperatorSet.DistancePp(hv_glueCenter.TupleSelect(0), hv_glueCenter.TupleSelect( 1), hv_Row_glue, hv_Col_glue, out hv_Distance_offset); //*算出拟合胶水的宽度 hv_Radius = hv_Radius_Outter - hv_Radius_Inner; //平滑轮廓 HOperatorSet.SmoothContoursXld(ho_glueOutterContour, out SmoothedGlueOutterContour, 5); HOperatorSet.SmoothContoursXld(ho_glueInnerContour, out SmoothedGlueInnerContour, 5); //HOperatorSet.WriteString() #endregion if ((int)(new HTuple(hv_Distance_offset.TupleGreater(glueOffset_))) != 0) { is_qualified = false; HOperatorSet.SetColor(Window, "red"); HOperatorSet.SetLineWidth(Window, 2); HOperatorSet.DispObj(ho_ImageF, Window); HOperatorSet.DispObj(ho_glueOutterContour, Window); HOperatorSet.DispObj(ho_glueInnerContour, Window); //**显示 HOperatorSet.DispText(Window, (("胶离内边界最大 / 最小距离:" + hv_Distance_inner_max) + " / ") + hv_Distance_inner_min, "window", 15, 7, "green", "box", "false"); HOperatorSet.DispText(Window, (("胶离外边界最大 / 最小距离:" + hv_Distance_outter_max) + " / ") + hv_Distance_outter_min, "window", 40, 7, "green", "box", "false"); HOperatorSet.DispText(Window, "内外胶拟合圆宽度:" + hv_Radius, "window", 65, 7, "green", "box", "false"); HOperatorSet.DispText(Window, "最小胶宽度:" + hv_glueWidthMin, "window", 90, 7, "green", "box", "false"); HOperatorSet.DispText(Window, "最大胶宽度:" + hv_glueWidthMax, "window", 115, 7, "green", "box", "false"); HOperatorSet.DispText(Window, "胶偏移距离:" + hv_Distance_offset, "window", 140, 7, "green", "box", "false"); HOperatorSet.DispText(Window, "标准胶宽:" + hv_glueWidth, "window", 165, 7, "green", "box", "false"); HOperatorSet.DispText(Window, "胶圈偏移NG", "window", 40, 400, "red", "box", "false"); SetString(Window, "NG", "red", 100);; #region//释放图像变量 ho_ImageT.Dispose(); ho_ImageF.Dispose(); ho_TransImage.Dispose(); ho_ImageSub.Dispose(); ho_ImageGauss.Dispose(); ho_Regions_R.Dispose(); ho_RegionClosing.Dispose(); ho_RegionOpening1.Dispose(); ho_RegionUnion.Dispose(); ho_Rectangle.Dispose(); ho_RegionDifference.Dispose(); ho_RegionOpening.Dispose(); ho_Regionc.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_RegionFillUp1.Dispose(); ho_RegionErosion.Dispose(); ho_Contours1.Dispose(); ho_Circle_mask_S.Dispose(); ho_target_ring.Dispose(); ho_target_ring_scale.Dispose(); ho_Regions.Dispose(); ho_RegionFillUp.Dispose(); ho_RegionTrans.Dispose(); ho_Contours.Dispose(); ho_glueOutterContour.Dispose(); ho_glueInnerContour.Dispose(); ho_Contour_I_sap.Dispose(); ho_Contour_O_sap.Dispose(); ho_ContCircle_outter.Dispose(); ho_ContCircle_Inner.Dispose(); #endregion return(is_qualified); } else { HOperatorSet.SetColor(Window, "green"); HOperatorSet.SetLineWidth(Window, 2); HOperatorSet.DispObj(ho_ImageF, Window); HOperatorSet.DispObj(ho_glueOutterContour, Window); HOperatorSet.DispObj(ho_glueInnerContour, Window); if ((int)(new HTuple(hv_Distance_outter_max.TupleGreater(glueoverflow_Outter))) != 0) { HOperatorSet.DispText(Window, "外胶溢胶!NG", "window", 15, 400, "red", "box", "false"); is_qualified = false; } if ((int)(new HTuple(((hv_Distance_outter_min.TupleAbs())).TupleGreater(glueLack_Outter))) != 0) { HOperatorSet.DispText(Window, "外胶缺胶!NG", "window", 40, 400, "red", "box", "false"); is_qualified = false; } if ((int)(new HTuple(((hv_Distance_inner_min.TupleAbs())).TupleGreater(glueoverflow_Inner))) != 0) { HOperatorSet.DispText(Window, "内胶溢胶!NG", "window", 65, 400, "red", "box", "false"); is_qualified = false; } if ((int)(new HTuple(hv_Distance_inner_max.TupleGreater(glueLack_Inner))) != 0) { HOperatorSet.DispText(Window, "内胶缺胶!NG", "window", 90, 400, "red", "box", "false"); is_qualified = false; } //if (hv_glueWidthMax > (hv_glueWidth + (glueOverflowOutter + glueOverflowInner) / 2.0)) //{ // HOperatorSet.DispText(Window, "最大胶宽度超出!NG", "window", 115, 400, "red", "box", "false"); // is_qualified = false; //} //if (hv_glueWidthMin < (hv_glueWidth - (glueLackOutter + glueLackInner) / 2.0)) //{ // HOperatorSet.DispText(Window, "最小胶宽度超出!NG", "window", 140, 400, "red", "box", "false"); // is_qualified = false; //} //**显示 HOperatorSet.DispText(Window, (("胶离内边界最大 / 最小距离:" + hv_Distance_inner_max) + " / ") + hv_Distance_inner_min, "window", 15, 7, "green", "box", "false"); HOperatorSet.DispText(Window, (("胶离外边界最大 / 最小距离:" + hv_Distance_outter_max) + " / ") + hv_Distance_outter_min, "window", 40, 7, "green", "box", "false"); HOperatorSet.DispText(Window, "内外胶拟合圆宽度:" + hv_Radius, "window", 65, 7, "green", "box", "false"); HOperatorSet.DispText(Window, "最小胶宽度:" + hv_glueWidthMin, "window", 90, 7, "green", "box", "false"); HOperatorSet.DispText(Window, "最大胶宽度:" + hv_glueWidthMax, "window", 115, 7, "green", "box", "false"); HOperatorSet.DispText(Window, "胶偏移距离:" + hv_Distance_offset, "window", 140, 7, "green", "box", "false"); HOperatorSet.DispText(Window, "标准胶宽:" + hv_glueWidth, "window", 165, 7, "green", "box", "false"); if (is_qualified) { SetString(Window, "OK", "green", 100); } else { SetString(Window, "NG", "red", 100); } #region//释放图像变量 ho_ImageT.Dispose(); ho_ImageF.Dispose(); ho_TransImage.Dispose(); ho_ImageSub.Dispose(); ho_ImageGauss.Dispose(); ho_Regions_R.Dispose(); ho_RegionClosing.Dispose(); ho_RegionOpening1.Dispose(); ho_RegionUnion.Dispose(); ho_Rectangle.Dispose(); ho_RegionDifference.Dispose(); ho_RegionOpening.Dispose(); ho_Regionc.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_RegionFillUp1.Dispose(); ho_RegionErosion.Dispose(); ho_Contours1.Dispose(); ho_Circle_mask_S.Dispose(); ho_target_ring.Dispose(); ho_target_ring_scale.Dispose(); ho_Regions.Dispose(); ho_RegionFillUp.Dispose(); ho_RegionTrans.Dispose(); ho_Contours.Dispose(); ho_glueOutterContour.Dispose(); ho_glueInnerContour.Dispose(); ho_Contour_I_sap.Dispose(); ho_Contour_O_sap.Dispose(); ho_ContCircle_outter.Dispose(); ho_ContCircle_Inner.Dispose(); #endregion return(is_qualified); } } #endregion } catch (HalconException ex) { SetString(Window, "NG", "red", 100); HOperatorSet.DispText(Window, ex.GetErrorMessage(), "window", 100, 0, "red", new HTuple(), new HTuple()); return(false); } }
// Main procedure private HTuple action() { // Local iconic variables HObject ho_Image, ho_ImageRotate, ho_GrayImage; HObject ho_ImageMedian, ho_Region, ho_ConnectedRegions; HObject ho_SelectedRegions, ho_SelectedRegions2, ho_SelectedRegions1; HObject ho_SelectedRegions3, ho_SortedRegions, ho_RegionUnion; HObject ho_Rectangle, ho_ImageReduced, ho_GrayImageA, ho_GragImageA; HObject ho_RegionsA_temp, ho_ConnectedRegionsA_temp, ho_SelectedRegionsA_temp; HObject ho_ImageAffineTrans, ho_RegionsA, ho_RegionDilationA; HObject ho_ConnectedRegionsA, ho_SelectedRegionsA, ho_RegionsA1; HObject ho_RegionOpeningA, ho_SelectedRegionsA1, ho_RegionUnionA; HObject ho_SelectedRegionsA2, ho_RegionsB, ho_ConnectedRegionsB; HObject ho_SelectedRegionsB, ho_RectangleB1, ho_RectangleB2; HObject ho_RegionDifferenceB, ho_ImageReducedB1, ho_RegionB1; HObject ho_ConnectedRegionsB2, ho_SelectedRegionsB2, ho_outContours; HObject ho_SelectedContours, ho_SortedContours, ho_Region1; HObject ho_ConnectedRegions1, ho_SelectedRegions4, ho_SelectedRegions5; HObject ho_SelectedRegions6, ho_Contours, ho_SortedContours3; HObject ho_obj = null, ho_objj = null, ho_ContoursSplit = null; HObject ho_SelectedXLD = null, ho_SortedContours1 = null, ho_M = null; HObject ho_Contour = null, ho_SelectedXLD1 = null, ho_SortedContours2 = null; HObject ho_ObjectSelectedB = null; // Local control variables HTuple hv_NumberB = null, hv_Row1 = null, hv_Column1 = null; HTuple hv_Phi = null, hv_Length1 = null, hv_Length2 = null; HTuple hv_Phi_temp = null, hv_Area_temp = null, hv_Row_temp = null; HTuple hv_Column_temp = null, hv_HomMat2D = null, hv_RowA1 = null; HTuple hv_ColumnA1 = null, hv_RowA2 = null, hv_ColumnA2 = null; HTuple hv_RowA3 = null, hv_ColumnA3 = null, hv_RowA4 = null; HTuple hv_ColumnA4 = null, hv_RowB16 = null, hv_ColumnB16 = null; HTuple hv_PhiB3 = null, hv_LengthB1 = null, hv_LengthB2 = null; HTuple hv_AreaB = null, hv_RowB2 = null, hv_ColumnB2 = null; HTuple hv_Number = null, hv_Number3 = null, hv_A1 = null; HTuple hv_A2 = null, hv_B1 = null, hv_B2 = null, hv_C1 = null; HTuple hv_C2 = null, hv_C3 = null, hv_C4 = null, hv_i = null; HTuple hv_Ro1 = new HTuple(), hv_Co1 = new HTuple(), hv_MinBR = new HTuple(); HTuple hv_MaxBR = new HTuple(), hv_MinBC = new HTuple(); HTuple hv_MaxBC = new HTuple(), hv_BR = new HTuple(), hv_BC = new HTuple(); HTuple hv_j = new HTuple(), hv_Number1 = new HTuple(); HTuple hv_a = new HTuple(), hv_Ro2 = new HTuple(), hv_Co2 = new HTuple(); HTuple hv_LUC = new HTuple(), hv_RUC = new HTuple(), hv_Ro3 = new HTuple(); HTuple hv_Co3 = new HTuple(), hv_LDC = new HTuple(), hv_RDC = new HTuple(); HTuple hv_RowBegin = new HTuple(), hv_ColBegin = new HTuple(); HTuple hv_RowEnd = new HTuple(), hv_ColEnd = new HTuple(); HTuple hv_Nr = new HTuple(), hv_Nc = new HTuple(), hv_Dist = new HTuple(); HTuple hv_Rowc = new HTuple(), hv_Colc = new HTuple(); HTuple hv_MeanR = new HTuple(), hv_H1 = new HTuple(), hv_H3 = new HTuple(); HTuple hv_Number2 = new HTuple(), hv_b = new HTuple(); HTuple hv_Ro4 = new HTuple(), hv_Co4 = new HTuple(), hv_LUR = new HTuple(); HTuple hv_LDR = new HTuple(), hv_Ro5 = new HTuple(), hv_Co5 = new HTuple(); HTuple hv_RUR = new HTuple(), hv_RDR = new HTuple(), hv_MeanC = new HTuple(); HTuple hv_H4 = new HTuple(), hv_H2 = new HTuple(), hv_Distance1 = new HTuple(); HTuple hv_Distance2 = new HTuple(), hv_AreaA = null, hv_RowA = null; HTuple hv_ColumnA = null, hv_phi = null, hv_paozhengwei = null; HTuple hv_k = null, hv_Indices = null, hv_liebo = null; HTuple hv_NumB = null, hv_count = null, hv_I = new HTuple(); HTuple hv_AreaB1 = new HTuple(), hv_RowB3 = new HTuple(); HTuple hv_ColumnB3 = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_ImageRotate); HOperatorSet.GenEmptyObj(out ho_GrayImage); HOperatorSet.GenEmptyObj(out ho_ImageMedian); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions2); HOperatorSet.GenEmptyObj(out ho_SelectedRegions1); HOperatorSet.GenEmptyObj(out ho_SelectedRegions3); HOperatorSet.GenEmptyObj(out ho_SortedRegions); HOperatorSet.GenEmptyObj(out ho_RegionUnion); HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_GrayImageA); HOperatorSet.GenEmptyObj(out ho_GragImageA); HOperatorSet.GenEmptyObj(out ho_RegionsA_temp); HOperatorSet.GenEmptyObj(out ho_ConnectedRegionsA_temp); HOperatorSet.GenEmptyObj(out ho_SelectedRegionsA_temp); HOperatorSet.GenEmptyObj(out ho_ImageAffineTrans); HOperatorSet.GenEmptyObj(out ho_RegionsA); HOperatorSet.GenEmptyObj(out ho_RegionDilationA); HOperatorSet.GenEmptyObj(out ho_ConnectedRegionsA); HOperatorSet.GenEmptyObj(out ho_SelectedRegionsA); HOperatorSet.GenEmptyObj(out ho_RegionsA1); HOperatorSet.GenEmptyObj(out ho_RegionOpeningA); HOperatorSet.GenEmptyObj(out ho_SelectedRegionsA1); HOperatorSet.GenEmptyObj(out ho_RegionUnionA); HOperatorSet.GenEmptyObj(out ho_SelectedRegionsA2); HOperatorSet.GenEmptyObj(out ho_RegionsB); HOperatorSet.GenEmptyObj(out ho_ConnectedRegionsB); HOperatorSet.GenEmptyObj(out ho_SelectedRegionsB); HOperatorSet.GenEmptyObj(out ho_RectangleB1); HOperatorSet.GenEmptyObj(out ho_RectangleB2); HOperatorSet.GenEmptyObj(out ho_RegionDifferenceB); HOperatorSet.GenEmptyObj(out ho_ImageReducedB1); HOperatorSet.GenEmptyObj(out ho_RegionB1); HOperatorSet.GenEmptyObj(out ho_ConnectedRegionsB2); HOperatorSet.GenEmptyObj(out ho_SelectedRegionsB2); HOperatorSet.GenEmptyObj(out ho_outContours); HOperatorSet.GenEmptyObj(out ho_SelectedContours); HOperatorSet.GenEmptyObj(out ho_SortedContours); HOperatorSet.GenEmptyObj(out ho_Region1); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1); HOperatorSet.GenEmptyObj(out ho_SelectedRegions4); HOperatorSet.GenEmptyObj(out ho_SelectedRegions5); HOperatorSet.GenEmptyObj(out ho_SelectedRegions6); HOperatorSet.GenEmptyObj(out ho_Contours); HOperatorSet.GenEmptyObj(out ho_SortedContours3); HOperatorSet.GenEmptyObj(out ho_obj); HOperatorSet.GenEmptyObj(out ho_objj); HOperatorSet.GenEmptyObj(out ho_ContoursSplit); HOperatorSet.GenEmptyObj(out ho_SelectedXLD); HOperatorSet.GenEmptyObj(out ho_SortedContours1); HOperatorSet.GenEmptyObj(out ho_M); HOperatorSet.GenEmptyObj(out ho_Contour); HOperatorSet.GenEmptyObj(out ho_SelectedXLD1); HOperatorSet.GenEmptyObj(out ho_SortedContours2); HOperatorSet.GenEmptyObj(out ho_ObjectSelectedB); ho_Image.Dispose(); if (!this.index) { HOperatorSet.ReadImage(out ho_Image, "test.bmp"); } else { HOperatorSet.ReadImage(out ho_Image, "PersonImg\\xiaosy.png"); } //*******************************************第一组预处理*********************************************** ho_ImageRotate.Dispose(); HOperatorSet.RotateImage(ho_Image, out ho_ImageRotate, 45, "constant"); ho_GrayImage.Dispose(); HOperatorSet.Rgb1ToGray(ho_ImageRotate, out ho_GrayImage); ho_ImageMedian.Dispose(); HOperatorSet.MedianImage(ho_GrayImage, out ho_ImageMedian, "circle", 3, "mirrored"); ho_Region.Dispose(); HOperatorSet.Threshold(ho_GrayImage, out ho_Region, 200, 255); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions); ho_SelectedRegions.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area", "and", 150, 99999); ho_SelectedRegions2.Dispose(); HOperatorSet.SelectShape(ho_SelectedRegions, out ho_SelectedRegions2, "circularity", "and", 0.37, 0.7); ho_SelectedRegions1.Dispose(); HOperatorSet.ClosingRectangle1(ho_SelectedRegions2, out ho_SelectedRegions1, 5, 5); ho_SelectedRegions3.Dispose(); HOperatorSet.SelectShape(ho_SelectedRegions1, out ho_SelectedRegions3, "holes_num", "and", 1, 99); ho_SortedRegions.Dispose(); HOperatorSet.SortRegion(ho_SelectedRegions3, out ho_SortedRegions, "first_point", "true", "column"); HOperatorSet.CountObj(ho_SelectedRegions3, out hv_NumberB); ho_RegionUnion.Dispose(); HOperatorSet.Union1(ho_SelectedRegions3, out ho_RegionUnion); HOperatorSet.SmallestRectangle2(ho_RegionUnion, out hv_Row1, out hv_Column1, out hv_Phi, out hv_Length1, out hv_Length2); ho_Rectangle.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle, hv_Row1, hv_Column1, hv_Phi, hv_Length1, hv_Length2); ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_GrayImage, ho_Rectangle, out ho_ImageReduced); //*******************************************第二组预处理*********************************************** ho_GrayImageA.Dispose(); HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImageA); ho_GragImageA.Dispose(); HOperatorSet.GrayDilationRect(ho_GrayImageA, out ho_GragImageA, 4, 4); ho_RegionsA_temp.Dispose(); HOperatorSet.Threshold(ho_GrayImageA, out ho_RegionsA_temp, 116, 218); ho_ConnectedRegionsA_temp.Dispose(); HOperatorSet.Connection(ho_RegionsA_temp, out ho_ConnectedRegionsA_temp); ho_SelectedRegionsA_temp.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegionsA_temp, out ho_SelectedRegionsA_temp, "area", "and", 108357, 495042); HOperatorSet.OrientationRegion(ho_SelectedRegionsA_temp, out hv_Phi_temp); HOperatorSet.AreaCenter(ho_SelectedRegionsA_temp, out hv_Area_temp, out hv_Row_temp, out hv_Column_temp); HOperatorSet.VectorAngleToRigid(hv_Row_temp, hv_Column_temp, hv_Phi_temp, hv_Row_temp, hv_Column_temp, (new HTuple(0)).TupleRad(), out hv_HomMat2D); ho_ImageAffineTrans.Dispose(); HOperatorSet.AffineTransImage(ho_GrayImageA, out ho_ImageAffineTrans, hv_HomMat2D, "constant", "false"); ho_RegionsA.Dispose(); HOperatorSet.Threshold(ho_ImageAffineTrans, out ho_RegionsA, 116, 218); ho_RegionDilationA.Dispose(); HOperatorSet.DilationRectangle1(ho_RegionsA, out ho_RegionDilationA, 2, 2); ho_ConnectedRegionsA.Dispose(); HOperatorSet.Connection(ho_RegionDilationA, out ho_ConnectedRegionsA); ho_SelectedRegionsA.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegionsA, out ho_SelectedRegionsA, "circularity", "and", 0.001, 0.03); ho_RegionsA1.Dispose(); HOperatorSet.Threshold(ho_GrayImageA, out ho_RegionsA1, 227, 255); ho_RegionOpeningA.Dispose(); HOperatorSet.OpeningCircle(ho_RegionsA1, out ho_RegionOpeningA, 5.5); ho_ConnectedRegionsA.Dispose(); HOperatorSet.Connection(ho_RegionOpeningA, out ho_ConnectedRegionsA); ho_SelectedRegionsA1.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegionsA, out ho_SelectedRegionsA1, "row", "and", 513.2, 601.23); ho_RegionUnionA.Dispose(); HOperatorSet.Union1(ho_SelectedRegionsA1, out ho_RegionUnionA); HOperatorSet.SmallestRectangle1(ho_RegionUnionA, out hv_RowA1, out hv_ColumnA1, out hv_RowA2, out hv_ColumnA2); ho_SelectedRegionsA2.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegionsA, out ho_SelectedRegionsA2, "width", "and", 92.694, 100); HOperatorSet.InnerRectangle1(ho_SelectedRegionsA2, out hv_RowA3, out hv_ColumnA3, out hv_RowA4, out hv_ColumnA4); ho_ConnectedRegionsA.Dispose(); HOperatorSet.Connection(ho_RegionsA1, out ho_ConnectedRegionsA); //*******************************************第三组预处理********************************************* ho_RegionsB.Dispose(); HOperatorSet.Threshold(ho_Image, out ho_RegionsB, 131, 153); ho_ConnectedRegionsB.Dispose(); HOperatorSet.Connection(ho_RegionsB, out ho_ConnectedRegionsB); ho_SelectedRegionsB.Dispose(); HOperatorSet.SelectShapeStd(ho_ConnectedRegionsB, out ho_SelectedRegionsB, "max_area", 70); HOperatorSet.SmallestRectangle2(ho_SelectedRegionsB, out hv_RowB16, out hv_ColumnB16, out hv_PhiB3, out hv_LengthB1, out hv_LengthB2); ho_RectangleB1.Dispose(); HOperatorSet.GenRectangle2(out ho_RectangleB1, hv_RowB16 - 5, hv_ColumnB16, hv_PhiB3, hv_LengthB1 - 5, hv_LengthB2 - 5); ho_RectangleB2.Dispose(); HOperatorSet.GenRectangle2(out ho_RectangleB2, hv_RowB16 - 3, hv_ColumnB16, hv_PhiB3, hv_LengthB1 - 5, hv_LengthB2 - 18); ho_RegionDifferenceB.Dispose(); HOperatorSet.Difference(ho_RectangleB1, ho_RectangleB2, out ho_RegionDifferenceB ); ho_ImageReducedB1.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_RegionDifferenceB, out ho_ImageReducedB1 ); ho_RegionB1.Dispose(); HOperatorSet.Threshold(ho_ImageReducedB1, out ho_RegionB1, 200, 255); ho_ConnectedRegionsB2.Dispose(); HOperatorSet.Connection(ho_RegionB1, out ho_ConnectedRegionsB2); ho_SelectedRegionsB2.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegionsB2, out ho_SelectedRegionsB2, "area", "and", 26.01, 200); HOperatorSet.AreaCenter(ho_SelectedRegionsB2, out hv_AreaB, out hv_RowB2, out hv_ColumnB2); //*****************************************花瓣ABC************************************************** //边缘轮廓检测 ho_outContours.Dispose(); HOperatorSet.GenContourRegionXld(ho_SelectedRegions3, out ho_outContours, "border"); ho_SelectedContours.Dispose(); HOperatorSet.SelectContoursXld(ho_outContours, out ho_SelectedContours, "contour_length", 60, 600, -0.5, 0.5); ho_SortedContours.Dispose(); HOperatorSet.SortContoursXld(ho_SelectedContours, out ho_SortedContours, "upper_left", "true", "column"); HOperatorSet.CountObj(ho_SortedContours, out hv_Number); //中心轮廓检测 ho_Region1.Dispose(); HOperatorSet.Threshold(ho_ImageReduced, out ho_Region1, 0, 135); ho_ConnectedRegions1.Dispose(); HOperatorSet.Connection(ho_Region1, out ho_ConnectedRegions1); ho_SelectedRegions4.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions1, out ho_SelectedRegions4, "area", "and", 230, 600); ho_SelectedRegions5.Dispose(); HOperatorSet.SelectShape(ho_SelectedRegions4, out ho_SelectedRegions5, "ratio", "and", 0.7, 1.352); ho_SelectedRegions6.Dispose(); HOperatorSet.SelectShape(ho_SelectedRegions5, out ho_SelectedRegions6, "circularity", "and", 0.2, 1); ho_Contours.Dispose(); HOperatorSet.GenContourRegionXld(ho_SelectedRegions6, out ho_Contours, "border"); ho_SortedContours3.Dispose(); HOperatorSet.SortContoursXld(ho_Contours, out ho_SortedContours3, "upper_left", "true", "row"); HOperatorSet.CountObj(ho_SortedContours3, out hv_Number3); if ((int)(new HTuple((((hv_Number + hv_Number3) % 2)).TupleEqual(1))) != 0) { //这里需要在C#输出无法检测的信息 HDevelopStop(); } //里面的轮廓 A1左上到右下 A2左下到右上 hv_A1 = new HTuple(); hv_A2 = new HTuple(); //外面的轮廓 B1垂直方向 B2水平方向 hv_B1 = new HTuple(); hv_B2 = new HTuple(); //c1到c4顺时针顺序的四个花瓣长度 hv_C1 = new HTuple(); hv_C2 = new HTuple(); hv_C3 = new HTuple(); hv_C4 = new HTuple(); //B的检测 HTuple end_val103 = hv_Number; HTuple step_val103 = 1; for (hv_i = 1; hv_i.Continue(end_val103, step_val103); hv_i = hv_i.TupleAdd(step_val103)) { ho_obj.Dispose(); HOperatorSet.SelectObj(ho_SortedContours, out ho_obj, hv_i); HOperatorSet.GetContourXld(ho_obj, out hv_Ro1, out hv_Co1); HOperatorSet.TupleMin(hv_Ro1, out hv_MinBR); HOperatorSet.TupleMax(hv_Ro1, out hv_MaxBR); HOperatorSet.TupleMin(hv_Co1, out hv_MinBC); HOperatorSet.TupleMax(hv_Co1, out hv_MaxBC); hv_BR = hv_MaxBR - hv_MinBR; hv_BC = hv_MaxBC - hv_MinBC; hv_B1 = hv_B1.TupleConcat(hv_BR); hv_B2 = hv_B2.TupleConcat(hv_BC); //A的检测 HTuple end_val118 = hv_Number3; HTuple step_val118 = 1; for (hv_j = 1; hv_j.Continue(end_val118, step_val118); hv_j = hv_j.TupleAdd(step_val118)) { if ((int)(new HTuple(hv_i.TupleEqual(hv_j))) != 0) { ho_objj.Dispose(); HOperatorSet.SelectObj(ho_SortedContours3, out ho_objj, hv_j); //C的检测 ho_ContoursSplit.Dispose(); HOperatorSet.SegmentContoursXld(ho_objj, out ho_ContoursSplit, "lines", 5, 4, 2); ho_SelectedXLD.Dispose(); HOperatorSet.SelectShapeXld(ho_ContoursSplit, out ho_SelectedXLD, "width", "and", 13, 99); ho_SortedContours1.Dispose(); HOperatorSet.SortContoursXld(ho_SelectedXLD, out ho_SortedContours1, "upper_left", "true", "row"); HOperatorSet.CountObj(ho_SortedContours1, out hv_Number1); HTuple end_val130 = hv_Number1; HTuple step_val130 = 1; for (hv_a = 1; hv_a.Continue(end_val130, step_val130); hv_a = hv_a.TupleAdd(step_val130)) { ho_M.Dispose(); HOperatorSet.SelectObj(ho_SortedContours1, out ho_M, hv_a); if ((int)(new HTuple(hv_a.TupleEqual(1))) != 0) { HOperatorSet.GetContourXld(ho_M, out hv_Ro2, out hv_Co2); HOperatorSet.TupleMin(hv_Co2, out hv_LUC); HOperatorSet.TupleMax(hv_Co2, out hv_RUC); } else { HOperatorSet.GetContourXld(ho_M, out hv_Ro3, out hv_Co3); HOperatorSet.TupleMin(hv_Co3, out hv_LDC); HOperatorSet.TupleMax(hv_Co3, out hv_RDC); } HOperatorSet.FitLineContourXld(ho_M, "huber", -1, 0, 5, 2, out hv_RowBegin, out hv_ColBegin, out hv_RowEnd, out hv_ColEnd, out hv_Nr, out hv_Nc, out hv_Dist); ho_Contour.Dispose(); HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_RowBegin.TupleConcat( hv_RowEnd), hv_ColBegin.TupleConcat(hv_ColEnd)); HOperatorSet.GetContourXld(ho_Contour, out hv_Rowc, out hv_Colc); HOperatorSet.TupleMean(hv_Rowc, out hv_MeanR); if ((int)(new HTuple(hv_a.TupleEqual(1))) != 0) { hv_H1 = hv_MeanR - hv_MinBR; hv_C1 = hv_C1.TupleConcat(hv_H1); } else { hv_H3 = hv_MaxBR - hv_MeanR; hv_C3 = hv_C3.TupleConcat(hv_H3); } } ho_SelectedXLD1.Dispose(); HOperatorSet.SelectShapeXld(ho_ContoursSplit, out ho_SelectedXLD1, "height", "and", 13, 99); ho_SortedContours2.Dispose(); HOperatorSet.SortContoursXld(ho_SelectedXLD1, out ho_SortedContours2, "upper_left", "true", "column"); HOperatorSet.CountObj(ho_SortedContours2, out hv_Number2); HTuple end_val157 = hv_Number2; HTuple step_val157 = 1; for (hv_b = 1; hv_b.Continue(end_val157, step_val157); hv_b = hv_b.TupleAdd(step_val157)) { ho_M.Dispose(); HOperatorSet.SelectObj(ho_SortedContours2, out ho_M, hv_b); if ((int)(new HTuple(hv_b.TupleEqual(1))) != 0) { HOperatorSet.GetContourXld(ho_M, out hv_Ro4, out hv_Co4); HOperatorSet.TupleMin(hv_Ro4, out hv_LUR); HOperatorSet.TupleMax(hv_Ro4, out hv_LDR); } else { HOperatorSet.GetContourXld(ho_M, out hv_Ro5, out hv_Co5); HOperatorSet.TupleMin(hv_Ro5, out hv_RUR); HOperatorSet.TupleMax(hv_Ro5, out hv_RDR); } HOperatorSet.FitLineContourXld(ho_M, "huber", -1, 0, 5, 2, out hv_RowBegin, out hv_ColBegin, out hv_RowEnd, out hv_ColEnd, out hv_Nr, out hv_Nc, out hv_Dist); ho_Contour.Dispose(); HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_RowBegin.TupleConcat( hv_RowEnd), hv_ColBegin.TupleConcat(hv_ColEnd)); HOperatorSet.GetContourXld(ho_Contour, out hv_Rowc, out hv_Colc); HOperatorSet.TupleMean(hv_Colc, out hv_MeanC); if ((int)(new HTuple(hv_b.TupleEqual(1))) != 0) { hv_H4 = hv_MeanC - hv_MinBC; hv_C4 = hv_C4.TupleConcat(hv_H4); } else { hv_H2 = hv_MaxBC - hv_MeanC; hv_C2 = hv_C2.TupleConcat(hv_H2); } } HOperatorSet.DistancePp(hv_LUC, hv_LUR, hv_RDC, hv_RDR, out hv_Distance1); HOperatorSet.DistancePp(hv_RUC, hv_RUR, hv_LDC, hv_LDR, out hv_Distance2); hv_A1 = hv_A1.TupleConcat(hv_Distance1); hv_A2 = hv_A2.TupleConcat(hv_Distance2); } } } //*********************************倾斜角度 跑针位 k******************************************* HOperatorSet.AreaCenter(ho_SelectedRegionsA, out hv_AreaA, out hv_RowA, out hv_ColumnA); HOperatorSet.OrientationRegion(ho_SelectedRegionsA, out hv_Phi); hv_Phi = hv_Phi.TupleDeg(); hv_phi = ((90 - (hv_Phi.TupleAbs()))).TupleAbs(); hv_paozhengwei = (hv_RowA3 - ((hv_RowA2 / 2) + (hv_RowA1 / 2))) - 4; hv_k = (hv_RowA2 - hv_RowA1) + 4; //***************************************裂箔**************************************************** HOperatorSet.TupleSortIndex(hv_AreaB, out hv_Indices); hv_liebo = new HTuple(); hv_NumB = new HTuple(hv_Indices.TupleLength()); hv_count = 0; if ((int)(new HTuple(hv_NumB.TupleGreater(0))) != 0) { HTuple end_val208 = hv_NumB; HTuple step_val208 = 1; for (hv_I = 1; hv_I.Continue(end_val208, step_val208); hv_I = hv_I.TupleAdd(step_val208)) { ho_ObjectSelectedB.Dispose(); HOperatorSet.SelectObj(ho_SelectedRegionsB2, out ho_ObjectSelectedB, hv_I); HOperatorSet.AreaCenter(ho_ObjectSelectedB, out hv_AreaB1, out hv_RowB3, out hv_ColumnB3); if ((int)(new HTuple(hv_AreaB1.TupleGreater(30))) != 0) { hv_count = hv_count + 1; } else { } } } else { } if (hv_liebo == null) { hv_liebo = new HTuple(); } hv_liebo[0] = hv_count; HTuple result = new HTuple(); result.Append(hv_A1[0]); result.Append(hv_A2[0]); result.Append(hv_B1[0]); result.Append(hv_B2[0]); result.Append(hv_C1[0]); result.Append(hv_C2[0]); result.Append(hv_C3[0]); result.Append(hv_C4[0]); result.Append(hv_phi[0]); result.Append(hv_paozhengwei[0]); result.Append(hv_liebo[0]); //************************************************************************************************** ho_Image.Dispose(); ho_ImageRotate.Dispose(); ho_GrayImage.Dispose(); ho_ImageMedian.Dispose(); ho_Region.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_SelectedRegions2.Dispose(); ho_SelectedRegions1.Dispose(); ho_SelectedRegions3.Dispose(); ho_SortedRegions.Dispose(); ho_RegionUnion.Dispose(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_GrayImageA.Dispose(); ho_GragImageA.Dispose(); ho_RegionsA_temp.Dispose(); ho_ConnectedRegionsA_temp.Dispose(); ho_SelectedRegionsA_temp.Dispose(); ho_ImageAffineTrans.Dispose(); ho_RegionsA.Dispose(); ho_RegionDilationA.Dispose(); ho_ConnectedRegionsA.Dispose(); ho_SelectedRegionsA.Dispose(); ho_RegionsA1.Dispose(); ho_RegionOpeningA.Dispose(); ho_SelectedRegionsA1.Dispose(); ho_RegionUnionA.Dispose(); ho_SelectedRegionsA2.Dispose(); ho_RegionsB.Dispose(); ho_ConnectedRegionsB.Dispose(); ho_SelectedRegionsB.Dispose(); ho_RectangleB1.Dispose(); ho_RectangleB2.Dispose(); ho_RegionDifferenceB.Dispose(); ho_ImageReducedB1.Dispose(); ho_RegionB1.Dispose(); ho_ConnectedRegionsB2.Dispose(); ho_SelectedRegionsB2.Dispose(); ho_outContours.Dispose(); ho_SelectedContours.Dispose(); ho_SortedContours.Dispose(); ho_Region1.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions4.Dispose(); ho_SelectedRegions5.Dispose(); ho_SelectedRegions6.Dispose(); ho_Contours.Dispose(); ho_SortedContours3.Dispose(); ho_obj.Dispose(); ho_objj.Dispose(); ho_ContoursSplit.Dispose(); ho_SelectedXLD.Dispose(); ho_SortedContours1.Dispose(); ho_M.Dispose(); ho_Contour.Dispose(); ho_SelectedXLD1.Dispose(); ho_SortedContours2.Dispose(); ho_ObjectSelectedB.Dispose(); return(result); }
public static void findline(HObject ho_Image, out HObject ho_Line, HTuple hv_Rectangle2Row, HTuple hv_Rectangle2Column, HTuple hv_Rectangle2Phi, HTuple hv_Rectangle2Length1, HTuple hv_Rectangle2Length2, HTuple hv_Interpolation, HTuple hv_Sigma, HTuple hv_Threshold, HTuple hv_Transition, HTuple hv_Select) { // Local iconic variables HObject ho_Cross3 = null, ho_Contour; // Local control variables HTuple hv_Width = null, hv_Height = null, hv_Cos = null; HTuple hv_Sin = null, hv_row1 = null, hv_column1 = null; HTuple hv_row2 = null, hv_column2 = null, hv_pointsX = null; HTuple hv_pointsY = null, hv_Function = new HTuple(), hv_num = new HTuple(); HTuple hv_Index = new HTuple(), hv_x = new HTuple(), hv_y = new HTuple(); HTuple hv_MeasureHandle = new HTuple(), hv_RowEdge = new HTuple(); HTuple hv_ColumnEdge = new HTuple(), hv_Amplitude = new HTuple(); HTuple hv_Distance = new HTuple(), hv_RowBegin = null; HTuple hv_ColBegin = null, hv_RowEnd = null, hv_ColEnd = null; HTuple hv_Nr = null, hv_Nc = null, hv_Dist = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Line); HOperatorSet.GenEmptyObj(out ho_Cross3); HOperatorSet.GenEmptyObj(out ho_Contour); try { HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); HOperatorSet.TupleCos(hv_Rectangle2Phi, out hv_Cos); HOperatorSet.TupleSin(hv_Rectangle2Phi, out hv_Sin); hv_row1 = hv_Rectangle2Row - (hv_Rectangle2Length2 * hv_Cos); hv_column1 = hv_Rectangle2Column - (hv_Rectangle2Length2 * hv_Sin); hv_row2 = hv_Rectangle2Row + (hv_Rectangle2Length2 * hv_Cos); hv_column2 = hv_Rectangle2Column + (hv_Rectangle2Length2 * hv_Sin); hv_pointsX = new HTuple(); hv_pointsY = new HTuple(); if ((int)((new HTuple(((hv_Rectangle2Phi.TupleAbs())).TupleGreater(0.785))).TupleAnd( new HTuple(((hv_Rectangle2Phi.TupleAbs())).TupleLess(2.345)))) != 0) { if ((int)(new HTuple(hv_column1.TupleLess(hv_column2))) != 0) { HOperatorSet.CreateFunct1dPairs(hv_column1.TupleConcat(hv_column2), hv_row1.TupleConcat( hv_row2), out hv_Function); } else { HOperatorSet.CreateFunct1dPairs(hv_column2.TupleConcat(hv_column1), hv_row2.TupleConcat( hv_row1), out hv_Function); } hv_num = ((hv_Rectangle2Length2 / 3)).TupleInt(); HTuple end_val18 = hv_num; HTuple step_val18 = 1; for (hv_Index = 1; hv_Index.Continue(end_val18, step_val18); hv_Index = hv_Index.TupleAdd(step_val18)) { if ((int)(new HTuple(hv_Index.TupleEqual(1))) != 0) { hv_x = (hv_column1.TupleMin2(hv_column2)) + (((((hv_column1 - hv_column2)).TupleAbs() ) / hv_num) / 2); } else { hv_x = (hv_column1.TupleMin2(hv_column2)) + (((((hv_column1 - hv_column2)).TupleAbs() ) / hv_num) * (hv_Index - 0.5)); } HOperatorSet.GetYValueFunct1d(hv_Function, hv_x, "constant", out hv_y); //gen_cross_contour_xld (Cross2, x, y, 6, Phi) //gen_rectangle2 (Rectangle1, y, x, Phi, Length1, abs(row1-row2)/num/2) HOperatorSet.GenMeasureRectangle2(hv_y, hv_x, hv_Rectangle2Phi, hv_Rectangle2Length1, ((((hv_column1 - hv_column2)).TupleAbs()) / hv_num) / 2, hv_Width, hv_Height, hv_Interpolation, out hv_MeasureHandle); HOperatorSet.MeasurePos(ho_Image, hv_MeasureHandle, hv_Sigma, hv_Threshold, hv_Transition, hv_Select, out hv_RowEdge, out hv_ColumnEdge, out hv_Amplitude, out hv_Distance); ho_Cross3.Dispose(); HOperatorSet.GenCrossContourXld(out ho_Cross3, hv_RowEdge, hv_ColumnEdge, 10, hv_Rectangle2Phi); HOperatorSet.CloseMeasure(hv_MeasureHandle); hv_pointsX = hv_pointsX.TupleConcat(hv_RowEdge); hv_pointsY = hv_pointsY.TupleConcat(hv_ColumnEdge); } } else { if ((int)(new HTuple(hv_row1.TupleLess(hv_row2))) != 0) { HOperatorSet.CreateFunct1dPairs(hv_row1.TupleConcat(hv_row2), hv_column1.TupleConcat( hv_column2), out hv_Function); } else { HOperatorSet.CreateFunct1dPairs(hv_row2.TupleConcat(hv_row1), hv_column2.TupleConcat( hv_column1), out hv_Function); } hv_num = ((hv_Rectangle2Length2 / 3)).TupleInt(); HTuple end_val49 = hv_num; HTuple step_val49 = 1; for (hv_Index = 1; hv_Index.Continue(end_val49, step_val49); hv_Index = hv_Index.TupleAdd(step_val49)) { if ((int)(new HTuple(hv_Index.TupleEqual(1))) != 0) { hv_x = (hv_row1.TupleMin2(hv_row2)) + ((((((hv_row1 - hv_row2)).TupleAbs() ) / hv_num) / 2) * hv_Index); } else { hv_x = (hv_row1.TupleMin2(hv_row2)) + (((((hv_row1 - hv_row2)).TupleAbs() ) / hv_num) * (hv_Index - 0.5)); } HOperatorSet.GetYValueFunct1d(hv_Function, hv_x, "constant", out hv_y); //gen_cross_contour_xld (Cross2, x, y, 6, Phi) //gen_rectangle2 (Rectangle1, x, y, Phi, Length1, abs(row1-row2)/num/2) HOperatorSet.GenMeasureRectangle2(hv_x, hv_y, hv_Rectangle2Phi, hv_Rectangle2Length1, ((((hv_row1 - hv_row2)).TupleAbs()) / hv_num) / 2, hv_Width, hv_Height, hv_Interpolation, out hv_MeasureHandle); HOperatorSet.MeasurePos(ho_Image, hv_MeasureHandle, hv_Sigma, hv_Threshold, hv_Transition, hv_Select, out hv_RowEdge, out hv_ColumnEdge, out hv_Amplitude, out hv_Distance); ho_Cross3.Dispose(); HOperatorSet.GenCrossContourXld(out ho_Cross3, hv_RowEdge, hv_ColumnEdge, 10, hv_Rectangle2Phi); HOperatorSet.CloseMeasure(hv_MeasureHandle); hv_pointsX = hv_pointsX.TupleConcat(hv_RowEdge); hv_pointsY = hv_pointsY.TupleConcat(hv_ColumnEdge); } } ho_Contour.Dispose(); if (hv_pointsX.Length < 2) { throw new Exception("not get enough point in findline operater"); } HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_pointsX, hv_pointsY); HOperatorSet.FitLineContourXld(ho_Contour, "tukey", -1, 0, 5, 2, out hv_RowBegin, out hv_ColBegin, out hv_RowEnd, out hv_ColEnd, out hv_Nr, out hv_Nc, out hv_Dist); ho_Line.Dispose(); HOperatorSet.GenRegionLine(out ho_Line, hv_RowBegin, hv_ColBegin, hv_RowEnd, hv_ColEnd); ho_Cross3.Dispose(); ho_Contour.Dispose(); return; } catch (HalconException HDevExpDefaultException) { ho_Cross3.Dispose(); ho_Contour.Dispose(); throw HDevExpDefaultException; } }