/// <summary> /// Evaluates the gray value contrast between the marks and the calibration /// plate and the homogeneity of the used illumination. /// </summary> public void eval_caltab_contrast_homogeneity(HObject ho_Image, HObject ho_Marks, out HTuple hv_Contrast, out HTuple hv_ContrastScore, out HTuple hv_HomogeneityScore) { // Local iconic variables HObject ho_Region, ho_RegionDilation; // Local control variables HTuple hv_Number, hv_Min, hv_Max, hv_Range; HTuple hv_MinContrast, hv_MaxContrast, hv_DeviationMax; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_RegionDilation); hv_ContrastScore = 0.0; hv_Contrast = 0.0; hv_HomogeneityScore = 0.0; HOperatorSet.CountObj(ho_Marks, out hv_Number); if ((int)(new HTuple(hv_Number.TupleLess(4))) != 0) { ho_Region.Dispose(); ho_RegionDilation.Dispose(); return; } ho_Region.Dispose(); HOperatorSet.GenRegionContourXld(ho_Marks, out ho_Region, "margin"); ho_RegionDilation.Dispose(); HOperatorSet.DilationCircle(ho_Region, out ho_RegionDilation, 5.5); HOperatorSet.MinMaxGray(ho_RegionDilation, ho_Image, 3, out hv_Min, out hv_Max, out hv_Range); //Calculate contrast score hv_Contrast = hv_Range.TupleMean(); hv_MinContrast = 70; hv_MaxContrast = 160; if ((int)(new HTuple(hv_Contrast.TupleGreater(hv_MinContrast))) != 0) { hv_ContrastScore = (hv_Contrast - hv_MinContrast) / (hv_MaxContrast - hv_MinContrast); hv_ContrastScore = ((hv_ContrastScore.TupleConcat(1.0))).TupleMin(); } //Now for the homogeneity score HOperatorSet.TupleDeviation(hv_Max, out hv_DeviationMax); hv_HomogeneityScore = 1.1 - (hv_DeviationMax / 40.0); hv_HomogeneityScore = ((((((hv_HomogeneityScore.TupleConcat(1.0))).TupleMin() )).TupleConcat(0.0))).TupleMax(); ho_Region.Dispose(); ho_RegionDilation.Dispose(); return; }
public static void dispImage(HWindowControl hWC, HObject HImage) { HTuple hv_Width = null, hv_Height = null, hv_picWHRatio = null; HTuple hv_winWHRatio = null, hv_dispWidth = new HTuple(); HTuple hv_dispHeight = new HTuple(); // Initialize local and output iconic variables HTuple hv_winWidth = hWC.Width; HTuple hv_winHeight = hWC.Height; HOperatorSet.SetSystem("int_zooming", "true"); HOperatorSet.GetImageSize(HImage, out hv_Width, out hv_Height); hv_picWHRatio = (1.0 * hv_Width) / hv_Height; hv_winWHRatio = (1.0 * hv_winWidth) / hv_winHeight; if (new HTuple(hv_Width.TupleGreater(hv_winWidth)).TupleOr(new HTuple(hv_Height.TupleGreater( hv_winHeight))) != 0) { //如果图片宽高比 大于 窗口宽高比 //则宽度方向顶格 if (new HTuple(hv_picWHRatio.TupleGreaterEqual(hv_winWHRatio)) != 0) { hv_dispWidth = hv_Width.Clone(); hv_dispHeight = hv_Width / hv_winWHRatio; HOperatorSet.SetPart(hWC.HalconWindow, 0, 0, hv_dispHeight, hv_dispWidth); HOperatorSet.DispObj(HImage, hWC.HalconWindow); } //如果图片宽高比 小于 窗口宽高比 //则高度方向顶格 if (new HTuple(hv_picWHRatio.TupleLess(hv_winWHRatio)) == 0) { return; } hv_dispWidth = hv_Height * hv_winWHRatio; hv_dispHeight = hv_Height.Clone(); HOperatorSet.SetPart(hWC.HalconWindow, 0, 0, hv_dispHeight, hv_dispWidth); HOperatorSet.DispObj(HImage, hWC.HalconWindow); } else { //如果图片的长和宽都小于窗口,则以图片的原真实尺寸显示 HOperatorSet.SetPart(hWC.HalconWindow, 0, 0, hv_winWidth, hv_winHeight); HOperatorSet.DispObj(HImage, hWC.HalconWindow); } return; ////////////////////////////////////////////////////////////////////////////test-end }
public MeasureResult Action() { #region 輸出結果 LineResult mResult = null; #endregion // Local iconic variables HObject ho_A2_Region = null; HObject ho_A2_Reduced = null, ho_A2Region = null, ho_A2_RegionBorder = null; HObject ho_A2_RegionDilation = null, ho_A2_Edges = null, ho_A2_Rectangles = null; HObject ho_A2Cross = null, ho_A3_Region = null, ho_A3_Reduced = null; HObject ho_A3Region = null, ho_A3_RegionBorder = null, ho_A3_RegionDilation = null; HObject ho_A3_Edges = null, ho_A3_Rectangles = null, ho_A3Object = null; HObject ho_A3Cross = null; // Local control variables HTuple hv_msgOffsetY, hv_msgOffsetX; HTuple hv_STD_Row; HTuple hv_STD_Col, hv_Img_Row, hv_Img_Col, hv_Img_Rotate_Angle; HTuple hv_OffsetRow, hv_OffsetCol, hv_A2_Center_X, hv_A2_Center_Y; HTuple hv_STD_A2_1_1_Row, hv_STD_A2_1_1_Col, hv_STD_A2_1_1_V_Row; HTuple hv_STD_A2_1_1_V_Col, hv_A2_1_1_X, hv_A2_1_1_Y, hv_A2_1_1_Pos_Row; HTuple hv_A2_1_1_Pos_Col, hv_A2_ROI_W = new HTuple(); HTuple hv_A2_ROI_H = new HTuple(), hv_Rec_W = new HTuple(); HTuple hv_Rec_H = new HTuple(), hv_A2_Alpha = new HTuple(); HTuple hv_a2low = new HTuple(), hv_a2high = new HTuple(), hv_a2Limit = new HTuple(); HTuple hv_A2_RecNumber = new HTuple(), hv_A2Row = new HTuple(); HTuple hv_A2Column = new HTuple(), hv_A2Phi = new HTuple(); HTuple hv_A2Length1 = new HTuple(), hv_A2Length2 = new HTuple(); HTuple hv_A2PointOrder = new HTuple(), hv_A2Number = new HTuple(); HTuple hv_A3_Center_X, hv_A3_Center_Y, hv_STD_A3_Row, hv_STD_A3_Col; HTuple hv_STD_A3_V_Row, hv_STD_A3_V_Col, hv_A3_X, hv_A3_Y; HTuple hv_A3_Pos_Row, hv_A3_Pos_Col, hv_A3_ROI_W = new HTuple(); HTuple hv_A3_ROI_H = new HTuple(), hv_A3_Alpha = new HTuple(); HTuple hv_low = new HTuple(), hv_high = new HTuple(), hv_a3Limit = new HTuple(); HTuple hv_A3RecNumber = new HTuple(), hv_A3Row = new HTuple(); HTuple hv_A3Column = new HTuple(), hv_A3Phi = new HTuple(); HTuple hv_A3Length1 = new HTuple(), hv_A3Length2 = new HTuple(); HTuple hv_A3PointOrder = new HTuple(), hv_A3Number = new HTuple(); HTuple hv_X1 = new HTuple(), hv_X2 = new HTuple(); HTuple hv_Y1 = new HTuple(), hv_Y2 = new HTuple(), hv_STD_Mark_A3_C_Row = new HTuple(); HTuple hv_STD_Mark_A3_C_Col = new HTuple(), hv_STD_Mark_A3_Start_Row = new HTuple(); HTuple hv_STD_Mark_A3_Start_Col = new HTuple(), hv_STD_Mark_A3_End_Row = new HTuple(); HTuple hv_STD_Mark_A3_End_Col = new HTuple(), hv_STD_Mark_A3_C_V_Row = new HTuple(); HTuple hv_STD_Mark_A3_C_V_Col = new HTuple(), hv_Mark_A3_C_X = new HTuple(); HTuple hv_Mark_A3_C_Y = new HTuple(), hv_Mark_A3_C_Pos_Row = new HTuple(); HTuple hv_Mark_A3_C_Pos_Col = new HTuple(), hv_STD_Mark_A3_Start_V_Row = new HTuple(); HTuple hv_STD_Mark_A3_Start_V_Col = new HTuple(), hv_Mark_A3_Start_X = new HTuple(); HTuple hv_Mark_A3_Start_Y = new HTuple(), hv_Mark_A3_Start_Pos_Row = new HTuple(); HTuple hv_Mark_A3_Start_Pos_Col = new HTuple(), hv_STD_Mark_A3_End_V_Row = new HTuple(); HTuple hv_STD_Mark_A3_End_V_Col = new HTuple(), hv_Mark_A3_End_X = new HTuple(); HTuple hv_Mark_A3_End_Y = new HTuple(), hv_Mark_A3_End_Pos_Row = new HTuple(); HTuple hv_Mark_A3_End_Pos_Col = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_A2_Region); HOperatorSet.GenEmptyObj(out ho_A2_Reduced); HOperatorSet.GenEmptyObj(out ho_A2Region); HOperatorSet.GenEmptyObj(out ho_A2_RegionBorder); HOperatorSet.GenEmptyObj(out ho_A2_RegionDilation); HOperatorSet.GenEmptyObj(out ho_A2_Edges); HOperatorSet.GenEmptyObj(out ho_A2_Rectangles); HOperatorSet.GenEmptyObj(out ho_A2Cross); HOperatorSet.GenEmptyObj(out ho_A3_Region); HOperatorSet.GenEmptyObj(out ho_A3_Reduced); HOperatorSet.GenEmptyObj(out ho_A3Region); HOperatorSet.GenEmptyObj(out ho_A3_RegionBorder); HOperatorSet.GenEmptyObj(out ho_A3_RegionDilation); HOperatorSet.GenEmptyObj(out ho_A3_Edges); HOperatorSet.GenEmptyObj(out ho_A3_Rectangles); HOperatorSet.GenEmptyObj(out ho_A3Object); HOperatorSet.GenEmptyObj(out ho_A3Cross); //Measure: SDMS_A3 //Author: John Hsieh //Date: 2012 //Note: A3 related to A2 //ho_Image.Dispose(); //HOperatorSet.ReadImage(out ho_Image, "Images/STD.bmp"); //dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle); // dev_update_off(...); only in hdevelop HOperatorSet.SetSystem("border_shape_models", "false"); //****Message Args hv_msgOffsetY = 100; hv_msgOffsetX = 100; //****Model Args //STD 中心點 hv_STD_Row = 772; hv_STD_Col = 1003; //目前圖形 中心點 hv_Img_Row = hv_AllModelRow.Clone(); hv_Img_Col = hv_AllModelColumn.Clone(); //目前圖形 Rotate Angle hv_Img_Rotate_Angle = hv_AllModelAngle.Clone(); //目前圖形偏移量 hv_OffsetRow = hv_Img_Row - hv_STD_Row; hv_OffsetCol = hv_Img_Col - hv_STD_Col; #region 找左邊數來第一個金手指的中心點 Finger /* //*****A3 //Step 1 */ hv_A2_Center_X = 0; hv_A2_Center_Y = 0; //****A2_1_1 //STD A2_1_1_ 位置 hv_STD_A2_1_1_Row = 410; hv_STD_A2_1_1_Col = 780; //STD 向量 STD_A2_1_1_ hv_STD_A2_1_1_V_Row = hv_STD_A2_1_1_Row - hv_STD_Row; hv_STD_A2_1_1_V_Col = hv_STD_A2_1_1_Col - hv_STD_Col; //A2_1_1_X, A2_1_1_Y 分量 hv_A2_1_1_X = (hv_STD_A2_1_1_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_A2_1_1_V_Row * (hv_Img_Rotate_Angle.TupleSin() )); hv_A2_1_1_Y = (hv_STD_A2_1_1_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_A2_1_1_V_Col * (hv_Img_Rotate_Angle.TupleSin() )); //目前圖形 A2_1_1_ 位置 hv_A2_1_1_Pos_Row = (hv_STD_Row + hv_A2_1_1_Y) + hv_OffsetRow; hv_A2_1_1_Pos_Col = (hv_STD_Col + hv_A2_1_1_X) + hv_OffsetCol; //A2_ROI hv_A2_ROI_W = 40; hv_A2_ROI_H = 120; ho_A2_Region.Dispose(); HOperatorSet.GenRectangle2(out ho_A2_Region, hv_A2_1_1_Pos_Row, hv_A2_1_1_Pos_Col, hv_Img_Rotate_Angle, hv_A2_ROI_W, hv_A2_ROI_H); //stop () ho_A2_Reduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_A2_Region, out ho_A2_Reduced); //fit_Rectangle //stop () // ho_A2Region.Dispose(); HOperatorSet.FastThreshold(ho_A2_Reduced, out ho_A2Region, 50, 150, 20); ho_A2_RegionBorder.Dispose(); HOperatorSet.Boundary(ho_A2Region, out ho_A2_RegionBorder, "inner"); hv_Rec_W = 11; hv_Rec_H = 11; ho_A2_RegionDilation.Dispose(); HOperatorSet.DilationRectangle1(ho_A2_RegionBorder, out ho_A2_RegionDilation, hv_Rec_W, hv_Rec_H); hv_A2_Alpha = 0.9; hv_a2low = 20; hv_a2high = 110; ho_A2_Edges.Dispose(); HOperatorSet.EdgesSubPix(ho_A2_Reduced, out ho_A2_Edges, "canny", hv_A2_Alpha, hv_a2low, hv_a2high); //stop () hv_a2Limit = 200; ho_A2_Rectangles.Dispose(); HOperatorSet.SelectShapeXld(ho_A2_Edges, out ho_A2_Rectangles, "contlength", "and", hv_a2Limit, 99999); HOperatorSet.CountObj(ho_A2_Rectangles, out hv_A2_RecNumber); while ((int)(new HTuple(hv_A2_RecNumber.TupleGreater(1))) != 0) { hv_a2Limit = hv_a2Limit + 10; ho_A2_Rectangles.Dispose(); HOperatorSet.SelectShapeXld(ho_A2_Edges, out ho_A2_Rectangles, "contlength", "and", hv_a2Limit, 99999); HOperatorSet.CountObj(ho_A2_Rectangles, out hv_A2_RecNumber); } //stop () HOperatorSet.FitRectangle2ContourXld(ho_A2_Rectangles, "regression", -1, 0, 0, 3, 2, out hv_A2Row, out hv_A2Column, out hv_A2Phi, out hv_A2Length1, out hv_A2Length2, out hv_A2PointOrder); HOperatorSet.CountObj(ho_A2_Rectangles, out hv_A2Number); //取A2 if ((int)(new HTuple(hv_A2Number.TupleGreater(0))) != 0) { hv_A2_Center_X = hv_A2Column[0]; hv_A2_Center_Y = hv_A2Row[0]; ho_A2Cross.Dispose(); HOperatorSet.GenCrossContourXld(out ho_A2Cross, hv_A2Row, hv_A2Column, 10, 0); mResult = new LineResult() { Row1 = new HTuple(hv_A2Row), Col1 = new HTuple(hv_A2Column), }; } #endregion //step 2 取點 A3,金手指由右數來第一個的中心點 hv_A3_Center_X = 0; hv_A3_Center_Y = 0; //****A3 //STD A3_ 位置 hv_STD_A3_Row = 393; hv_STD_A3_Col = 1330; //STD 向量 STD_A3_ hv_STD_A3_V_Row = hv_STD_A3_Row - hv_STD_Row; hv_STD_A3_V_Col = hv_STD_A3_Col - hv_STD_Col; //A3_X, A3_Y 分量 hv_A3_X = (hv_STD_A3_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_A3_V_Row * (hv_Img_Rotate_Angle.TupleSin() )); hv_A3_Y = (hv_STD_A3_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_A3_V_Col * (hv_Img_Rotate_Angle.TupleSin() )); //目前圖形 A3_ 位置 hv_A3_Pos_Row = (hv_STD_Row + hv_A3_Y) + hv_OffsetRow; hv_A3_Pos_Col = (hv_STD_Col + hv_A3_X) + hv_OffsetCol; //A3_ROI hv_A3_ROI_W = 40; hv_A3_ROI_H = 140; ho_A3_Region.Dispose(); HOperatorSet.GenRectangle2(out ho_A3_Region, hv_A3_Pos_Row, hv_A3_Pos_Col, hv_Img_Rotate_Angle, hv_A3_ROI_W, hv_A3_ROI_H); //stop () ho_A3_Reduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_A3_Region, out ho_A3_Reduced); //fit_Rectangle, 找金手指 ho_A3Region.Dispose(); HOperatorSet.FastThreshold(ho_A3_Reduced, out ho_A3Region, 100, 250, 20); ho_A3_RegionBorder.Dispose(); HOperatorSet.Boundary(ho_A3Region, out ho_A3_RegionBorder, "inner"); if (HDevWindowStack.IsOpen()) { //dev_display (A3_RegionBorder) } hv_Rec_W = 11; hv_Rec_H = 11; ho_A3_RegionDilation.Dispose(); HOperatorSet.DilationRectangle1(ho_A3_RegionBorder, out ho_A3_RegionDilation, hv_Rec_W, hv_Rec_H); //alpha 大容錯大 hv_A3_Alpha = 4; hv_low = 10; hv_high = 100; ho_A3_Edges.Dispose(); HOperatorSet.EdgesSubPix(ho_A3_Reduced, out ho_A3_Edges, "canny", hv_A3_Alpha, hv_low, hv_high); //stop () hv_a3Limit = 200; ho_A3_Rectangles.Dispose(); HOperatorSet.SelectShapeXld(ho_A3_Edges, out ho_A3_Rectangles, "contlength", "and", hv_a3Limit, 99999); HOperatorSet.CountObj(ho_A3_Rectangles, out hv_A3RecNumber); while ((int)(new HTuple(hv_A3RecNumber.TupleGreater(1))) != 0) { hv_a3Limit = hv_a3Limit + 10; ho_A3_Rectangles.Dispose(); HOperatorSet.SelectShapeXld(ho_A3_Edges, out ho_A3_Rectangles, "contlength", "and", hv_a3Limit, 99999); HOperatorSet.CountObj(ho_A3_Rectangles, out hv_A3RecNumber); } HOperatorSet.FitRectangle2ContourXld(ho_A3_Rectangles, "regression", -1, 0, 0, 3, 2, out hv_A3Row, out hv_A3Column, out hv_A3Phi, out hv_A3Length1, out hv_A3Length2, out hv_A3PointOrder); //取A3 中心點 HOperatorSet.CountObj(ho_A3_Rectangles, out hv_A3Number); if ((int)(new HTuple(hv_A3Number.TupleGreater(0))) != 0) { ho_A3Object.Dispose(); HOperatorSet.SelectObj(ho_A3_Rectangles, out ho_A3Object, 1); //stop () //取 A2Object 資訊 hv_A3_Center_X = hv_A3Column[0]; hv_A3_Center_Y = hv_A3Row[0]; ho_A3Cross.Dispose(); HOperatorSet.GenCrossContourXld(out ho_A3Cross, hv_A3Row, hv_A3Column, 10, 0); if (hv_A3Row.TupleLength() > 0 && hv_A2Row.TupleLength() > 0) { mResult.Row2 = new HTuple(hv_A3Row); mResult.Col2 = new HTuple(hv_A3Column); HOperatorSet.DistancePp(hv_A2Row, hv_A2Column, hv_A3Row, hv_A3Column, out mResult.Distance); } } //*****A3 End ho_A2_Region.Dispose(); ho_A2_Reduced.Dispose(); ho_A2Region.Dispose(); ho_A2_RegionBorder.Dispose(); ho_A2_RegionDilation.Dispose(); ho_A2_Edges.Dispose(); ho_A2_Rectangles.Dispose(); ho_A2Cross.Dispose(); ho_A3_Region.Dispose(); ho_A3_Reduced.Dispose(); ho_A3Region.Dispose(); ho_A3_RegionBorder.Dispose(); ho_A3_RegionDilation.Dispose(); ho_A3_Edges.Dispose(); ho_A3_Rectangles.Dispose(); ho_A3Object.Dispose(); ho_A3Cross.Dispose(); return mResult; }
public void ForeignMaterial(HObject ho_GrayImage, out HObject ho_Contours, HTuple hv_Row, HTuple hv_Column, out HTuple hv_string, out HTuple hv_flag) { // Local iconic variables HObject ho_Rectangle_cut, ho_ImageReduced_cut; HObject ho_Region, ho_RegionErosion1, ho_ConnectedRegions1; HObject ho_RegionFillUp2, ho_SelectedRegions, ho_RegionDilation1; HObject ho_RegionFillUp, ho_RegionErosion, ho_ConnectedRegions; HObject ho_SelectedRegions1, ho_RegionUnion, ho_ImageReduced1; HObject ho_Region1; // Local control variables HTuple hv_Row_st = null, hv_Col_st = null; HTuple hv_UsedThreshold = null, hv_Area = null, hv_Row1 = null; HTuple hv_Column1 = null, hv_area = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Contours); HOperatorSet.GenEmptyObj(out ho_Rectangle_cut); HOperatorSet.GenEmptyObj(out ho_ImageReduced_cut); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_RegionErosion1); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1); HOperatorSet.GenEmptyObj(out ho_RegionFillUp2); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_RegionDilation1); HOperatorSet.GenEmptyObj(out ho_RegionFillUp); HOperatorSet.GenEmptyObj(out ho_RegionErosion); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions1); HOperatorSet.GenEmptyObj(out ho_RegionUnion); HOperatorSet.GenEmptyObj(out ho_ImageReduced1); HOperatorSet.GenEmptyObj(out ho_Region1); hv_string = ""; hv_flag = 0; hv_Row_st = hv_Row.Clone(); hv_Col_st = hv_Column.Clone(); ho_Rectangle_cut.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle_cut, hv_Row_st - 150, hv_Col_st - 200, hv_Row_st + 180, hv_Col_st + 230); ho_ImageReduced_cut.Dispose(); HOperatorSet.ReduceDomain(ho_GrayImage, ho_Rectangle_cut, out ho_ImageReduced_cut ); ho_Region.Dispose(); HOperatorSet.BinaryThreshold(ho_ImageReduced_cut, out ho_Region, "max_separability", "light", out hv_UsedThreshold); ho_RegionErosion1.Dispose(); HOperatorSet.ErosionRectangle1(ho_Region, out ho_RegionErosion1, 5, 1); ho_ConnectedRegions1.Dispose(); HOperatorSet.Connection(ho_RegionErosion1, out ho_ConnectedRegions1); ho_RegionFillUp2.Dispose(); HOperatorSet.FillUp(ho_ConnectedRegions1, out ho_RegionFillUp2); ho_SelectedRegions.Dispose(); HOperatorSet.SelectShape(ho_RegionFillUp2, out ho_SelectedRegions, (new HTuple("width")).TupleConcat( "area"), "and", (new HTuple(100)).TupleConcat(2000), (new HTuple(400)).TupleConcat( 999999)); ho_RegionDilation1.Dispose(); HOperatorSet.DilationCircle(ho_SelectedRegions, out ho_RegionDilation1, 3); ho_RegionFillUp.Dispose(); HOperatorSet.FillUp(ho_RegionDilation1, out ho_RegionFillUp); ho_RegionErosion.Dispose(); HOperatorSet.ErosionCircle(ho_RegionFillUp, out ho_RegionErosion, 7); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_RegionErosion, out ho_ConnectedRegions); ho_SelectedRegions1.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions1, (new HTuple("width")).TupleConcat( "area"), "and", (new HTuple(100)).TupleConcat(2000), (new HTuple(400)).TupleConcat( 999999)); ho_RegionUnion.Dispose(); HOperatorSet.Union1(ho_SelectedRegions1, out ho_RegionUnion); ho_ImageReduced1.Dispose(); HOperatorSet.ReduceDomain(ho_GrayImage, ho_RegionUnion, out ho_ImageReduced1); ho_Region1.Dispose(); HOperatorSet.Threshold(ho_ImageReduced1, out ho_Region1, 0, 40); HOperatorSet.AreaCenter(ho_Region1, out hv_Area, out hv_Row1, out hv_Column1); ho_Contours.Dispose(); HOperatorSet.GenContourRegionXld(ho_Region1, out ho_Contours, "border"); hv_area = hv_Area.TupleSum(); if ((int)(new HTuple(hv_area.TupleGreater(100))) != 0) { hv_string = "Foreign material"; hv_flag = 2; //错误2:外来物检测 } else { hv_flag = 1; } ho_Rectangle_cut.Dispose(); ho_ImageReduced_cut.Dispose(); ho_Region.Dispose(); ho_RegionErosion1.Dispose(); ho_ConnectedRegions1.Dispose(); ho_RegionFillUp2.Dispose(); ho_SelectedRegions.Dispose(); ho_RegionDilation1.Dispose(); ho_RegionFillUp.Dispose(); ho_RegionErosion.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions1.Dispose(); ho_RegionUnion.Dispose(); ho_ImageReduced1.Dispose(); ho_Region1.Dispose(); return; }
public MeasureResult Action() { #region 輸出結果 CircleResult mResult = null; #endregion // Local iconic variables HObject ho_R19_Circle = null; HObject ho_R19_ROI_Image = null, ho_R19_Region = null, ho_R19_ImageReduced = null; HObject ho_R19_Edges = null, ho_R19_ContoursSplit = null, ho_R19_SingleSegment = null; HObject ho_R19_ContEllipse = null; // Local control variables HTuple hv_msgOffsetY, hv_msgOffsetX; HTuple hv_STD_Row; HTuple hv_STD_Col, hv_Img_Row, hv_Img_Col, hv_Img_Rotate_Angle; HTuple hv_OffsetRow, hv_OffsetCol, hv_STD_R19_Row, hv_STD_R19_Col; HTuple hv_STD_R19_V_Row, hv_STD_R19_V_Col, hv_R19_X, hv_R19_Y; HTuple hv_R19_Pos_Row, hv_R19_Pos_Col, hv_R19_R; HTuple hv_alpha = new HTuple(), hv_R19_low = new HTuple(); HTuple hv_R19_high = new HTuple(), hv_R19_NumSegments = new HTuple(); HTuple hv_NumCircles = new HTuple(), hv_Num_Circle_Point = new HTuple(); HTuple hv_R19 = new HTuple(), hv_i = new HTuple(), hv_Attrib = new HTuple(); HTuple hv_R19_Row = new HTuple(), hv_R19_Column = new HTuple(); HTuple hv_R19_Radius = new HTuple(), hv_R19_StartPhi = new HTuple(); HTuple hv_R19_EndPhi = new HTuple(), hv_R19_PointOrder = new HTuple(); HTuple hv_R19_MinDist = new HTuple(), hv_R19_MaxDist = new HTuple(); HTuple hv_R19_AvgDist = new HTuple(), hv_R19_SigmaDist = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_R19_Circle); HOperatorSet.GenEmptyObj(out ho_R19_ROI_Image); HOperatorSet.GenEmptyObj(out ho_R19_Region); HOperatorSet.GenEmptyObj(out ho_R19_ImageReduced); HOperatorSet.GenEmptyObj(out ho_R19_Edges); HOperatorSet.GenEmptyObj(out ho_R19_ContoursSplit); HOperatorSet.GenEmptyObj(out ho_R19_SingleSegment); HOperatorSet.GenEmptyObj(out ho_R19_ContEllipse); //Measure: SDMS_R19 //Author: John Hsieh //Date: 2012 HOperatorSet.SetSystem("border_shape_models", "false"); //****Message Args hv_msgOffsetY = 100; hv_msgOffsetX = 100; //****Model Args //STD 中心點 hv_STD_Row = 772; hv_STD_Col = 1003; //目前圖形 中心點 hv_Img_Row = hv_AllModelRow.Clone(); hv_Img_Col = hv_AllModelColumn.Clone(); //目前圖形 Rotate Angle hv_Img_Rotate_Angle = hv_AllModelAngle.Clone(); //目前圖形偏移量 hv_OffsetRow = hv_Img_Row - hv_STD_Row; hv_OffsetCol = hv_Img_Col - hv_STD_Col; //****Display if (HDevWindowStack.IsOpen()) { HOperatorSet.ClearWindow(HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } //****R19 //STD R19_ 位置 hv_STD_R19_Row = 794; hv_STD_R19_Col = 620; //STD 向量 STD_R19_ hv_STD_R19_V_Row = hv_STD_R19_Row - hv_STD_Row; hv_STD_R19_V_Col = hv_STD_R19_Col - hv_STD_Col; //R19_X, R19_Y 分量 hv_R19_X = (hv_STD_R19_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_R19_V_Row * (hv_Img_Rotate_Angle.TupleSin() )); hv_R19_Y = (hv_STD_R19_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_R19_V_Col * (hv_Img_Rotate_Angle.TupleSin() )); //目前圖形 R19_ 位置 hv_R19_Pos_Row = (hv_STD_Row + hv_R19_Y) + hv_OffsetRow; hv_R19_Pos_Col = (hv_STD_Col + hv_R19_X) + hv_OffsetCol; hv_R19_R = 15; ho_R19_Circle.Dispose(); HOperatorSet.GenCircle(out ho_R19_Circle, hv_R19_Pos_Row, hv_R19_Pos_Col, hv_R19_R); if (HDevWindowStack.IsOpen()) { //dev_display (R19_Circle) } //stop () ho_R19_ROI_Image.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_R19_Circle, out ho_R19_ROI_Image); ho_R19_Region.Dispose(); HOperatorSet.FastThreshold(ho_R19_ROI_Image, out ho_R19_Region, 100, 255, 15); ho_R19_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_R19_ROI_Image, ho_R19_Region, out ho_R19_ImageReduced ); //stop () //sobel_fast 具有較寬的選擇範圍,搭配 alpha 參數 (alpha 越大, 容錯範圍大) hv_alpha = 0.9; hv_R19_low = 2; hv_R19_high = 60; ho_R19_Edges.Dispose(); HOperatorSet.EdgesSubPix(ho_R19_ImageReduced, out ho_R19_Edges, "sobel_fast", hv_alpha, hv_R19_low, hv_R19_high); //stop () //*所有的數值越小,表示容錯範圍大,反之亦然 ho_R19_ContoursSplit.Dispose(); HOperatorSet.SegmentContoursXld(ho_R19_Edges, out ho_R19_ContoursSplit, "lines_circles", 17, 1, 1); //Display the results //=========================================================== HOperatorSet.CountObj(ho_R19_ContoursSplit, out hv_R19_NumSegments); hv_NumCircles = 0; hv_Num_Circle_Point = 0; hv_R19 = 999; for (hv_i = 1; hv_i.Continue(hv_R19_NumSegments, 1); hv_i = hv_i.TupleAdd(1)) { ho_R19_SingleSegment.Dispose(); HOperatorSet.SelectObj(ho_R19_ContoursSplit, out ho_R19_SingleSegment, hv_i); HOperatorSet.GetContourGlobalAttribXld(ho_R19_SingleSegment, "cont_approx", out hv_Attrib); if ((int)(new HTuple(hv_Attrib.TupleEqual(1))) != 0) { HOperatorSet.FitCircleContourXld(ho_R19_SingleSegment, "atukey", -1, 2, hv_Num_Circle_Point, 5, 2, out hv_R19_Row, out hv_R19_Column, out hv_R19_Radius, out hv_R19_StartPhi, out hv_R19_EndPhi, out hv_R19_PointOrder); ho_R19_ContEllipse.Dispose(); HOperatorSet.GenEllipseContourXld(out ho_R19_ContEllipse, hv_R19_Row, hv_R19_Column, 0, hv_R19_Radius, hv_R19_Radius, 0, (new HTuple(360)).TupleRad(), "positive", 1.0); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_R19_ContEllipse, HDevWindowStack.GetActive()); } HOperatorSet.DistEllipseContourXld(ho_R19_SingleSegment, "algebraic", -1, 0, hv_R19_Row, hv_R19_Column, 0, hv_R19_Radius, hv_R19_Radius, out hv_R19_MinDist, out hv_R19_MaxDist, out hv_R19_AvgDist, out hv_R19_SigmaDist); hv_NumCircles = hv_NumCircles + 1; if ((int)(new HTuple(hv_R19.TupleGreater(hv_R19_Radius))) != 0) { hv_R19 = hv_R19_Radius.Clone(); mResult = new CircleResult() { Row = new HTuple(hv_R19_Row), Col = new HTuple(hv_R19_Column), Radius = new HTuple(hv_R19_Radius), StartPhi = new HTuple(hv_R19_StartPhi), EndPhi = new HTuple(hv_R19_EndPhi), PointOrder = new HTuple(hv_R19_PointOrder), }; } } } ho_R19_Circle.Dispose(); ho_R19_ROI_Image.Dispose(); ho_R19_Region.Dispose(); ho_R19_ImageReduced.Dispose(); ho_R19_Edges.Dispose(); ho_R19_ContoursSplit.Dispose(); ho_R19_SingleSegment.Dispose(); ho_R19_ContEllipse.Dispose(); return(mResult); }
// 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 MeasureResult Action() { #region 輸出結果 DistanceResult mResult = null; #endregion // Local iconic variables HObject ho_A_1_1_Rectangle = null; HObject ho_A_1_1_Image = null, ho_A_1_1_Edges = null, ho_A_1_1_Contours = null; HObject ho_A_1_1_SelectedLines = null, ho_A_1_1_Line = null; HObject ho_A_1_2_Rectangle = null, ho_A_1_2_Image = null, ho_A_1_2_Edges = null; HObject ho_A_1_2_Contours = null, ho_A_1_2_SelectedLines = null; HObject ho_A_1_2_Line = null; // Local control variables HTuple hv_msgOffsetY, hv_msgOffsetX; HTuple hv_STD_Row; HTuple hv_STD_Col, hv_Img_Row, hv_Img_Col, hv_Img_Rotate_Angle; HTuple hv_OffsetRow, hv_OffsetCol, hv_STD_A_1_1_Row, hv_STD_A_1_1_Col; HTuple hv_STD_A_1_1_V_Row, hv_STD_A_1_1_V_Col, hv_A_1_1_X; HTuple hv_A_1_1_Y, hv_A_1_1_Pos_Row, hv_A_1_1_Pos_Col; HTuple hv_Corner_W = new HTuple(), hv_Corner_H = new HTuple(); HTuple hv_low = new HTuple(), hv_high = new HTuple(), hv_algo = new HTuple(); HTuple hv_A_1_1Number = new HTuple(), hv_A_1_1_LineRowBegin = new HTuple(); HTuple hv_A_1_1_LineColBegin = new HTuple(), hv_A_1_1_LineRowEnd = new HTuple(); HTuple hv_A_1_1_LineColEnd = new HTuple(), hv_A_1_1_LineNr = new HTuple(); HTuple hv_A_1_1_LineNc = new HTuple(), hv_A_1_1_LineDist = new HTuple(); HTuple hv_STD_A_1_2_Row, hv_STD_A_1_2_Col, hv_STD_A_1_2_V_Row; HTuple hv_STD_A_1_2_V_Col, hv_A_1_2_X, hv_A_1_2_Y, hv_A_1_2_Pos_Row; HTuple hv_A_1_2_Pos_Col, hv_A_1_2Number = new HTuple(), hv_A_1_2_LineRowBegin = new HTuple(); HTuple hv_A_1_2_LineColBegin = new HTuple(), hv_A_1_2_LineRowEnd = new HTuple(); HTuple hv_A_1_2_LineColEnd = new HTuple(), hv_A_1_2_LineNr = new HTuple(); HTuple hv_A_1_2_LineNc = new HTuple(), hv_A_1_2_LineDist = new HTuple(); HTuple hv_DistanceMin, hv_DistanceMax; HTuple hv_MeasureReasult; // Initialize local and output iconic variables //HOperatorSet.GenEmptyObj(out ho_Image); //HOperatorSet.GenEmptyObj(out ho_AllModelContours); HOperatorSet.GenEmptyObj(out ho_A_1_1_Rectangle); HOperatorSet.GenEmptyObj(out ho_A_1_1_Image); HOperatorSet.GenEmptyObj(out ho_A_1_1_Edges); HOperatorSet.GenEmptyObj(out ho_A_1_1_Contours); HOperatorSet.GenEmptyObj(out ho_A_1_1_SelectedLines); HOperatorSet.GenEmptyObj(out ho_A_1_1_Line); HOperatorSet.GenEmptyObj(out ho_A_1_2_Rectangle); HOperatorSet.GenEmptyObj(out ho_A_1_2_Image); HOperatorSet.GenEmptyObj(out ho_A_1_2_Edges); HOperatorSet.GenEmptyObj(out ho_A_1_2_Contours); HOperatorSet.GenEmptyObj(out ho_A_1_2_SelectedLines); HOperatorSet.GenEmptyObj(out ho_A_1_2_Line); //Measure: SDMS_A //Author: John Hsieh //Date: 2012 //ho_Image.Dispose(); //HOperatorSet.ReadImage(out ho_Image, "Images/STD.bmp"); //dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle); // dev_update_off(...); only in hdevelop HOperatorSet.SetSystem("border_shape_models", "false"); //****Message Args hv_msgOffsetY = 100; hv_msgOffsetX = 100; //****Model All //HOperatorSet.ReadShapeModel("D:/Projects/Halcon/SDMS/SDMS_Measure/Model/MatchingAll.shm", // out hv_AllModelId); //ho_AllModelContours.Dispose(); //HOperatorSet.GetShapeModelContours(out ho_AllModelContours, hv_AllModelId, 1); //HOperatorSet.FindShapeModel(ho_Image, hv_AllModelId, (new HTuple(0)).TupleRad() // , (new HTuple(360)).TupleRad(), 0.5, 1, 0.5, "least_squares", 6, 0.75, out hv_AllModelRow, // out hv_AllModelColumn, out hv_AllModelAngle, out hv_AllModelScore); //****Model Args //STD 中心點 hv_STD_Row = 772; hv_STD_Col = 1003; //目前圖形 中心點 hv_Img_Row = hv_AllModelRow.Clone(); hv_Img_Col = hv_AllModelColumn.Clone(); //目前圖形 Rotate Angle hv_Img_Rotate_Angle = hv_AllModelAngle.Clone(); //目前圖形偏移量 hv_OffsetRow = hv_Img_Row - hv_STD_Row; hv_OffsetCol = hv_Img_Col - hv_STD_Col; //*****A //****A_1_1 //STD A_1_1_ 位置 HTuple newR, newC; hv_STD_A_1_1_Row = 1110; hv_STD_A_1_1_Col = 630; ReLocater(hv_STD_Row, hv_STD_Col, hv_Img_Rotate_Angle, hv_OffsetRow, hv_OffsetCol, hv_STD_A_1_1_Row, hv_STD_A_1_1_Col, out newR, out newC); //STD 向量 STD_A_1_1_ hv_STD_A_1_1_V_Row = hv_STD_A_1_1_Row - hv_STD_Row; hv_STD_A_1_1_V_Col = hv_STD_A_1_1_Col - hv_STD_Col; //A_1_1_X, A_1_1_Y 分量 hv_A_1_1_X = (hv_STD_A_1_1_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_A_1_1_V_Row * (hv_Img_Rotate_Angle.TupleSin() )); hv_A_1_1_Y = (hv_STD_A_1_1_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_A_1_1_V_Col * (hv_Img_Rotate_Angle.TupleSin() )); //目前圖形 A_1_1_ 位置 hv_A_1_1_Pos_Row = (hv_STD_Row + hv_A_1_1_Y) + hv_OffsetRow; hv_A_1_1_Pos_Col = (hv_STD_Col + hv_A_1_1_X) + hv_OffsetCol; hv_Corner_W = 30; hv_Corner_H = 120; ho_A_1_1_Rectangle.Dispose(); HOperatorSet.GenRectangle2(out ho_A_1_1_Rectangle, hv_A_1_1_Pos_Row, hv_A_1_1_Pos_Col, hv_Img_Rotate_Angle, hv_Corner_W, hv_Corner_H); ho_A_1_1_Image.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_A_1_1_Rectangle, out ho_A_1_1_Image); //corner detect hv_low = 60; hv_high = 90; ho_A_1_1_Edges.Dispose(); HOperatorSet.EdgesSubPix(ho_A_1_1_Image, out ho_A_1_1_Edges, "lanser2", 0.3, hv_low, hv_high); hv_algo = "lines"; ho_A_1_1_Contours.Dispose(); HOperatorSet.SegmentContoursXld(ho_A_1_1_Edges, out ho_A_1_1_Contours, hv_algo, 9, 4, 2); //取長線段 ho_A_1_1_SelectedLines.Dispose(); HOperatorSet.SelectShapeXld(ho_A_1_1_Contours, out ho_A_1_1_SelectedLines, "contlength", "and", 50, 99999); HOperatorSet.CountObj(ho_A_1_1_SelectedLines, out hv_A_1_1Number); if ((int)(new HTuple(hv_A_1_1Number.TupleGreater(0))) != 0) { ho_A_1_1_Line.Dispose(); HOperatorSet.SelectObj(ho_A_1_1_SelectedLines, out ho_A_1_1_Line, 1); HOperatorSet.FitLineContourXld(ho_A_1_1_Line, "tukey", -1, 0, 5, 2, out hv_A_1_1_LineRowBegin, out hv_A_1_1_LineColBegin, out hv_A_1_1_LineRowEnd, out hv_A_1_1_LineColEnd, out hv_A_1_1_LineNr, out hv_A_1_1_LineNc, out hv_A_1_1_LineDist); mResult = new DistanceResult() { FirstRowBegin = new HTuple(hv_A_1_1_LineRowBegin), FirstColBegin = new HTuple(hv_A_1_1_LineColBegin), FirstRowEnd = new HTuple(hv_A_1_1_LineRowEnd), FirstColEnd = new HTuple(hv_A_1_1_LineColEnd), Angle = this.hv_AllModelAngle, }; } //********* //****A_1_2 //STD A_1_2_ 位置 hv_STD_A_1_2_Row = 997; hv_STD_A_1_2_Col = 1376; //STD 向量 STD_A_1_2_ hv_STD_A_1_2_V_Row = hv_STD_A_1_2_Row - hv_STD_Row; hv_STD_A_1_2_V_Col = hv_STD_A_1_2_Col - hv_STD_Col; //A_1_2_X, A_1_2_Y 分量 hv_A_1_2_X = (hv_STD_A_1_2_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_A_1_2_V_Row * (hv_Img_Rotate_Angle.TupleSin() )); hv_A_1_2_Y = (hv_STD_A_1_2_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_A_1_2_V_Col * (hv_Img_Rotate_Angle.TupleSin() )); //目前圖形 A_1_2_ 位置 hv_A_1_2_Pos_Row = (hv_STD_Row + hv_A_1_2_Y) + hv_OffsetRow; hv_A_1_2_Pos_Col = (hv_STD_Col + hv_A_1_2_X) + hv_OffsetCol; //for (hv_MatchingObjIdx = 0; (int)hv_MatchingObjIdx <= (int)((new HTuple(hv_AllModelScore.TupleLength() // )) - 1); hv_MatchingObjIdx = (int)hv_MatchingObjIdx + 1) //{ hv_Corner_W = 30; hv_Corner_H = 600; ho_A_1_2_Rectangle.Dispose(); HOperatorSet.GenRectangle2(out ho_A_1_2_Rectangle, hv_A_1_2_Pos_Row, hv_A_1_2_Pos_Col, hv_Img_Rotate_Angle, hv_Corner_W, hv_Corner_H); //stop () ho_A_1_2_Image.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_A_1_2_Rectangle, out ho_A_1_2_Image); //corner detect hv_low = 60; hv_high = 90; ho_A_1_2_Edges.Dispose(); HOperatorSet.EdgesSubPix(ho_A_1_2_Image, out ho_A_1_2_Edges, "lanser2", 0.3, hv_low, hv_high); hv_algo = "lines"; ho_A_1_2_Contours.Dispose(); HOperatorSet.SegmentContoursXld(ho_A_1_2_Edges, out ho_A_1_2_Contours, hv_algo, 9, 4, 2); //取長線段 ho_A_1_2_SelectedLines.Dispose(); HOperatorSet.SelectShapeXld(ho_A_1_2_Contours, out ho_A_1_2_SelectedLines, "contlength", "and", 50, 99999); HOperatorSet.CountObj(ho_A_1_2_SelectedLines, out hv_A_1_2Number); if ((int)(new HTuple(hv_A_1_2Number.TupleGreater(0))) != 0) { ho_A_1_2_Line.Dispose(); HOperatorSet.SelectObj(ho_A_1_2_SelectedLines, out ho_A_1_2_Line, 1); HOperatorSet.FitLineContourXld(ho_A_1_2_Line, "tukey", -1, 0, 5, 2, out hv_A_1_2_LineRowBegin, out hv_A_1_2_LineColBegin, out hv_A_1_2_LineRowEnd, out hv_A_1_2_LineColEnd, out hv_A_1_2_LineNr, out hv_A_1_2_LineNc, out hv_A_1_2_LineDist); mResult.SecondColBegin = new HTuple(hv_A_1_2_LineColBegin); mResult.SecondColEnd = new HTuple(hv_A_1_2_LineColEnd); mResult.SecondRowBegin = new HTuple(hv_A_1_2_LineRowBegin); mResult.SecondRowEnd = new HTuple(hv_A_1_2_LineRowEnd); } //********* HOperatorSet.DistanceCc(ho_A_1_1_Line, ho_A_1_2_Line, "point_to_segment", out hv_DistanceMin, out hv_DistanceMax); mResult.Distance = new HTuple(hv_DistanceMin); /* //****Mark_A_C //STD Mark_A_C_ 位置 hv_STD_Mark_A_C_Row = 1400; hv_STD_Mark_A_C_Col = 1000; //STD 向量 STD_Mark_A_C_ hv_STD_Mark_A_C_V_Row = hv_STD_Mark_A_C_Row - hv_STD_Row; hv_STD_Mark_A_C_V_Col = hv_STD_Mark_A_C_Col - hv_STD_Col; //Mark_A_C_X, Mark_A_C_Y 分量 hv_Mark_A_C_X = (hv_STD_Mark_A_C_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_Mark_A_C_V_Row * (hv_Img_Rotate_Angle.TupleSin() )); hv_Mark_A_C_Y = (hv_STD_Mark_A_C_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_Mark_A_C_V_Col * (hv_Img_Rotate_Angle.TupleSin() )); //目前圖形 Mark_A_C_ 位置 hv_Mark_A_C_Pos_Row = (hv_STD_Row + hv_Mark_A_C_Y) + hv_OffsetRow; hv_Mark_A_C_Pos_Col = (hv_STD_Col + hv_Mark_A_C_X) + hv_OffsetCol; //****Mark_A_Start //STD Mark_A_Start_ 位置 hv_STD_Mark_A_Start_Row = 1400; hv_STD_Mark_A_Start_Col = 618; //STD 向量 STD_Mark_A_Start_ hv_STD_Mark_A_Start_V_Row = hv_STD_Mark_A_Start_Row - hv_STD_Row; hv_STD_Mark_A_Start_V_Col = hv_STD_Mark_A_Start_Col - hv_STD_Col; //Mark_A_Start_X, Mark_A_Start_Y 分量 hv_Mark_A_Start_X = (hv_STD_Mark_A_Start_V_Col * (hv_Img_Rotate_Angle.TupleCos() )) + (hv_STD_Mark_A_Start_V_Row * (hv_Img_Rotate_Angle.TupleSin())); hv_Mark_A_Start_Y = (hv_STD_Mark_A_Start_V_Row * (hv_Img_Rotate_Angle.TupleCos() )) - (hv_STD_Mark_A_Start_V_Col * (hv_Img_Rotate_Angle.TupleSin())); //目前圖形 Mark_A_Start_ 位置 hv_Mark_A_Start_Pos_Row = (hv_STD_Row + hv_Mark_A_Start_Y) + hv_OffsetRow; hv_Mark_A_Start_Pos_Col = (hv_STD_Col + hv_Mark_A_Start_X) + hv_OffsetCol; //****Mark_A_End //STD Mark_A_End_ 位置 hv_STD_Mark_A_End_Row = 1400; hv_STD_Mark_A_End_Col = 1415; //STD 向量 STD_Mark_A_End_ hv_STD_Mark_A_End_V_Row = hv_STD_Mark_A_End_Row - hv_STD_Row; hv_STD_Mark_A_End_V_Col = hv_STD_Mark_A_End_Col - hv_STD_Col; //Mark_A_End_X, Mark_A_End_Y 分量 hv_Mark_A_End_X = (hv_STD_Mark_A_End_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_Mark_A_End_V_Row * (hv_Img_Rotate_Angle.TupleSin() )); hv_Mark_A_End_Y = (hv_STD_Mark_A_End_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_Mark_A_End_V_Col * (hv_Img_Rotate_Angle.TupleSin() )); //目前圖形 Mark_A_End_ 位置 hv_Mark_A_End_Pos_Row = (hv_STD_Row + hv_Mark_A_End_Y) + hv_OffsetRow; hv_Mark_A_End_Pos_Col = (hv_STD_Col + hv_Mark_A_End_X) + hv_OffsetCol; HOperatorSet.SetTposition(hv_WindowHandle, hv_Mark_A_C_Pos_Row - 25, hv_Mark_A_C_Pos_Col); HOperatorSet.WriteString(hv_WindowHandle, "A"); HOperatorSet.DispArrow(hv_WindowHandle, hv_Mark_A_C_Pos_Row, hv_Mark_A_C_Pos_Col - 50, hv_Mark_A_Start_Pos_Row, hv_Mark_A_Start_Pos_Col, 5); HOperatorSet.DispArrow(hv_WindowHandle, hv_Mark_A_C_Pos_Row, hv_Mark_A_C_Pos_Col + 50, hv_Mark_A_End_Pos_Row, hv_Mark_A_End_Pos_Col, 5); */ //********* hv_MeasureReasult = hv_DistanceMin.Clone(); //*****A End //ho_Image.Dispose(); //ho_AllModelContours.Dispose(); ho_A_1_1_Rectangle.Dispose(); ho_A_1_1_Image.Dispose(); ho_A_1_1_Edges.Dispose(); ho_A_1_1_Contours.Dispose(); ho_A_1_1_SelectedLines.Dispose(); ho_A_1_1_Line.Dispose(); ho_A_1_2_Rectangle.Dispose(); ho_A_1_2_Image.Dispose(); ho_A_1_2_Edges.Dispose(); ho_A_1_2_Contours.Dispose(); ho_A_1_2_SelectedLines.Dispose(); ho_A_1_2_Line.Dispose(); return mResult; }
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(); } }
private void action() { // Stack for temporary objects HObject[] OTemp = new HObject[20]; // Local iconic variables HObject ho_Rectangle, ho_ImageReduced; HObject ho_Region, ho_ConnectedRegions, ho_SelectedRegions; HObject ho_Rectangle1, ho_ImageRotate, ho_Region1, ho_RegionAffineTrans; HObject ho_Region2, ho_RegionUnionu, ho_RegionUniond, ho_Contours; HObject ho_SelectedContours, ho_SmoothedContours1, ho_ClippedContoursu; HObject ho_ClippedContoursd, ho_Rectangle2, ho_ClippedContoursuObjectSelected = null; HObject ho_ClippedContoursdObjectSelected = null, ho_Circle = null; HObject ho_Circle1 = null, ho_ConnectedRegions1, ho_SelectedRegions1; HObject ho_SortedRegionssdj, ho_Region3, ho_ConnectedRegions2; HObject ho_SelectedRegions2, ho_SortedRegionsxdj, ho_RegionLines = null; HObject ho_RegionAffineTrans1; // Local control variables HTuple hv_Width = null, hv_Height = null; HTuple hv_jd = null, hv_Row = null, hv_Column = null, hv_Phi = null; HTuple hv_Length1 = null, hv_Length2 = null, hv_HomMat2DIdentity = null; HTuple hv_HomMat2DRotate = null, hv_Row1 = null, hv_Column1 = null; HTuple hv_Row2 = null, hv_Column2 = null, hv_Rown = null; HTuple hv_Rowu = null, hv_Columnu = null, hv_Phiu = null; HTuple hv_Length1u = null, hv_Length2u = null, hv_MeasureHandleu = null; HTuple hv_GrayValuesu = null, hv_Sigmau = null, hv_Functionu = null; HTuple hv_SmoothedFunctionu = null, hv_FirstDerivativeu = null; HTuple hv_ZeroCrossingsu = null, hv_RowStartu = null, hv_ColStartu = null; HTuple hv_RowEndu = null, hv_ColEndu = null, hv_iu = null; HTuple hv_Rowd = null, hv_Columnd = null, hv_Phid = null; HTuple hv_Length1d = null, hv_Length2d = null, hv_MeasureHandled = null; HTuple hv_GrayValuesd = null, hv_Sigmad = null, hv_Functiond = null; HTuple hv_SmoothedFunctiond = null, hv_FirstDerivatived = null; HTuple hv_ZeroCrossingsd = null, hv_RowStartd = null, hv_ColStartd = null; HTuple hv_RowEndd = null, hv_ColEndd = null, hv_id = null; HTuple hv_RowIntuAll = null, hv_ColumnIntuAll = null, hv_u1 = null; HTuple hv_i = null, hv_Length = new HTuple(), hv_Indices1 = new HTuple(); HTuple hv_RowIntu = new HTuple(), hv_ColumnIntu = new HTuple(); HTuple hv_IsOverlappingu = new HTuple(), hv_RowIntdAll = null; HTuple hv_ColumnIntdAll = null, hv_j = null, hv_RowIntd = new HTuple(); HTuple hv_ColumnIntd = new HTuple(), hv_IsOverlappingd = new HTuple(); HTuple hv_Sortedu = null, hv_Sortedd = null, hv_RowIntuu = null; HTuple hv_ColumnIntuu = null, hv_RowIntud = null, hv_ColumnIntud = null; HTuple hv_uu = null, hv_ud = null, hv_ub1 = null, hv_RowIntdu = null; HTuple hv_ColumnIntdu = null, hv_RowIntdd = null, hv_ColumnIntdd = null; HTuple hv_du = null, hv_dd = null, hv_db1 = null, hv_Areasdj = null; HTuple hv_Rowsdj = null, hv_Columnsdj = null, hv_sdj = null; HTuple hv_Areaxdj = null, hv_Rowxdj = null, hv_Columnxdj = null; HTuple hv_xdj = null, hv_djsl1 = null, hv_djsl = null; HTuple hv_Mean1 = null, hv_Mean2 = null, hv_Mean3 = null; HTuple hv_Mean4 = null, hv_Columnuu = null, hv_Columndd = null; HTuple hv_AngleAll = null, hv_l = null, hv_Angle = new HTuple(); HTuple hv_DegAll = null, hv_Mean5 = null, hv_Mean8 = null; HTuple hv_Columnuum = null, hv_uum1 = null, hv_uus = null; HTuple hv_Columnddm = null, hv_ddm = null, hv_dds = null; HTuple hv_ColumnmArr = null, hv_Mean9 = null, hv_Mean10 = null; HTuple hv_Sorteda = null, hv_Ai = null, hv_Ax = null, hv_pixeldist = null; HTuple hv_L = null, hv_D = null, hv_Dmax = null, hv_Dmin = null; HTuple hv_d = null, hv_dmax = null, hv_dmin = null, hv_LS = null; HTuple hv_HomMat2DIdentity1 = null, hv_HomMat2DRotate1 = 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_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_Rectangle1); HOperatorSet.GenEmptyObj(out ho_ImageRotate); HOperatorSet.GenEmptyObj(out ho_Region1); HOperatorSet.GenEmptyObj(out ho_RegionAffineTrans); HOperatorSet.GenEmptyObj(out ho_Region2); HOperatorSet.GenEmptyObj(out ho_RegionUnionu); HOperatorSet.GenEmptyObj(out ho_RegionUniond); HOperatorSet.GenEmptyObj(out ho_Contours); HOperatorSet.GenEmptyObj(out ho_SelectedContours); HOperatorSet.GenEmptyObj(out ho_SmoothedContours1); HOperatorSet.GenEmptyObj(out ho_ClippedContoursu); HOperatorSet.GenEmptyObj(out ho_ClippedContoursd); HOperatorSet.GenEmptyObj(out ho_Rectangle2); HOperatorSet.GenEmptyObj(out ho_ClippedContoursuObjectSelected); HOperatorSet.GenEmptyObj(out ho_ClippedContoursdObjectSelected); HOperatorSet.GenEmptyObj(out ho_Circle); HOperatorSet.GenEmptyObj(out ho_Circle1); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1); HOperatorSet.GenEmptyObj(out ho_SelectedRegions1); HOperatorSet.GenEmptyObj(out ho_SortedRegionssdj); HOperatorSet.GenEmptyObj(out ho_Region3); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions2); HOperatorSet.GenEmptyObj(out ho_SelectedRegions2); HOperatorSet.GenEmptyObj(out ho_SortedRegionsxdj); HOperatorSet.GenEmptyObj(out ho_RegionLines); HOperatorSet.GenEmptyObj(out ho_RegionAffineTrans1); 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_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(Image, ho_Rectangle, out ho_ImageReduced); //* ho_Region.Dispose(); HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, 0, 128); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions); ho_SelectedRegions.Dispose(); HOperatorSet.SelectShapeStd(ho_ConnectedRegions, out ho_SelectedRegions, "max_area", 70); //hv_jd = 0; HOperatorSet.SmallestRectangle2(ho_SelectedRegions, 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, hv_Length2); ho_ImageRotate.Dispose(); HOperatorSet.RotateImage(Image, out ho_ImageRotate, -(hv_Phi.TupleDeg()), "constant"); //* ho_Region1.Dispose(); HOperatorSet.Threshold(ho_ImageRotate, out ho_Region1, 0, 128); //* HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity); HOperatorSet.HomMat2dRotate(hv_HomMat2DIdentity, -hv_Phi, hv_Height / 2, hv_Width / 2, out hv_HomMat2DRotate); ho_RegionAffineTrans.Dispose(); HOperatorSet.AffineTransRegion(ho_Rectangle1, out ho_RegionAffineTrans, hv_HomMat2DRotate, "nearest_neighbor"); HOperatorSet.SmallestRectangle1(ho_RegionAffineTrans, out hv_Row1, out hv_Column1, out hv_Row2, out hv_Column2); //*** ho_Region2.Dispose(); HOperatorSet.GenRegionPoints(out ho_Region2, hv_Row1, hv_Column1); ho_RegionUnionu.Dispose(); HOperatorSet.Union1(ho_Region2, out ho_RegionUnionu); ho_RegionUniond.Dispose(); HOperatorSet.Union1(ho_Region2, out ho_RegionUniond); ho_Contours.Dispose(); HOperatorSet.GenContourRegionXld(ho_Region1, out ho_Contours, "border"); ho_SelectedContours.Dispose(); HOperatorSet.SelectContoursXld(ho_Contours, out ho_SelectedContours, "contour_length", 20, 2000000, -1, 1); ho_SmoothedContours1.Dispose(); HOperatorSet.SmoothContoursXld(ho_SelectedContours, out ho_SmoothedContours1, 13); //* hv_Rown = (hv_Row1 + hv_Row2) / 2; ho_ClippedContoursu.Dispose(); HOperatorSet.ClipContoursXld(ho_SmoothedContours1, out ho_ClippedContoursu, hv_Row1, hv_Column1, hv_Rown, hv_Column2); ho_ClippedContoursd.Dispose(); HOperatorSet.ClipContoursXld(ho_SmoothedContours1, out ho_ClippedContoursd, hv_Rown, hv_Column1, hv_Row2, hv_Column2); hv_Rowu = (hv_Row1 + hv_Rown) / 2; hv_Columnu = (hv_Column1 + hv_Column2) / 2; hv_Phiu = 0; hv_Length1u = (hv_Column2 - hv_Column1) / 2; hv_Length2u = (hv_Rown - hv_Row1) / 2; //dev_set_draw ('margin') HOperatorSet.GenMeasureRectangle2(hv_Rowu, hv_Columnu, hv_Phiu, hv_Length1u, hv_Length2u, hv_Width, hv_Height, "bilinear", out hv_MeasureHandleu); ho_Rectangle2.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle2, hv_Rowu, hv_Columnu, hv_Phiu, hv_Length1u, hv_Length2u); HOperatorSet.MeasureProjection(ho_ImageRotate, hv_MeasureHandleu, out hv_GrayValuesu); hv_Sigmau = 5; HOperatorSet.CreateFunct1dArray(hv_GrayValuesu, out hv_Functionu); HOperatorSet.SmoothFunct1dGauss(hv_Functionu, hv_Sigmau, out hv_SmoothedFunctionu); HOperatorSet.DerivateFunct1d(hv_SmoothedFunctionu, "first", out hv_FirstDerivativeu); HOperatorSet.ZeroCrossingsFunct1d(hv_FirstDerivativeu, out hv_ZeroCrossingsu); hv_RowStartu = hv_Rowu - (hv_Length2u / 2); hv_ColStartu = (hv_Columnu - hv_Length1u) + hv_ZeroCrossingsu; hv_RowEndu = hv_Rowu + (hv_Length2u / 2); hv_ColEndu = (hv_Columnu - hv_Length1u) + hv_ZeroCrossingsu; hv_iu = new HTuple(hv_ZeroCrossingsu.TupleLength()); hv_Rowd = (hv_Rown + hv_Row2) / 2; hv_Columnd = (hv_Column1 + hv_Column2) / 2; hv_Phid = 0; hv_Length1d = (hv_Column2 - hv_Column1) / 2; hv_Length2d = (hv_Row2 - hv_Rown) / 2; HOperatorSet.GenMeasureRectangle2(hv_Rowd, hv_Columnd, hv_Phid, hv_Length1d, hv_Length2d, hv_Width, hv_Height, "bilinear", out hv_MeasureHandled); HOperatorSet.MeasureProjection(ho_ImageRotate, hv_MeasureHandled, out hv_GrayValuesd); // hv_Sigmad = 5; HOperatorSet.CreateFunct1dArray(hv_GrayValuesd, out hv_Functiond); HOperatorSet.SmoothFunct1dGauss(hv_Functiond, hv_Sigmad, out hv_SmoothedFunctiond); HOperatorSet.DerivateFunct1d(hv_SmoothedFunctiond, "first", out hv_FirstDerivatived); HOperatorSet.ZeroCrossingsFunct1d(hv_FirstDerivatived, out hv_ZeroCrossingsd); HOperatorSet.CloseMeasure(hv_MeasureHandleu); HOperatorSet.CloseMeasure(hv_MeasureHandled); hv_RowStartd = hv_Rowd - (hv_Length2d / 2); hv_ColStartd = (hv_Columnd - hv_Length1d) + hv_ZeroCrossingsd; hv_RowEndd = hv_Rowd + (hv_Length2d / 2); hv_ColEndd = (hv_Columnd - hv_Length1d) + hv_ZeroCrossingsd; hv_id = new HTuple(hv_ZeroCrossingsd.TupleLength()); hv_RowIntuAll = new HTuple(); hv_ColumnIntuAll = new HTuple(); hv_u1 = 0; HTuple end_val96 = hv_iu - 1; HTuple step_val96 = 1; for (hv_i = 0; hv_i.Continue(end_val96, step_val96); hv_i = hv_i.TupleAdd(step_val96)) { HOperatorSet.LengthXld(ho_ClippedContoursu, out hv_Length); HOperatorSet.TupleFind(hv_Length, hv_Length.TupleMax(), out hv_Indices1); ho_ClippedContoursuObjectSelected.Dispose(); HOperatorSet.SelectObj(ho_ClippedContoursu, out ho_ClippedContoursuObjectSelected, hv_Indices1 + 1); HOperatorSet.IntersectionLineContourXld(ho_ClippedContoursuObjectSelected, hv_RowStartu, hv_ColStartu.TupleSelect(hv_i), hv_RowEndu, hv_ColEndu.TupleSelect( hv_i), out hv_RowIntu, out hv_ColumnIntu, out hv_IsOverlappingu); //*gen_cross_contour_xld (Cross1u, RowIntu, ColumnIntu, 12, 0)//根据每个输入点交叉的形状创键一个XLD轮廓(contour) hv_RowIntuAll = hv_RowIntuAll.TupleConcat(hv_RowIntu); hv_ColumnIntuAll = hv_ColumnIntuAll.TupleConcat(hv_ColumnIntu); hv_u1 = hv_u1 + 1; } hv_RowIntdAll = new HTuple(); hv_ColumnIntdAll = new HTuple(); HTuple end_val109 = hv_id - 1; HTuple step_val109 = 1; for (hv_j = 0; hv_j.Continue(end_val109, step_val109); hv_j = hv_j.TupleAdd(step_val109)) { HOperatorSet.LengthXld(ho_ClippedContoursd, out hv_Length); HOperatorSet.TupleFind(hv_Length, hv_Length.TupleMax(), out hv_Indices1); ho_ClippedContoursdObjectSelected.Dispose(); HOperatorSet.SelectObj(ho_ClippedContoursd, out ho_ClippedContoursdObjectSelected, hv_Indices1 + 1); HOperatorSet.IntersectionLineContourXld(ho_ClippedContoursdObjectSelected, hv_RowStartd, hv_ColStartd.TupleSelect(hv_j), hv_RowEndd, hv_ColEndd.TupleSelect( hv_j), out hv_RowIntd, out hv_ColumnIntd, out hv_IsOverlappingd); //*gen_cross_contour_xld (Cross1d, RowIntd, ColumnIntd, 12, 0)//根据每个输入点交叉的形状创键一个XLD轮廓(contour) hv_RowIntdAll = hv_RowIntdAll.TupleConcat(hv_RowIntd); hv_ColumnIntdAll = hv_ColumnIntdAll.TupleConcat(hv_ColumnIntd); } //stop () HOperatorSet.TupleSort(hv_RowIntuAll, out hv_Sortedu); HOperatorSet.TupleSort(hv_RowIntdAll, out hv_Sortedd); hv_RowIntuu = new HTuple(); hv_ColumnIntuu = new HTuple(); hv_RowIntud = new HTuple(); hv_ColumnIntud = new HTuple(); hv_uu = 0; hv_ud = 0; for (hv_ub1 = 0; (int)hv_ub1 <= (int)((new HTuple(hv_Sortedu.TupleLength())) - 1); hv_ub1 = (int)hv_ub1 + 1) { if ((int)(new HTuple(((hv_RowIntuAll.TupleSelect(hv_ub1))).TupleLess(((((hv_Sortedu.TupleSelect( 0)) * 2) + (hv_Sortedu.TupleSelect((new HTuple(hv_Sortedu.TupleLength())) - 1))) / 3) + 3))) != 0) { hv_RowIntuu = hv_RowIntuu.TupleConcat(hv_RowIntuAll.TupleSelect(hv_ub1)); hv_ColumnIntuu = hv_ColumnIntuu.TupleConcat(hv_ColumnIntuAll.TupleSelect( hv_ub1)); ho_Circle.Dispose(); HOperatorSet.GenCircle(out ho_Circle, hv_RowIntuAll.TupleSelect(hv_ub1), hv_ColumnIntuAll.TupleSelect(hv_ub1), 1); { HObject ExpTmpOutVar_0; HOperatorSet.Union2(ho_RegionUnionu, ho_Circle, out ExpTmpOutVar_0); ho_RegionUnionu.Dispose(); ho_RegionUnionu = ExpTmpOutVar_0; } hv_uu = hv_uu + 1; } if ((int)(new HTuple(((hv_RowIntuAll.TupleSelect(hv_ub1))).TupleGreater((((hv_Sortedu.TupleSelect( 0)) + ((hv_Sortedu.TupleSelect((new HTuple(hv_Sortedu.TupleLength())) - 1)) * 2)) / 3) - 3))) != 0) { hv_RowIntud = hv_RowIntud.TupleConcat(hv_RowIntuAll.TupleSelect(hv_ub1)); hv_ColumnIntud = hv_ColumnIntud.TupleConcat(hv_ColumnIntuAll.TupleSelect( hv_ub1)); hv_ud = hv_ud + 1; } } //stop () hv_RowIntdu = new HTuple(); hv_ColumnIntdu = new HTuple(); hv_RowIntdd = new HTuple(); hv_ColumnIntdd = new HTuple(); hv_du = 0; hv_dd = 0; for (hv_db1 = 0; (int)hv_db1 <= (int)((new HTuple(hv_Sortedd.TupleLength())) - 1); hv_db1 = (int)hv_db1 + 1) { if ((int)(new HTuple(((hv_RowIntdAll.TupleSelect(hv_db1))).TupleLess(((((hv_Sortedd.TupleSelect( 0)) * 2) + (hv_Sortedd.TupleSelect((new HTuple(hv_Sortedd.TupleLength())) - 1))) / 3) + 3))) != 0) { hv_RowIntdu = hv_RowIntdu.TupleConcat(hv_RowIntdAll.TupleSelect(hv_db1)); hv_ColumnIntdu = hv_ColumnIntdu.TupleConcat(hv_ColumnIntdAll.TupleSelect( hv_db1)); hv_du = hv_du + 1; } if ((int)(new HTuple(((hv_RowIntdAll.TupleSelect(hv_db1))).TupleGreater((((hv_Sortedd.TupleSelect( 0)) + ((hv_Sortedd.TupleSelect((new HTuple(hv_Sortedd.TupleLength())) - 1)) * 2)) / 3) - 3))) != 0) { hv_RowIntdd = hv_RowIntdd.TupleConcat(hv_RowIntdAll.TupleSelect(hv_db1)); hv_ColumnIntdd = hv_ColumnIntdd.TupleConcat(hv_ColumnIntdAll.TupleSelect( hv_db1)); ho_Circle1.Dispose(); HOperatorSet.GenCircle(out ho_Circle1, hv_RowIntdAll.TupleSelect(hv_db1), hv_ColumnIntdAll.TupleSelect(hv_db1), 1); { HObject ExpTmpOutVar_0; HOperatorSet.Union2(ho_RegionUniond, ho_Circle1, out ExpTmpOutVar_0); ho_RegionUniond.Dispose(); ho_RegionUniond = ExpTmpOutVar_0; } hv_dd = hv_dd + 1; } } ho_ConnectedRegions1.Dispose(); HOperatorSet.Connection(ho_RegionUnionu, out ho_ConnectedRegions1); ho_SelectedRegions1.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions1, out ho_SelectedRegions1, "area", "and", 3, 99999); ho_SortedRegionssdj.Dispose(); HOperatorSet.SortRegion(ho_SelectedRegions1, out ho_SortedRegionssdj, "upper_left", "true", "column"); HOperatorSet.AreaCenter(ho_SortedRegionssdj, out hv_Areasdj, out hv_Rowsdj, out hv_Columnsdj); ho_Region3.Dispose(); HOperatorSet.GenRegionPoints(out ho_Region3, hv_Rowsdj.TupleSelect(0), hv_Columnsdj.TupleSelect( 0)); hv_sdj = new HTuple(hv_Columnsdj.TupleLength()); ho_ConnectedRegions2.Dispose(); HOperatorSet.Connection(ho_RegionUniond, out ho_ConnectedRegions2); ho_SelectedRegions2.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions2, out ho_SelectedRegions2, "area", "and", 3, 99999); ho_SortedRegionsxdj.Dispose(); HOperatorSet.SortRegion(ho_SelectedRegions2, out ho_SortedRegionsxdj, "upper_left", "true", "column"); HOperatorSet.AreaCenter(ho_SortedRegionsxdj, out hv_Areaxdj, out hv_Rowxdj, out hv_Columnxdj); hv_xdj = new HTuple(hv_Columnxdj.TupleLength()); hv_djsl1 = new HTuple(); hv_djsl1 = hv_djsl1.TupleConcat(hv_sdj); hv_djsl1 = hv_djsl1.TupleConcat(hv_xdj); HOperatorSet.TupleMin(hv_djsl1, out hv_djsl); HOperatorSet.TupleMean(hv_RowIntuu, out hv_Mean1); HOperatorSet.TupleMean(hv_RowIntud, out hv_Mean2); HOperatorSet.TupleMean(hv_RowIntdu, out hv_Mean3); HOperatorSet.TupleMean(hv_RowIntdd, out hv_Mean4); hv_Columnuu = new HTuple(); hv_Columndd = new HTuple(); hv_AngleAll = new HTuple(); HTuple end_val191 = hv_djsl - 1; HTuple step_val191 = 1; for (hv_l = 0; hv_l.Continue(end_val191, step_val191); hv_l = hv_l.TupleAdd(step_val191)) { ho_RegionLines.Dispose(); HOperatorSet.GenRegionLine(out ho_RegionLines, hv_Rowsdj.TupleSelect(hv_l), hv_Columnsdj.TupleSelect(hv_l), hv_Rowxdj.TupleSelect(hv_l), hv_Columnxdj.TupleSelect( hv_l)); { HObject ExpTmpOutVar_0; HOperatorSet.Union2(ho_Region3, ho_RegionLines, out ExpTmpOutVar_0); ho_Region3.Dispose(); ho_Region3 = ExpTmpOutVar_0; } HOperatorSet.AngleLx(hv_Rowsdj.TupleSelect(hv_l), hv_Columnsdj.TupleSelect( hv_l), hv_Rowxdj.TupleSelect(hv_l), hv_Columnxdj.TupleSelect(hv_l), out hv_Angle); hv_AngleAll = hv_AngleAll.TupleConcat(0 - hv_Angle); if ((int)(new HTuple(hv_l.TupleGreater(0))) != 0) { hv_Columnuu = hv_Columnuu.TupleConcat((hv_ColumnIntuu.TupleSelect(hv_l)) - (hv_ColumnIntuu.TupleSelect( hv_l - 1))); hv_Columndd = hv_Columndd.TupleConcat((hv_ColumnIntdd.TupleSelect(hv_l)) - (hv_ColumnIntdd.TupleSelect( hv_l - 1))); } //stop () } HOperatorSet.TupleDeg(hv_AngleAll, out hv_DegAll); HOperatorSet.TupleMean(hv_Columnuu, out hv_Mean5); HOperatorSet.TupleMean(hv_Columndd, out hv_Mean8); //**过滤 hv_Columnuum = new HTuple(); hv_uum1 = 0; for (hv_uus = 0; (int)hv_uus <= (int)((new HTuple(hv_Columnuu.TupleLength())) - 1); hv_uus = (int)hv_uus + 1) { if ((int)((new HTuple(hv_Columnuu.TupleLessEqual(hv_Mean5 * 1.1))).TupleAnd(new HTuple(hv_Columnuu.TupleGreaterEqual( hv_Mean5 * 0.9)))) != 0) { hv_Columnuum = hv_Columnuum.TupleConcat(hv_Columnuu.TupleSelect(hv_uus)); hv_uum1 = hv_uum1 + 1; } } hv_Columnddm = new HTuple(); hv_ddm = 0; for (hv_dds = 0; (int)hv_dds <= (int)((new HTuple(hv_Columndd.TupleLength())) - 1); hv_dds = (int)hv_dds + 1) { if ((int)((new HTuple(hv_Columndd.TupleLessEqual(hv_Mean8 * 1.1))).TupleAnd(new HTuple(hv_Columndd.TupleGreaterEqual( hv_Mean8 * 0.9)))) != 0) { hv_Columnddm = hv_Columnddm.TupleConcat(hv_Columndd.TupleSelect(hv_dds)); hv_ddm = hv_ddm + 1; } } hv_ColumnmArr = new HTuple(); hv_ColumnmArr = hv_ColumnmArr.TupleConcat(hv_Columnuum); hv_ColumnmArr = hv_ColumnmArr.TupleConcat(hv_Columnddm); HOperatorSet.TupleMean(hv_ColumnmArr, out hv_Mean9); HOperatorSet.TupleMean(hv_DegAll, out hv_Mean10); HOperatorSet.TupleSort(hv_DegAll, out hv_Sorteda); hv_Ai = hv_Sorteda[0]; hv_Ax = hv_Sorteda[(new HTuple(hv_Sorteda.TupleLength())) - 1]; hv_pixeldist = 1; hv_L = ((hv_Columnsdj.TupleSelect(hv_djsl - 1)) - (hv_Columnsdj.TupleSelect(0))) * hv_pixeldist; hv_D = (hv_Mean4 - hv_Mean1) * hv_pixeldist; hv_Dmax = ((hv_RowIntdd.TupleMax()) - (hv_RowIntuu.TupleMin())) * hv_pixeldist; hv_Dmin = ((hv_RowIntdd.TupleMin()) - (hv_RowIntuu.TupleMax())) * hv_pixeldist; hv_d = (hv_Mean3 - hv_Mean2) * hv_pixeldist; hv_dmax = ((hv_RowIntdu.TupleMax()) - (hv_RowIntud.TupleMin())) * hv_pixeldist; hv_dmin = ((hv_RowIntdu.TupleMin()) - (hv_RowIntud.TupleMax())) * hv_pixeldist; hv_LS = hv_Mean9 * hv_pixeldist; //*区域旋转 HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity1); HOperatorSet.HomMat2dRotate(hv_HomMat2DIdentity1, hv_Phi, hv_Height / 2, hv_Width / 2, out hv_HomMat2DRotate1); ho_RegionAffineTrans1.Dispose(); HOperatorSet.AffineTransRegion(ho_Region3, out ho_RegionAffineTrans1, hv_HomMat2DRotate1, "nearest_neighbor"); HOperatorSet.Union1(ho_RegionAffineTrans1, out RegionToDisp); HTuple hv_result = GetHv_result(); hv_result = hv_result.TupleConcat("齿数"); hv_result = hv_result.TupleConcat(hv_djsl.D); hv_result = hv_result.TupleConcat("螺纹长度"); hv_result = hv_result.TupleConcat(hv_L.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹大径平均值"); hv_result = hv_result.TupleConcat(hv_D.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹大径最小值"); hv_result = hv_result.TupleConcat(hv_Dmin.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹大径最大值"); hv_result = hv_result.TupleConcat(hv_Dmax.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹小径平均值"); hv_result = hv_result.TupleConcat(hv_d.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹小径最小值"); hv_result = hv_result.TupleConcat(hv_dmin.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹小径最大值"); hv_result = hv_result.TupleConcat(hv_dmax.D * pixeldist); hv_result = hv_result.TupleConcat("牙距"); hv_result = hv_result.TupleConcat(hv_LS.D * pixeldist); hv_result = hv_result.TupleConcat("平均牙倾角"); hv_result = hv_result.TupleConcat(hv_Mean10.D); hv_result = hv_result.TupleConcat("最小牙倾角"); hv_result = hv_result.TupleConcat(hv_Ai.D); hv_result = hv_result.TupleConcat("最大牙倾角"); hv_result = hv_result.TupleConcat(hv_Ax.D); result = hv_result.Clone(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Region.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_Rectangle1.Dispose(); ho_ImageRotate.Dispose(); ho_Region1.Dispose(); ho_RegionAffineTrans.Dispose(); ho_Region2.Dispose(); ho_RegionUnionu.Dispose(); ho_RegionUniond.Dispose(); ho_Contours.Dispose(); ho_SelectedContours.Dispose(); ho_SmoothedContours1.Dispose(); ho_ClippedContoursu.Dispose(); ho_ClippedContoursd.Dispose(); ho_Rectangle2.Dispose(); ho_ClippedContoursuObjectSelected.Dispose(); ho_ClippedContoursdObjectSelected.Dispose(); ho_Circle.Dispose(); ho_Circle1.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions1.Dispose(); ho_SortedRegionssdj.Dispose(); ho_Region3.Dispose(); ho_ConnectedRegions2.Dispose(); ho_SelectedRegions2.Dispose(); ho_SortedRegionsxdj.Dispose(); ho_RegionLines.Dispose(); ho_RegionAffineTrans1.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(0); 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(0); 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(0); 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(0); result = hv_result.Clone(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Region.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_Rectangle1.Dispose(); ho_ImageRotate.Dispose(); ho_Region1.Dispose(); ho_RegionAffineTrans.Dispose(); ho_Region2.Dispose(); ho_RegionUnionu.Dispose(); ho_RegionUniond.Dispose(); ho_Contours.Dispose(); ho_SelectedContours.Dispose(); ho_SmoothedContours1.Dispose(); ho_ClippedContoursu.Dispose(); ho_ClippedContoursd.Dispose(); ho_Rectangle2.Dispose(); ho_ClippedContoursuObjectSelected.Dispose(); ho_ClippedContoursdObjectSelected.Dispose(); ho_Circle.Dispose(); ho_Circle1.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions1.Dispose(); ho_SortedRegionssdj.Dispose(); ho_Region3.Dispose(); ho_ConnectedRegions2.Dispose(); ho_SelectedRegions2.Dispose(); ho_SortedRegionsxdj.Dispose(); ho_RegionLines.Dispose(); ho_RegionAffineTrans1.Dispose(); algorithm.Region.Dispose(); } finally { ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Region.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_Rectangle1.Dispose(); ho_ImageRotate.Dispose(); ho_Region1.Dispose(); ho_RegionAffineTrans.Dispose(); ho_Region2.Dispose(); ho_RegionUnionu.Dispose(); ho_RegionUniond.Dispose(); ho_Contours.Dispose(); ho_SelectedContours.Dispose(); ho_SmoothedContours1.Dispose(); ho_ClippedContoursu.Dispose(); ho_ClippedContoursd.Dispose(); ho_Rectangle2.Dispose(); ho_ClippedContoursuObjectSelected.Dispose(); ho_ClippedContoursdObjectSelected.Dispose(); ho_Circle.Dispose(); ho_Circle1.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions1.Dispose(); ho_SortedRegionssdj.Dispose(); ho_Region3.Dispose(); ho_ConnectedRegions2.Dispose(); ho_SelectedRegions2.Dispose(); ho_SortedRegionsxdj.Dispose(); ho_RegionLines.Dispose(); ho_RegionAffineTrans1.Dispose(); algorithm.Region.Dispose(); } }
// Procedures public void region_get_corner(HObject ho_Image, HObject ho_Rectangle, HTuple hv_WindowHandle, out HTuple hv_Rows, out HTuple hv_Cols) { // Local iconic variables HObject ho_RegionsLine3 = null, ho_RegionIntersection = null; // Local control variables HTuple hv_Width = new HTuple(), hv_Height = new HTuple(); HTuple hv_LengthDiagonal = new HTuple(), hv_HalfDiagonal = new HTuple(); HTuple hv_Polarity = new HTuple(), hv_Index = new HTuple(); HTuple hv_RegionIntersectionArea = new HTuple(), hv_Row = new HTuple(); HTuple hv_Column = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_RegionsLine3); HOperatorSet.GenEmptyObj(out ho_RegionIntersection); hv_Rows = new HTuple(); hv_Cols = new HTuple(); //* Gen Line In Loop and juage cross hv_Width.Dispose(); hv_Height.Dispose(); HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); using (HDevDisposeHelper dh = new HDevDisposeHelper()) { hv_LengthDiagonal.Dispose(); HOperatorSet.TupleSqrt((hv_Width * hv_Width) + (hv_Height * hv_Height), out hv_LengthDiagonal); } hv_HalfDiagonal.Dispose(); using (HDevDisposeHelper dh = new HDevDisposeHelper()) { hv_HalfDiagonal = hv_LengthDiagonal / 2; } //PolarityDegree := 1 //PolarityLength := 1 //Polarity := 3 hv_Rows.Dispose(); hv_Rows = new HTuple(); hv_Cols.Dispose(); hv_Cols = new HTuple(); for (hv_Polarity = 1; (int)hv_Polarity <= 4; hv_Polarity = (int)hv_Polarity + 1) { if ((int)(new HTuple(hv_Polarity.TupleEqual(1))) != 0) { HTuple end_val11 = hv_HalfDiagonal; HTuple step_val11 = 1; for (hv_Index = 0; hv_Index.Continue(end_val11, step_val11); hv_Index = hv_Index.TupleAdd(step_val11)) { using (HDevDisposeHelper dh = new HDevDisposeHelper()) { ho_RegionsLine3.Dispose(); HOperatorSet.GenRegionHline(out ho_RegionsLine3, (new HTuple(-45)).TupleRad() , 1 * ((1 * hv_HalfDiagonal) - hv_Index)); } ho_RegionIntersection.Dispose(); HOperatorSet.Intersection(ho_RegionsLine3, ho_Rectangle, out ho_RegionIntersection ); hv_RegionIntersectionArea.Dispose(); hv_Row.Dispose(); hv_Column.Dispose(); HOperatorSet.AreaCenter(ho_RegionIntersection, out hv_RegionIntersectionArea, out hv_Row, out hv_Column); if ((int)(new HTuple(hv_RegionIntersectionArea.TupleGreater(0))) != 0) { if (hv_Rows == null) { hv_Rows = new HTuple(); } hv_Rows[hv_Polarity] = hv_Row; if (hv_Cols == null) { hv_Cols = new HTuple(); } hv_Cols[hv_Polarity] = hv_Column; break; } } } if ((int)(new HTuple(hv_Polarity.TupleEqual(2))) != 0) { HTuple end_val23 = hv_HalfDiagonal; HTuple step_val23 = 1; for (hv_Index = 0; hv_Index.Continue(end_val23, step_val23); hv_Index = hv_Index.TupleAdd(step_val23)) { using (HDevDisposeHelper dh = new HDevDisposeHelper()) { ho_RegionsLine3.Dispose(); HOperatorSet.GenRegionHline(out ho_RegionsLine3, (new HTuple(45)).TupleRad() , 1 * ((0 * hv_HalfDiagonal) + hv_Index)); } ho_RegionIntersection.Dispose(); HOperatorSet.Intersection(ho_RegionsLine3, ho_Rectangle, out ho_RegionIntersection ); hv_RegionIntersectionArea.Dispose(); hv_Row.Dispose(); hv_Column.Dispose(); HOperatorSet.AreaCenter(ho_RegionIntersection, out hv_RegionIntersectionArea, out hv_Row, out hv_Column); if ((int)(new HTuple(hv_RegionIntersectionArea.TupleGreater(0))) != 0) { if (hv_Rows == null) { hv_Rows = new HTuple(); } hv_Rows[hv_Polarity] = hv_Row; if (hv_Cols == null) { hv_Cols = new HTuple(); } hv_Cols[hv_Polarity] = hv_Column; break; } } } if ((int)(new HTuple(hv_Polarity.TupleEqual(3))) != 0) { HTuple end_val35 = hv_HalfDiagonal; HTuple step_val35 = 1; for (hv_Index = 0; hv_Index.Continue(end_val35, step_val35); hv_Index = hv_Index.TupleAdd(step_val35)) { using (HDevDisposeHelper dh = new HDevDisposeHelper()) { ho_RegionsLine3.Dispose(); HOperatorSet.GenRegionHline(out ho_RegionsLine3, (new HTuple(-45)).TupleRad() , -1 * ((1 * hv_HalfDiagonal) - hv_Index)); } ho_RegionIntersection.Dispose(); HOperatorSet.Intersection(ho_RegionsLine3, ho_Rectangle, out ho_RegionIntersection ); hv_RegionIntersectionArea.Dispose(); hv_Row.Dispose(); hv_Column.Dispose(); HOperatorSet.AreaCenter(ho_RegionIntersection, out hv_RegionIntersectionArea, out hv_Row, out hv_Column); if ((int)(new HTuple(hv_RegionIntersectionArea.TupleGreater(0))) != 0) { if (hv_Rows == null) { hv_Rows = new HTuple(); } hv_Rows[hv_Polarity] = hv_Row; if (hv_Cols == null) { hv_Cols = new HTuple(); } hv_Cols[hv_Polarity] = hv_Column; break; } } } if ((int)(new HTuple(hv_Polarity.TupleEqual(4))) != 0) { HTuple end_val47 = hv_HalfDiagonal; HTuple step_val47 = 1; for (hv_Index = 0; hv_Index.Continue(end_val47, step_val47); hv_Index = hv_Index.TupleAdd(step_val47)) { using (HDevDisposeHelper dh = new HDevDisposeHelper()) { ho_RegionsLine3.Dispose(); HOperatorSet.GenRegionHline(out ho_RegionsLine3, (new HTuple(45)).TupleRad() , 1 * ((2 * hv_HalfDiagonal) - hv_Index)); } ho_RegionIntersection.Dispose(); HOperatorSet.Intersection(ho_RegionsLine3, ho_Rectangle, out ho_RegionIntersection ); hv_RegionIntersectionArea.Dispose(); hv_Row.Dispose(); hv_Column.Dispose(); HOperatorSet.AreaCenter(ho_RegionIntersection, out hv_RegionIntersectionArea, out hv_Row, out hv_Column); if ((int)(new HTuple(hv_RegionIntersectionArea.TupleGreater(0))) != 0) { if (hv_Rows == null) { hv_Rows = new HTuple(); } hv_Rows[hv_Polarity] = hv_Row; if (hv_Cols == null) { hv_Cols = new HTuple(); } hv_Cols[hv_Polarity] = hv_Column; break; } } } //get_region_points (RegionIntersection, Rows, Columns) //disp_message (WindowHandle, 'Intesection Row:'+ Row +'Col'+ Column, 'window', Row, Column, 'black', 'true') } //dev_set_colored (12) //for Index := 1 to 4 by 1 //disp_circle (WindowHandle, Rows[Index], Cols[Index], 64) //endfor ho_RegionsLine3.Dispose(); ho_RegionIntersection.Dispose(); hv_Width.Dispose(); hv_Height.Dispose(); hv_LengthDiagonal.Dispose(); hv_HalfDiagonal.Dispose(); hv_Polarity.Dispose(); hv_Index.Dispose(); hv_RegionIntersectionArea.Dispose(); hv_Row.Dispose(); hv_Column.Dispose(); return; }
// Procedures public void EnhanceEdgeArea3(HObject ho_InputImage, out HObject ho_EnhancedImage, HTuple hv_EmpMaskWidth, HTuple hv_EmpMaskHeight, HTuple hv_EmpMaskFactor, HTuple hv_MeanMaskWidth, HTuple hv_MeanMaskHeight, HTuple hv_IterationCount, HTuple hv_ScaleMult, HTuple hv_ScaleAdd) { // Stack for temporary objects HObject[] OTemp = new HObject[20]; // Local iconic variables HObject ho_ImageScaled = null, ho_ImageOpening = null; // Local control variables HTuple hv_Index = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_EnhancedImage); HOperatorSet.GenEmptyObj(out ho_ImageScaled); HOperatorSet.GenEmptyObj(out ho_ImageOpening); ho_EnhancedImage.Dispose(); HOperatorSet.MeanImage(ho_InputImage, out ho_EnhancedImage, 1, 1); HTuple end_val4 = hv_IterationCount; HTuple step_val4 = 1; for (hv_Index = 1; hv_Index.Continue(end_val4, step_val4); hv_Index = hv_Index.TupleAdd(step_val4)) { HOperatorSet.MeanImage(ho_EnhancedImage, out OTemp[0], hv_MeanMaskWidth, hv_MeanMaskHeight); ho_EnhancedImage.Dispose(); ho_EnhancedImage = OTemp[0]; ho_ImageScaled.Dispose(); HOperatorSet.ScaleImage(ho_EnhancedImage, out ho_ImageScaled, hv_ScaleMult, hv_ScaleAdd); ho_EnhancedImage.Dispose(); HOperatorSet.Emphasize(ho_ImageScaled, out ho_EnhancedImage, hv_EmpMaskWidth, hv_EmpMaskHeight, hv_EmpMaskFactor); } if ((int)(new HTuple(hv_MeanMaskWidth.TupleGreater(hv_MeanMaskHeight))) != 0) { ho_ImageOpening.Dispose(); HOperatorSet.GrayOpeningRect(ho_EnhancedImage, out ho_ImageOpening, 1, hv_MeanMaskWidth * 2); ho_EnhancedImage.Dispose(); HOperatorSet.GrayClosingRect(ho_ImageOpening, out ho_EnhancedImage, 1, hv_MeanMaskWidth * 2); } else { ho_ImageOpening.Dispose(); HOperatorSet.GrayOpeningRect(ho_EnhancedImage, out ho_ImageOpening, hv_MeanMaskHeight * 2, 1); ho_EnhancedImage.Dispose(); HOperatorSet.GrayClosingRect(ho_ImageOpening, out ho_EnhancedImage, hv_MeanMaskHeight * 2, 1); } //mean_image (ImageEmp, EnhancedImage, MeanMaskWidth, MeanMaskHeight) ho_ImageScaled.Dispose(); ho_ImageOpening.Dispose(); return; }
public MeasureResult Action() { #region 輸出結果 CircleResult mResult = null; #endregion // Local iconic variables HObject ho_R3_Circle = null; HObject ho_R3_ROI_Image = null, ho_R3_Region = null, ho_R3_ImageReduced = null; HObject ho_R3_Edges = null, ho_R3_ContoursSplit = null, ho_R3_SingleSegment = null; HObject ho_R3_ContEllipse = null; // Local control variables HTuple hv_msgOffsetY, hv_msgOffsetX; HTuple hv_STD_Row; HTuple hv_STD_Col, hv_Img_Row, hv_Img_Col, hv_Img_Rotate_Angle; HTuple hv_OffsetRow, hv_OffsetCol, hv_R3_R; HTuple hv_STD_R3_Row = new HTuple(), hv_STD_R3_Col = new HTuple(); HTuple hv_STD_R3_V_Row = new HTuple(), hv_STD_R3_V_Col = new HTuple(); HTuple hv_R3_X = new HTuple(), hv_R3_Y = new HTuple(), hv_R3_Pos_Row = new HTuple(); HTuple hv_R3_Pos_Col = new HTuple(), hv_alpha = new HTuple(); HTuple hv_R3_low = new HTuple(), hv_R3_high = new HTuple(); HTuple hv_R3_NumSegments = new HTuple(), hv_NumCircles = new HTuple(); HTuple hv_Num_Circle_Point = new HTuple(), hv_R3 = new HTuple(); HTuple hv_i = new HTuple(), hv_Attrib = new HTuple(), hv_R3_Row = new HTuple(); HTuple hv_R3_Column = new HTuple(), hv_R3_Radius = new HTuple(); HTuple hv_R3_StartPhi = new HTuple(), hv_R3_EndPhi = new HTuple(); HTuple hv_R3_PointOrder = new HTuple(), hv_R3_MinDist = new HTuple(); HTuple hv_R3_MaxDist = new HTuple(), hv_R3_AvgDist = new HTuple(); HTuple hv_R3_SigmaDist = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_R3_Circle); HOperatorSet.GenEmptyObj(out ho_R3_ROI_Image); HOperatorSet.GenEmptyObj(out ho_R3_Region); HOperatorSet.GenEmptyObj(out ho_R3_ImageReduced); HOperatorSet.GenEmptyObj(out ho_R3_Edges); HOperatorSet.GenEmptyObj(out ho_R3_ContoursSplit); HOperatorSet.GenEmptyObj(out ho_R3_SingleSegment); HOperatorSet.GenEmptyObj(out ho_R3_ContEllipse); //Measure: SDMS_R3 //Author: John Hsieh //Date: 2012 //ho_Image.Dispose(); //HOperatorSet.ReadImage(out ho_Image, "Images/STD.bmp"); //dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle); //// dev_update_off(...); only in hdevelop HOperatorSet.SetSystem("border_shape_models", "false"); //****Message Args hv_msgOffsetY = 100; hv_msgOffsetX = 100; //****Model All //HOperatorSet.ReadShapeModel("D:/Projects/Halcon/SDMS/SDMS_Measure/Model/MatchingAll.shm", // out hv_AllModelId); //ho_AllModelContours.Dispose(); //HOperatorSet.GetShapeModelContours(out ho_AllModelContours, hv_AllModelId, 1); //HOperatorSet.FindShapeModel(ho_Image, hv_AllModelId, (new HTuple(0)).TupleRad() // , (new HTuple(360)).TupleRad(), 0.5, 1, 0.5, "least_squares", 6, 0.75, out hv_AllModelRow, // out hv_AllModelColumn, out hv_AllModelAngle, out hv_AllModelScore); //****Model Args //STD 中心點 hv_STD_Row = 772; hv_STD_Col = 1003; //目前圖形 中心點 hv_Img_Row = hv_AllModelRow.Clone(); hv_Img_Col = hv_AllModelColumn.Clone(); //目前圖形 Rotate Angle hv_Img_Rotate_Angle = hv_AllModelAngle.Clone(); //目前圖形偏移量 hv_OffsetRow = hv_Img_Row - hv_STD_Row; hv_OffsetCol = hv_Img_Col - hv_STD_Col; //****Display if (HDevWindowStack.IsOpen()) { HOperatorSet.ClearWindow(HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } //*****R3 hv_R3_R = 29; //STD R3_ 位置 hv_STD_R3_Row = 252; hv_STD_R3_Col = 1381; //STD 向量 STD_R3_ hv_STD_R3_V_Row = hv_STD_R3_Row - hv_STD_Row; hv_STD_R3_V_Col = hv_STD_R3_Col - hv_STD_Col; //R3_X, R3_Y 分量 hv_R3_X = (hv_STD_R3_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_R3_V_Row * (hv_Img_Rotate_Angle.TupleSin() )); hv_R3_Y = (hv_STD_R3_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_R3_V_Col * (hv_Img_Rotate_Angle.TupleSin() )); //目前圖形 R3_ 位置 hv_R3_Pos_Row = (hv_STD_Row + hv_R3_Y) + hv_OffsetRow; hv_R3_Pos_Col = (hv_STD_Col + hv_R3_X) + hv_OffsetCol; //** 開始計算 ho_R3_Circle.Dispose(); HOperatorSet.GenCircle(out ho_R3_Circle, hv_R3_Pos_Row, hv_R3_Pos_Col, hv_R3_R); ho_R3_ROI_Image.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_R3_Circle, out ho_R3_ROI_Image); ho_R3_Region.Dispose(); HOperatorSet.FastThreshold(ho_R3_ROI_Image, out ho_R3_Region, 100, 255, 15); ho_R3_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_R3_ROI_Image, ho_R3_Region, out ho_R3_ImageReduced ); //stop () //sobel_fast 具有較寬的選擇範圍,搭配 alpha 參數 (alpha 越大, 容錯範圍大) hv_alpha = 0.9; hv_R3_low = 20; hv_R3_high = 60; ho_R3_Edges.Dispose(); HOperatorSet.EdgesSubPix(ho_R3_ImageReduced, out ho_R3_Edges, "sobel_fast", hv_alpha, hv_R3_low, hv_R3_high); //stop () //*所有的數值越小,表示容錯範圍大,反之亦然 ho_R3_ContoursSplit.Dispose(); HOperatorSet.SegmentContoursXld(ho_R3_Edges, out ho_R3_ContoursSplit, "lines_circles", 17, 4, 2); //Display the results //=========================================================== HOperatorSet.CountObj(ho_R3_ContoursSplit, out hv_R3_NumSegments); hv_NumCircles = 0; hv_Num_Circle_Point = 0; hv_R3 = 999; for (hv_i = 1; hv_i.Continue(hv_R3_NumSegments, 1); hv_i = hv_i.TupleAdd(1)) { ho_R3_SingleSegment.Dispose(); HOperatorSet.SelectObj(ho_R3_ContoursSplit, out ho_R3_SingleSegment, hv_i); HOperatorSet.GetContourGlobalAttribXld(ho_R3_SingleSegment, "cont_approx", out hv_Attrib); if ((int)(new HTuple(hv_Attrib.TupleEqual(1))) != 0) { HOperatorSet.FitCircleContourXld(ho_R3_SingleSegment, "atukey", -1, 2, hv_Num_Circle_Point, 5, 2, out hv_R3_Row, out hv_R3_Column, out hv_R3_Radius, out hv_R3_StartPhi, out hv_R3_EndPhi, out hv_R3_PointOrder); hv_NumCircles = hv_NumCircles + 1; if ((int)(new HTuple(hv_R3.TupleGreater(hv_R3_Radius))) != 0) { hv_R3 = hv_R3_Radius.Clone(); mResult = new CircleResult() { Row = new HTuple(hv_R3_Row), Col = new HTuple(hv_R3_Column), Radius = new HTuple(hv_R3_Radius), StartPhi = new HTuple(hv_R3_StartPhi), EndPhi = new HTuple(hv_R3_EndPhi), PointOrder = new HTuple(hv_R3_PointOrder), }; //HOperatorSet.SetTposition(hv_WindowHandle, (hv_R3_Pos_Row - hv_msgOffsetY) + 60, // (hv_R3_Pos_Col + hv_msgOffsetX) - 60); //HOperatorSet.WriteString(hv_WindowHandle, "R3"); //stop () } } } //*****R3 End ho_R3_Circle.Dispose(); ho_R3_ROI_Image.Dispose(); ho_R3_Region.Dispose(); ho_R3_ImageReduced.Dispose(); ho_R3_Edges.Dispose(); ho_R3_ContoursSplit.Dispose(); ho_R3_SingleSegment.Dispose(); ho_R3_ContEllipse.Dispose(); return mResult; }
public void do_inspect1(HObject ho_Image, HTuple hv_countPath, HTuple hv_startPath, HTuple hv_pausePath, HTuple hv_stopPath, out HTuple hv_outport_result) { // Local iconic variables HObject ho_rightRectangle = null, ho_leftRectangle = null; HObject ho_GrayImage = null, ho_rightImageReduced = null, ho_leftImageReduced = null; HObject ho_leftBlueRegions = null, ho_rightBlueRegions = null; HObject ho_leftRedRegions = null, ho_rightRedRegions = null; HObject ho_leftYellowRegions = null, ho_rightYellowRegions = null; HObject ho_leftGreenRegions = null, ho_rightGreenRegions = null; // Local control variables HTuple hv_TestModelID, hv_testRow, hv_testColumn; HTuple hv_testAngle, hv_testScore, hv_rightBlueArea = new HTuple(); HTuple hv_Row = new HTuple(), hv_Column = new HTuple(), hv_leftBlueArea = new HTuple(); HTuple hv_rightRedArea = new HTuple(), hv_leftRedArea = new HTuple(); HTuple hv_rightYellowArea = new HTuple(), hv_leftYellowArea = new HTuple(); HTuple hv_rightGreenArea = new HTuple(), hv_leftGreenArea = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_rightRectangle); HOperatorSet.GenEmptyObj(out ho_leftRectangle); HOperatorSet.GenEmptyObj(out ho_GrayImage); HOperatorSet.GenEmptyObj(out ho_rightImageReduced); HOperatorSet.GenEmptyObj(out ho_leftImageReduced); HOperatorSet.GenEmptyObj(out ho_leftBlueRegions); HOperatorSet.GenEmptyObj(out ho_rightBlueRegions); HOperatorSet.GenEmptyObj(out ho_leftRedRegions); HOperatorSet.GenEmptyObj(out ho_rightRedRegions); HOperatorSet.GenEmptyObj(out ho_leftYellowRegions); HOperatorSet.GenEmptyObj(out ho_rightYellowRegions); HOperatorSet.GenEmptyObj(out ho_leftGreenRegions); HOperatorSet.GenEmptyObj(out ho_rightGreenRegions); hv_outport_result = new HTuple(); HOperatorSet.ReadNccModel(hv_countPath, out hv_TestModelID); HOperatorSet.FindNccModel(ho_Image, hv_TestModelID, -0.39, 0.78, 0.5, 2, 0.5, "true", 3, out hv_testRow, out hv_testColumn, out hv_testAngle, out hv_testScore); if ((int)((new HTuple((new HTuple(hv_testScore.TupleLength())).TupleEqual(2))).TupleAnd( new HTuple(((hv_testScore.TupleSelect(1))).TupleGreater(0.75)))) != 0) { if ((int)(new HTuple(((hv_testColumn.TupleSelect(0))).TupleGreater(hv_testColumn.TupleSelect( 1)))) != 0) { ho_rightRectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_rightRectangle, (hv_testRow.TupleSelect( 0)) - 12, (hv_testColumn.TupleSelect(0)) - (((hv_testColumn.TupleSelect(0)) - (hv_testColumn.TupleSelect( 1))) - 120), (hv_testRow.TupleSelect(0)) + 38, (hv_testColumn.TupleSelect( 0)) - 30); ho_leftRectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_leftRectangle, (hv_testRow.TupleSelect( 1)) - 12, (hv_testColumn.TupleSelect(1)) - (((hv_testColumn.TupleSelect(0)) - (hv_testColumn.TupleSelect( 1))) - 120), (hv_testRow.TupleSelect(1)) + 38, (hv_testColumn.TupleSelect( 1)) - 30); } else { ho_rightRectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_rightRectangle, (hv_testRow.TupleSelect( 1)) - 12, (hv_testColumn.TupleSelect(1)) - (((hv_testColumn.TupleSelect(1)) - (hv_testColumn.TupleSelect( 0))) - 120), (hv_testRow.TupleSelect(1)) + 38, (hv_testColumn.TupleSelect( 1)) - 30); ho_leftRectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_leftRectangle, (hv_testRow.TupleSelect( 0)) - 12, (hv_testColumn.TupleSelect(0)) - (((hv_testColumn.TupleSelect(1)) - (hv_testColumn.TupleSelect( 0))) - 120), (hv_testRow.TupleSelect(0)) + 38, (hv_testColumn.TupleSelect( 0)) - 30); } ho_GrayImage.Dispose(); HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage); ho_rightImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_GrayImage, ho_rightRectangle, out ho_rightImageReduced ); ho_leftImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_GrayImage, ho_leftRectangle, out ho_leftImageReduced ); //À¶É«start ho_leftBlueRegions.Dispose(); HOperatorSet.Threshold(ho_leftImageReduced, out ho_leftBlueRegions, 29, 29); ho_rightBlueRegions.Dispose(); HOperatorSet.Threshold(ho_rightImageReduced, out ho_rightBlueRegions, 29, 29); HOperatorSet.AreaCenter(ho_rightBlueRegions, out hv_rightBlueArea, out hv_Row, out hv_Column); HOperatorSet.AreaCenter(ho_leftBlueRegions, out hv_leftBlueArea, out hv_Row, out hv_Column); //ºìÉ«fail ho_leftRedRegions.Dispose(); HOperatorSet.Threshold(ho_leftImageReduced, out ho_leftRedRegions, 76, 76); ho_rightRedRegions.Dispose(); HOperatorSet.Threshold(ho_rightImageReduced, out ho_rightRedRegions, 76, 76); HOperatorSet.AreaCenter(ho_rightRedRegions, out hv_rightRedArea, out hv_Row, out hv_Column); HOperatorSet.AreaCenter(ho_leftRedRegions, out hv_leftRedArea, out hv_Row, out hv_Column); //»ÆÉ«test ho_leftYellowRegions.Dispose(); HOperatorSet.Threshold(ho_leftImageReduced, out ho_leftYellowRegions, 226, 226); ho_rightYellowRegions.Dispose(); HOperatorSet.Threshold(ho_rightImageReduced, out ho_rightYellowRegions, 226, 226); HOperatorSet.AreaCenter(ho_rightYellowRegions, out hv_rightYellowArea, out hv_Row, out hv_Column); HOperatorSet.AreaCenter(ho_leftYellowRegions, out hv_leftYellowArea, out hv_Row, out hv_Column); //ÂÌÉ«pass ho_leftGreenRegions.Dispose(); HOperatorSet.Threshold(ho_leftImageReduced, out ho_leftGreenRegions, 150, 150); ho_rightGreenRegions.Dispose(); HOperatorSet.Threshold(ho_rightImageReduced, out ho_rightGreenRegions, 150, 150); HOperatorSet.AreaCenter(ho_rightGreenRegions, out hv_rightGreenArea, out hv_Row, out hv_Column); HOperatorSet.AreaCenter(ho_leftGreenRegions, out hv_leftGreenArea, out hv_Row, out hv_Column); hv_outport_result[0] = 5; //left if ((int)((new HTuple((new HTuple(hv_leftGreenArea.TupleLength())).TupleEqual( 1))).TupleAnd(new HTuple(hv_leftGreenArea.TupleGreater(400)))) != 0) { hv_outport_result[1] = 2; } else if ((int)((new HTuple((new HTuple(hv_leftBlueArea.TupleLength() )).TupleEqual(1))).TupleAnd(new HTuple(hv_leftBlueArea.TupleGreater(400)))) != 0) { hv_outport_result[1] = 0; } else if ((int)((new HTuple((new HTuple(hv_leftYellowArea.TupleLength() )).TupleEqual(1))).TupleAnd(new HTuple(hv_leftYellowArea.TupleGreater(400)))) != 0) { hv_outport_result[1] = 1; } else if ((int)((new HTuple((new HTuple(hv_leftRedArea.TupleLength() )).TupleEqual(1))).TupleAnd(new HTuple(hv_leftRedArea.TupleGreater(400)))) != 0) { hv_outport_result[1] = 3; } else { hv_outport_result[1] = 4; } //right if ((int)((new HTuple((new HTuple(hv_rightGreenArea.TupleLength())).TupleEqual( 1))).TupleAnd(new HTuple(hv_rightGreenArea.TupleGreater(400)))) != 0) { hv_outport_result[2] = 2; } else if ((int)((new HTuple((new HTuple(hv_rightBlueArea.TupleLength() )).TupleEqual(1))).TupleAnd(new HTuple(hv_rightBlueArea.TupleGreater(400)))) != 0) { hv_outport_result[2] = 0; } else if ((int)((new HTuple((new HTuple(hv_rightYellowArea.TupleLength() )).TupleEqual(1))).TupleAnd(new HTuple(hv_rightYellowArea.TupleGreater( 400)))) != 0) { hv_outport_result[2] = 1; } else if ((int)((new HTuple((new HTuple(hv_rightRedArea.TupleLength() )).TupleEqual(1))).TupleAnd(new HTuple(hv_rightRedArea.TupleGreater(400)))) != 0) { hv_outport_result[2] = 3; } else { hv_outport_result[2] = 4; } } else { hv_outport_result[0] = 3; } HOperatorSet.ClearNccModel(hv_TestModelID); ho_rightRectangle.Dispose(); ho_leftRectangle.Dispose(); ho_GrayImage.Dispose(); ho_rightImageReduced.Dispose(); ho_leftImageReduced.Dispose(); ho_leftBlueRegions.Dispose(); ho_rightBlueRegions.Dispose(); ho_leftRedRegions.Dispose(); ho_rightRedRegions.Dispose(); ho_leftYellowRegions.Dispose(); ho_rightYellowRegions.Dispose(); ho_leftGreenRegions.Dispose(); ho_rightGreenRegions.Dispose(); return; }
// Main procedure private void action() { // Stack for temporary objects HObject[] OTemp = new HObject[20]; long SP_O = 0; // Local iconic variables HObject ho_Image, ho_Image1, ho_Image2, ho_Image3; HObject ho_ImageResult1, ho_ImageResult2, ho_ImageResult3; HObject ho_ClassRegionsNotRejected, ho_ObjectSelected, ho_RegionOpening2; HObject ho_ConnectedRegions, ho_SelectedRegions, ho_RegionUnion; HObject ho_RegionOpening, ho_RegionTrans, ho_ImageReduced; HObject ho_ObjectSelected1, ho_RegionOpening1, ho_RegionFillUp; HObject ho_ConnectedRegions1, ho_EmptyObject, ho_ObjectSelected2 = null; HObject ho_RegionDilation = null, ho_RegionUnion1, ho_ConnectedRegions2; HObject ho_SelectedRegions1, ho_Rectangle; // Local control variables HTuple hv_WindowHandle = new HTuple(), hv_MLPHandle; HTuple hv_MLPHandle1, hv_Area2, hv_CircleRow, hv_CircleCol; HTuple hv_Number, hv_R, hv_Index1, hv_Area3 = new HTuple(); HTuple hv_Row2 = new HTuple(), hv_Column2 = new HTuple(), hv_Row1; HTuple hv_Column1, hv_Phi, hv_Length1, hv_Length2, hv_Area; HTuple hv_Row, hv_Column; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_Image1); HOperatorSet.GenEmptyObj(out ho_Image2); HOperatorSet.GenEmptyObj(out ho_Image3); HOperatorSet.GenEmptyObj(out ho_ImageResult1); HOperatorSet.GenEmptyObj(out ho_ImageResult2); HOperatorSet.GenEmptyObj(out ho_ImageResult3); HOperatorSet.GenEmptyObj(out ho_ClassRegionsNotRejected); HOperatorSet.GenEmptyObj(out ho_ObjectSelected); HOperatorSet.GenEmptyObj(out ho_RegionOpening2); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_RegionUnion); HOperatorSet.GenEmptyObj(out ho_RegionOpening); HOperatorSet.GenEmptyObj(out ho_RegionTrans); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_ObjectSelected1); HOperatorSet.GenEmptyObj(out ho_RegionOpening1); HOperatorSet.GenEmptyObj(out ho_RegionFillUp); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1); HOperatorSet.GenEmptyObj(out ho_EmptyObject); HOperatorSet.GenEmptyObj(out ho_ObjectSelected2); HOperatorSet.GenEmptyObj(out ho_RegionDilation); HOperatorSet.GenEmptyObj(out ho_RegionUnion1); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions2); HOperatorSet.GenEmptyObj(out ho_SelectedRegions1); HOperatorSet.GenEmptyObj(out ho_Rectangle); try { ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, new HTuple(new HTuple("C:/Code/Halcon/¼ì²âÓÐÎÞ/ͼƬ1/") + 13) + ".bmp"); //dev_close_window(...); dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle); HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle); HOperatorSet.ReadClassMlp("mlp_1.gmc", out hv_MLPHandle); HOperatorSet.ReadClassMlp("mlp_3.gmc", out hv_MLPHandle1); HOperatorSet.SetTposition(hv_ExpDefaultWinHandle, 10, 10); set_display_font(hv_ExpDefaultWinHandle, 50, "mono", "true", "false"); ho_Image1.Dispose(); ho_Image2.Dispose(); ho_Image3.Dispose(); HOperatorSet.Decompose3(ho_Image, out ho_Image1, out ho_Image2, out ho_Image3 ); ho_ImageResult1.Dispose(); ho_ImageResult2.Dispose(); ho_ImageResult3.Dispose(); HOperatorSet.TransFromRgb(ho_Image1, ho_Image2, ho_Image3, out ho_ImageResult1, out ho_ImageResult2, out ho_ImageResult3, "hsv"); HOperatorSet.DispObj(ho_Image3, hv_ExpDefaultWinHandle); HOperatorSet.SetColored(hv_ExpDefaultWinHandle, 12); ho_ClassRegionsNotRejected.Dispose(); HOperatorSet.ClassifyImageClassMlp(ho_Image, out ho_ClassRegionsNotRejected, hv_MLPHandle, 0.3); ho_ObjectSelected.Dispose(); HOperatorSet.SelectObj(ho_ClassRegionsNotRejected, out ho_ObjectSelected, 1); ho_RegionOpening2.Dispose(); HOperatorSet.OpeningCircle(ho_ObjectSelected, out ho_RegionOpening2, 5); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_RegionOpening2, out ho_ConnectedRegions); //area_center (ConnectedRegions, Area1, Row3, Column3) ho_SelectedRegions.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area", "and", 6000.2, 309205); ho_RegionUnion.Dispose(); HOperatorSet.Union1(ho_SelectedRegions, out ho_RegionUnion); ho_RegionOpening.Dispose(); HOperatorSet.OpeningCircle(ho_RegionUnion, out ho_RegionOpening, 3.5); ho_RegionTrans.Dispose(); HOperatorSet.ShapeTrans(ho_RegionOpening, out ho_RegionTrans, "circle"); HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "fill"); HOperatorSet.ClearWindow(hv_ExpDefaultWinHandle); HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle); HOperatorSet.DispObj(ho_RegionTrans, hv_ExpDefaultWinHandle); HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "fill"); ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_RegionTrans, out ho_ImageReduced); HOperatorSet.AreaCenter(ho_RegionTrans, out hv_Area2, out hv_CircleRow, out hv_CircleCol); ho_ClassRegionsNotRejected.Dispose(); HOperatorSet.ClassifyImageClassMlp(ho_ImageReduced, out ho_ClassRegionsNotRejected, hv_MLPHandle1, 0.3); HOperatorSet.SetColored(hv_ExpDefaultWinHandle, 12); HOperatorSet.DispObj(ho_ClassRegionsNotRejected, hv_ExpDefaultWinHandle); ho_ObjectSelected1.Dispose(); HOperatorSet.SelectObj(ho_ClassRegionsNotRejected, out ho_ObjectSelected1, 4); ho_RegionOpening1.Dispose(); HOperatorSet.OpeningCircle(ho_ObjectSelected1, out ho_RegionOpening1, 3.5); ho_RegionFillUp.Dispose(); HOperatorSet.FillUp(ho_RegionOpening1, out ho_RegionFillUp); //closing_circle (RegionFillUp, RegionClosing, 10) ho_ConnectedRegions1.Dispose(); HOperatorSet.Connection(ho_RegionFillUp, out ho_ConnectedRegions1); HOperatorSet.CountObj(ho_ConnectedRegions1, out hv_Number); //ÕÒ³ý¿¿½üÖÐÐĵÄÔ² ho_EmptyObject.Dispose(); HOperatorSet.GenEmptyObj(out ho_EmptyObject); hv_R = 200; for (hv_Index1 = 1; hv_Index1.Continue(hv_Number, 1); hv_Index1 = hv_Index1.TupleAdd(1)) { ho_ObjectSelected2.Dispose(); HOperatorSet.SelectObj(ho_ConnectedRegions1, out ho_ObjectSelected2, hv_Index1); HOperatorSet.AreaCenter(ho_ObjectSelected2, out hv_Area3, out hv_Row2, out hv_Column2); if ((int)((new HTuple((new HTuple((new HTuple(hv_Row2.TupleGreater(hv_CircleRow - hv_R))).TupleAnd( new HTuple(hv_Row2.TupleLess(hv_CircleRow + hv_R))))).TupleAnd(new HTuple(hv_Column2.TupleGreater( hv_CircleCol - hv_R))))).TupleAnd(new HTuple(hv_Column2.TupleLess(hv_CircleCol + hv_R)))) != 0) { ho_RegionDilation.Dispose(); HOperatorSet.DilationCircle(ho_ObjectSelected2, out ho_RegionDilation, 10); OTemp[SP_O] = ho_EmptyObject.CopyObj(1, -1); SP_O++; ho_EmptyObject.Dispose(); HOperatorSet.ConcatObj(ho_RegionDilation, OTemp[SP_O - 1], out ho_EmptyObject ); OTemp[SP_O - 1].Dispose(); SP_O = 0; } } ho_RegionUnion1.Dispose(); HOperatorSet.Union1(ho_EmptyObject, out ho_RegionUnion1); ho_ConnectedRegions2.Dispose(); HOperatorSet.Connection(ho_RegionUnion1, out ho_ConnectedRegions2); ho_SelectedRegions1.Dispose(); HOperatorSet.SelectShapeStd(ho_ConnectedRegions2, out ho_SelectedRegions1, "max_area", 70); HOperatorSet.SmallestRectangle2(ho_SelectedRegions1, 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); HOperatorSet.AreaCenter(ho_RegionFillUp, out hv_Area, out hv_Row, out hv_Column); HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "margin"); HOperatorSet.ClearWindow(hv_ExpDefaultWinHandle); HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle); if ((int)((new HTuple((new HTuple((new HTuple(hv_Length1.TupleLess(200))).TupleAnd( new HTuple(hv_Length2.TupleLess(200))))).TupleAnd(new HTuple(hv_Length1.TupleGreater( 50))))).TupleAnd(new HTuple(hv_Length2.TupleGreater(50)))) != 0) { HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green"); HOperatorSet.DispObj(ho_Rectangle, hv_ExpDefaultWinHandle); HOperatorSet.WriteString(hv_ExpDefaultWinHandle, "OK"); //set_framegrabber_param (AcqHandle, 'UserOutputValue', 0) } else { HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "red"); HOperatorSet.WriteString(hv_ExpDefaultWinHandle, "NG"); //set_framegrabber_param (AcqHandle, 'UserOutputValue', 1) } } catch (HalconException HDevExpDefaultException) { ho_Image.Dispose(); ho_Image1.Dispose(); ho_Image2.Dispose(); ho_Image3.Dispose(); ho_ImageResult1.Dispose(); ho_ImageResult2.Dispose(); ho_ImageResult3.Dispose(); ho_ClassRegionsNotRejected.Dispose(); ho_ObjectSelected.Dispose(); ho_RegionOpening2.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_RegionUnion.Dispose(); ho_RegionOpening.Dispose(); ho_RegionTrans.Dispose(); ho_ImageReduced.Dispose(); ho_ObjectSelected1.Dispose(); ho_RegionOpening1.Dispose(); ho_RegionFillUp.Dispose(); ho_ConnectedRegions1.Dispose(); ho_EmptyObject.Dispose(); ho_ObjectSelected2.Dispose(); ho_RegionDilation.Dispose(); ho_RegionUnion1.Dispose(); ho_ConnectedRegions2.Dispose(); ho_SelectedRegions1.Dispose(); ho_Rectangle.Dispose(); throw HDevExpDefaultException; } ho_Image.Dispose(); ho_Image1.Dispose(); ho_Image2.Dispose(); ho_Image3.Dispose(); ho_ImageResult1.Dispose(); ho_ImageResult2.Dispose(); ho_ImageResult3.Dispose(); ho_ClassRegionsNotRejected.Dispose(); ho_ObjectSelected.Dispose(); ho_RegionOpening2.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_RegionUnion.Dispose(); ho_RegionOpening.Dispose(); ho_RegionTrans.Dispose(); ho_ImageReduced.Dispose(); ho_ObjectSelected1.Dispose(); ho_RegionOpening1.Dispose(); ho_RegionFillUp.Dispose(); ho_ConnectedRegions1.Dispose(); ho_EmptyObject.Dispose(); ho_ObjectSelected2.Dispose(); ho_RegionDilation.Dispose(); ho_RegionUnion1.Dispose(); ho_ConnectedRegions2.Dispose(); ho_SelectedRegions1.Dispose(); ho_Rectangle.Dispose(); }
public MeasureResult Action() { #region 輸出結果 LineResult mResult = null; #endregion // Local iconic variables HObject ho_A2_Region = null; HObject ho_A2_Reduced = null, ho_A2Region = null, ho_A2_RegionBorder = null; HObject ho_A2_RegionDilation = null, ho_A2_Edges = null, ho_A2_Rectangles = null; HObject ho_A2Cross = null, ho_A3_Region = null, ho_A3_Reduced = null; HObject ho_A3Region = null, ho_A3_RegionBorder = null, ho_A3_RegionDilation = null; HObject ho_A3_Edges = null, ho_A3_Rectangles = null, ho_A3Object = null; HObject ho_A3Cross = null; // Local control variables HTuple hv_msgOffsetY, hv_msgOffsetX; HTuple hv_STD_Row; HTuple hv_STD_Col, hv_Img_Row, hv_Img_Col, hv_Img_Rotate_Angle; HTuple hv_OffsetRow, hv_OffsetCol, hv_A2_Center_X, hv_A2_Center_Y; HTuple hv_STD_A2_1_1_Row, hv_STD_A2_1_1_Col, hv_STD_A2_1_1_V_Row; HTuple hv_STD_A2_1_1_V_Col, hv_A2_1_1_X, hv_A2_1_1_Y, hv_A2_1_1_Pos_Row; HTuple hv_A2_1_1_Pos_Col, hv_A2_ROI_W = new HTuple(); HTuple hv_A2_ROI_H = new HTuple(), hv_Rec_W = new HTuple(); HTuple hv_Rec_H = new HTuple(), hv_A2_Alpha = new HTuple(); HTuple hv_a2low = new HTuple(), hv_a2high = new HTuple(), hv_a2Limit = new HTuple(); HTuple hv_A2_RecNumber = new HTuple(), hv_A2Row = new HTuple(); HTuple hv_A2Column = new HTuple(), hv_A2Phi = new HTuple(); HTuple hv_A2Length1 = new HTuple(), hv_A2Length2 = new HTuple(); HTuple hv_A2PointOrder = new HTuple(), hv_A2Number = new HTuple(); HTuple hv_A3_Center_X, hv_A3_Center_Y, hv_STD_A3_Row, hv_STD_A3_Col; HTuple hv_STD_A3_V_Row, hv_STD_A3_V_Col, hv_A3_X, hv_A3_Y; HTuple hv_A3_Pos_Row, hv_A3_Pos_Col, hv_A3_ROI_W = new HTuple(); HTuple hv_A3_ROI_H = new HTuple(), hv_A3_Alpha = new HTuple(); HTuple hv_low = new HTuple(), hv_high = new HTuple(), hv_a3Limit = new HTuple(); HTuple hv_A3RecNumber = new HTuple(), hv_A3Row = new HTuple(); HTuple hv_A3Column = new HTuple(), hv_A3Phi = new HTuple(); HTuple hv_A3Length1 = new HTuple(), hv_A3Length2 = new HTuple(); HTuple hv_A3PointOrder = new HTuple(), hv_A3Number = new HTuple(); HTuple hv_X1 = new HTuple(), hv_X2 = new HTuple(); HTuple hv_Y1 = new HTuple(), hv_Y2 = new HTuple(), hv_STD_Mark_A3_C_Row = new HTuple(); HTuple hv_STD_Mark_A3_C_Col = new HTuple(), hv_STD_Mark_A3_Start_Row = new HTuple(); HTuple hv_STD_Mark_A3_Start_Col = new HTuple(), hv_STD_Mark_A3_End_Row = new HTuple(); HTuple hv_STD_Mark_A3_End_Col = new HTuple(), hv_STD_Mark_A3_C_V_Row = new HTuple(); HTuple hv_STD_Mark_A3_C_V_Col = new HTuple(), hv_Mark_A3_C_X = new HTuple(); HTuple hv_Mark_A3_C_Y = new HTuple(), hv_Mark_A3_C_Pos_Row = new HTuple(); HTuple hv_Mark_A3_C_Pos_Col = new HTuple(), hv_STD_Mark_A3_Start_V_Row = new HTuple(); HTuple hv_STD_Mark_A3_Start_V_Col = new HTuple(), hv_Mark_A3_Start_X = new HTuple(); HTuple hv_Mark_A3_Start_Y = new HTuple(), hv_Mark_A3_Start_Pos_Row = new HTuple(); HTuple hv_Mark_A3_Start_Pos_Col = new HTuple(), hv_STD_Mark_A3_End_V_Row = new HTuple(); HTuple hv_STD_Mark_A3_End_V_Col = new HTuple(), hv_Mark_A3_End_X = new HTuple(); HTuple hv_Mark_A3_End_Y = new HTuple(), hv_Mark_A3_End_Pos_Row = new HTuple(); HTuple hv_Mark_A3_End_Pos_Col = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_A2_Region); HOperatorSet.GenEmptyObj(out ho_A2_Reduced); HOperatorSet.GenEmptyObj(out ho_A2Region); HOperatorSet.GenEmptyObj(out ho_A2_RegionBorder); HOperatorSet.GenEmptyObj(out ho_A2_RegionDilation); HOperatorSet.GenEmptyObj(out ho_A2_Edges); HOperatorSet.GenEmptyObj(out ho_A2_Rectangles); HOperatorSet.GenEmptyObj(out ho_A2Cross); HOperatorSet.GenEmptyObj(out ho_A3_Region); HOperatorSet.GenEmptyObj(out ho_A3_Reduced); HOperatorSet.GenEmptyObj(out ho_A3Region); HOperatorSet.GenEmptyObj(out ho_A3_RegionBorder); HOperatorSet.GenEmptyObj(out ho_A3_RegionDilation); HOperatorSet.GenEmptyObj(out ho_A3_Edges); HOperatorSet.GenEmptyObj(out ho_A3_Rectangles); HOperatorSet.GenEmptyObj(out ho_A3Object); HOperatorSet.GenEmptyObj(out ho_A3Cross); //Measure: SDMS_A3 //Author: John Hsieh //Date: 2012 //Note: A3 related to A2 //ho_Image.Dispose(); //HOperatorSet.ReadImage(out ho_Image, "Images/STD.bmp"); //dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle); // dev_update_off(...); only in hdevelop HOperatorSet.SetSystem("border_shape_models", "false"); //****Message Args hv_msgOffsetY = 100; hv_msgOffsetX = 100; //****Model Args //STD 中心點 hv_STD_Row = 772; hv_STD_Col = 1003; //目前圖形 中心點 hv_Img_Row = hv_AllModelRow.Clone(); hv_Img_Col = hv_AllModelColumn.Clone(); //目前圖形 Rotate Angle hv_Img_Rotate_Angle = hv_AllModelAngle.Clone(); //目前圖形偏移量 hv_OffsetRow = hv_Img_Row - hv_STD_Row; hv_OffsetCol = hv_Img_Col - hv_STD_Col; #region 找左邊數來第一個金手指的中心點 Finger /* * //*****A3 * //Step 1 */ hv_A2_Center_X = 0; hv_A2_Center_Y = 0; //****A2_1_1 //STD A2_1_1_ 位置 hv_STD_A2_1_1_Row = 410; hv_STD_A2_1_1_Col = 780; //STD 向量 STD_A2_1_1_ hv_STD_A2_1_1_V_Row = hv_STD_A2_1_1_Row - hv_STD_Row; hv_STD_A2_1_1_V_Col = hv_STD_A2_1_1_Col - hv_STD_Col; //A2_1_1_X, A2_1_1_Y 分量 hv_A2_1_1_X = (hv_STD_A2_1_1_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_A2_1_1_V_Row * (hv_Img_Rotate_Angle.TupleSin() )); hv_A2_1_1_Y = (hv_STD_A2_1_1_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_A2_1_1_V_Col * (hv_Img_Rotate_Angle.TupleSin() )); //目前圖形 A2_1_1_ 位置 hv_A2_1_1_Pos_Row = (hv_STD_Row + hv_A2_1_1_Y) + hv_OffsetRow; hv_A2_1_1_Pos_Col = (hv_STD_Col + hv_A2_1_1_X) + hv_OffsetCol; //A2_ROI hv_A2_ROI_W = 40; hv_A2_ROI_H = 120; ho_A2_Region.Dispose(); HOperatorSet.GenRectangle2(out ho_A2_Region, hv_A2_1_1_Pos_Row, hv_A2_1_1_Pos_Col, hv_Img_Rotate_Angle, hv_A2_ROI_W, hv_A2_ROI_H); //stop () ho_A2_Reduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_A2_Region, out ho_A2_Reduced); //fit_Rectangle //stop () // ho_A2Region.Dispose(); HOperatorSet.FastThreshold(ho_A2_Reduced, out ho_A2Region, 50, 150, 20); ho_A2_RegionBorder.Dispose(); HOperatorSet.Boundary(ho_A2Region, out ho_A2_RegionBorder, "inner"); hv_Rec_W = 11; hv_Rec_H = 11; ho_A2_RegionDilation.Dispose(); HOperatorSet.DilationRectangle1(ho_A2_RegionBorder, out ho_A2_RegionDilation, hv_Rec_W, hv_Rec_H); hv_A2_Alpha = 0.9; hv_a2low = 20; hv_a2high = 110; ho_A2_Edges.Dispose(); HOperatorSet.EdgesSubPix(ho_A2_Reduced, out ho_A2_Edges, "canny", hv_A2_Alpha, hv_a2low, hv_a2high); //stop () hv_a2Limit = 200; ho_A2_Rectangles.Dispose(); HOperatorSet.SelectShapeXld(ho_A2_Edges, out ho_A2_Rectangles, "contlength", "and", hv_a2Limit, 99999); HOperatorSet.CountObj(ho_A2_Rectangles, out hv_A2_RecNumber); while ((int)(new HTuple(hv_A2_RecNumber.TupleGreater(1))) != 0) { hv_a2Limit = hv_a2Limit + 10; ho_A2_Rectangles.Dispose(); HOperatorSet.SelectShapeXld(ho_A2_Edges, out ho_A2_Rectangles, "contlength", "and", hv_a2Limit, 99999); HOperatorSet.CountObj(ho_A2_Rectangles, out hv_A2_RecNumber); } //stop () HOperatorSet.FitRectangle2ContourXld(ho_A2_Rectangles, "regression", -1, 0, 0, 3, 2, out hv_A2Row, out hv_A2Column, out hv_A2Phi, out hv_A2Length1, out hv_A2Length2, out hv_A2PointOrder); HOperatorSet.CountObj(ho_A2_Rectangles, out hv_A2Number); //取A2 if ((int)(new HTuple(hv_A2Number.TupleGreater(0))) != 0) { hv_A2_Center_X = hv_A2Column[0]; hv_A2_Center_Y = hv_A2Row[0]; ho_A2Cross.Dispose(); HOperatorSet.GenCrossContourXld(out ho_A2Cross, hv_A2Row, hv_A2Column, 10, 0); mResult = new LineResult() { Row1 = new HTuple(hv_A2Row), Col1 = new HTuple(hv_A2Column), }; } #endregion //step 2 取點 A3,金手指由右數來第一個的中心點 hv_A3_Center_X = 0; hv_A3_Center_Y = 0; //****A3 //STD A3_ 位置 hv_STD_A3_Row = 393; hv_STD_A3_Col = 1330; //STD 向量 STD_A3_ hv_STD_A3_V_Row = hv_STD_A3_Row - hv_STD_Row; hv_STD_A3_V_Col = hv_STD_A3_Col - hv_STD_Col; //A3_X, A3_Y 分量 hv_A3_X = (hv_STD_A3_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_A3_V_Row * (hv_Img_Rotate_Angle.TupleSin() )); hv_A3_Y = (hv_STD_A3_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_A3_V_Col * (hv_Img_Rotate_Angle.TupleSin() )); //目前圖形 A3_ 位置 hv_A3_Pos_Row = (hv_STD_Row + hv_A3_Y) + hv_OffsetRow; hv_A3_Pos_Col = (hv_STD_Col + hv_A3_X) + hv_OffsetCol; //A3_ROI hv_A3_ROI_W = 40; hv_A3_ROI_H = 140; ho_A3_Region.Dispose(); HOperatorSet.GenRectangle2(out ho_A3_Region, hv_A3_Pos_Row, hv_A3_Pos_Col, hv_Img_Rotate_Angle, hv_A3_ROI_W, hv_A3_ROI_H); //stop () ho_A3_Reduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_A3_Region, out ho_A3_Reduced); //fit_Rectangle, 找金手指 ho_A3Region.Dispose(); HOperatorSet.FastThreshold(ho_A3_Reduced, out ho_A3Region, 100, 250, 20); ho_A3_RegionBorder.Dispose(); HOperatorSet.Boundary(ho_A3Region, out ho_A3_RegionBorder, "inner"); if (HDevWindowStack.IsOpen()) { //dev_display (A3_RegionBorder) } hv_Rec_W = 11; hv_Rec_H = 11; ho_A3_RegionDilation.Dispose(); HOperatorSet.DilationRectangle1(ho_A3_RegionBorder, out ho_A3_RegionDilation, hv_Rec_W, hv_Rec_H); //alpha 大容錯大 hv_A3_Alpha = 4; hv_low = 10; hv_high = 100; ho_A3_Edges.Dispose(); HOperatorSet.EdgesSubPix(ho_A3_Reduced, out ho_A3_Edges, "canny", hv_A3_Alpha, hv_low, hv_high); //stop () hv_a3Limit = 200; ho_A3_Rectangles.Dispose(); HOperatorSet.SelectShapeXld(ho_A3_Edges, out ho_A3_Rectangles, "contlength", "and", hv_a3Limit, 99999); HOperatorSet.CountObj(ho_A3_Rectangles, out hv_A3RecNumber); while ((int)(new HTuple(hv_A3RecNumber.TupleGreater(1))) != 0) { hv_a3Limit = hv_a3Limit + 10; ho_A3_Rectangles.Dispose(); HOperatorSet.SelectShapeXld(ho_A3_Edges, out ho_A3_Rectangles, "contlength", "and", hv_a3Limit, 99999); HOperatorSet.CountObj(ho_A3_Rectangles, out hv_A3RecNumber); } HOperatorSet.FitRectangle2ContourXld(ho_A3_Rectangles, "regression", -1, 0, 0, 3, 2, out hv_A3Row, out hv_A3Column, out hv_A3Phi, out hv_A3Length1, out hv_A3Length2, out hv_A3PointOrder); //取A3 中心點 HOperatorSet.CountObj(ho_A3_Rectangles, out hv_A3Number); if ((int)(new HTuple(hv_A3Number.TupleGreater(0))) != 0) { ho_A3Object.Dispose(); HOperatorSet.SelectObj(ho_A3_Rectangles, out ho_A3Object, 1); //stop () //取 A2Object 資訊 hv_A3_Center_X = hv_A3Column[0]; hv_A3_Center_Y = hv_A3Row[0]; ho_A3Cross.Dispose(); HOperatorSet.GenCrossContourXld(out ho_A3Cross, hv_A3Row, hv_A3Column, 10, 0); if (hv_A3Row.TupleLength() > 0 && hv_A2Row.TupleLength() > 0) { mResult.Row2 = new HTuple(hv_A3Row); mResult.Col2 = new HTuple(hv_A3Column); HOperatorSet.DistancePp(hv_A2Row, hv_A2Column, hv_A3Row, hv_A3Column, out mResult.Distance); } } //*****A3 End ho_A2_Region.Dispose(); ho_A2_Reduced.Dispose(); ho_A2Region.Dispose(); ho_A2_RegionBorder.Dispose(); ho_A2_RegionDilation.Dispose(); ho_A2_Edges.Dispose(); ho_A2_Rectangles.Dispose(); ho_A2Cross.Dispose(); ho_A3_Region.Dispose(); ho_A3_Reduced.Dispose(); ho_A3Region.Dispose(); ho_A3_RegionBorder.Dispose(); ho_A3_RegionDilation.Dispose(); ho_A3_Edges.Dispose(); ho_A3_Rectangles.Dispose(); ho_A3Object.Dispose(); ho_A3Cross.Dispose(); return(mResult); }
// Procedures public void GetRegionByBinaryThreshold(HObject ho_Image, out HObject ho_FoundRegion, HTuple hv_MedianRadius, HTuple hv_EmpWidth, HTuple hv_EmpHeight, HTuple hv_EmpFactor, HTuple hv_LightDark, HTuple hv_AreaMin, HTuple hv_AreaMax, HTuple hv_ClosingRadius, HTuple hv_DilationRadius) { // Stack for temporary objects HObject[] OTemp = new HObject[20]; // Local iconic variables HObject ho_ImageMedian, ho_ImageEmphasize; HObject ho_Regions, ho_ConnectedRegions, ho_SelectedRegions; HObject ho_RegionFillUp, ho_RegionUnion; // Local control variables HTuple hv_UsedThreshold = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_FoundRegion); HOperatorSet.GenEmptyObj(out ho_ImageMedian); HOperatorSet.GenEmptyObj(out ho_ImageEmphasize); HOperatorSet.GenEmptyObj(out ho_Regions); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_RegionFillUp); HOperatorSet.GenEmptyObj(out ho_RegionUnion); ho_ImageMedian.Dispose(); HOperatorSet.MedianImage(ho_Image, out ho_ImageMedian, "circle", hv_MedianRadius, "continued"); ho_ImageEmphasize.Dispose(); HOperatorSet.Emphasize(ho_ImageMedian, out ho_ImageEmphasize, hv_EmpWidth, hv_EmpHeight, hv_EmpFactor); ho_Regions.Dispose(); HOperatorSet.BinaryThreshold(ho_ImageEmphasize, out ho_Regions, "max_separability", hv_LightDark, out hv_UsedThreshold); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_Regions, out ho_ConnectedRegions); ho_SelectedRegions.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area", "and", hv_AreaMin, hv_AreaMax); ho_RegionFillUp.Dispose(); HOperatorSet.FillUp(ho_SelectedRegions, out ho_RegionFillUp); if ((int)(new HTuple(hv_ClosingRadius.TupleGreater(0))) != 0) { { HObject ExpTmpOutVar_0; HOperatorSet.ClosingCircle(ho_RegionFillUp, out ExpTmpOutVar_0, hv_ClosingRadius); ho_RegionFillUp.Dispose(); ho_RegionFillUp = ExpTmpOutVar_0; } } if ((int)(new HTuple(hv_DilationRadius.TupleGreater(0))) != 0) { { HObject ExpTmpOutVar_0; HOperatorSet.DilationCircle(ho_RegionFillUp, out ExpTmpOutVar_0, hv_DilationRadius); ho_RegionFillUp.Dispose(); ho_RegionFillUp = ExpTmpOutVar_0; } } ho_RegionUnion.Dispose(); HOperatorSet.Union1(ho_RegionFillUp, out ho_RegionUnion); ho_FoundRegion.Dispose(); HOperatorSet.MoveRegion(ho_RegionUnion, out ho_FoundRegion, 0, 0); ho_ImageMedian.Dispose(); ho_ImageEmphasize.Dispose(); ho_Regions.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_RegionFillUp.Dispose(); ho_RegionUnion.Dispose(); return; }
// Main procedure private void porosityHorizonatal() { // Local iconic variables HObject ho_Image = null, ho_Rectangle = null, ho_ImageReduced = null; HObject ho_ImageMean = null, ho_Region = null, ho_ConnectedRegions = null; HObject ho_RegionClosing = null, ho_SmallConnection = null; HObject ho_ContCircle = null; // Local control variables HTuple hv_AcqHandle = null, hv_found = null; HTuple hv_cnt = null, hv_bol = null, hv_porosity_area_px = null; HTuple hv_porosity_area_mm = null; HTuple hv_UsedThreshold = new HTuple(), hv_Circularity = new HTuple(); HTuple hv_Area = new HTuple(), hv_Row = new HTuple(), hv_Column = new HTuple(); HTuple hv_Length = new HTuple(), hv_circ_min = new HTuple(); HTuple hv_area_min = new HTuple(), hv_index = new HTuple(); HTuple hv_i = 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_ImageMean); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_RegionClosing); HOperatorSet.GenEmptyObj(out ho_SmallConnection); HOperatorSet.GenEmptyObj(out ho_ContCircle); // Wait for CAM4 thread to be closed _waitHandleCam3.WaitOne(); // Close te thread DOOR _waitHandleCam3.Reset(); // Open camera frame HOperatorSet.OpenFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1, "default", -1, "false", "default", "GC2591MP_CAM_3", 0, -1, out hv_AcqHandle); HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "ExposureTime", 35000.0); // 30 000 // Information for PLC that frame is opened DetectionHorStart(); hv_found = 0; hv_cnt = 0; hv_bol = 0; Porositydetectedhor = false; while (Porositydetectedhor == false) { ho_Image.Dispose(); HOperatorSet.GrabImage(out ho_Image, hv_AcqHandle); ho_Rectangle.Dispose(); //HOperatorSet.GenRectangle1(out ho_Rectangle, 520, 200, 1050, 700); //HOperatorSet.GenRectangle1(out ho_Rectangle, 1020, 200, 1460, 700); HOperatorSet.GenRectangle1(out ho_Rectangle, 900, 200, 1460, 700); //HOperatorSet.GenRectangle1(out ho_Rectangle, 1120, 200, 1630, 700); ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_Rectangle, out ho_ImageReduced); ho_ImageMean.Dispose(); HOperatorSet.MeanImage(ho_ImageReduced, out ho_ImageMean, 21, 21); ho_Region.Dispose(); HOperatorSet.BinaryThreshold(ho_ImageMean, out ho_Region, "max_separability", "dark", out hv_UsedThreshold); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions); ho_RegionClosing.Dispose(); HOperatorSet.ClosingCircle(ho_ConnectedRegions, out ho_RegionClosing, 17); ho_SmallConnection.Dispose(); HOperatorSet.Connection(ho_RegionClosing, out ho_SmallConnection); HOperatorSet.Circularity(ho_SmallConnection, out hv_Circularity); HOperatorSet.AreaCenter(ho_SmallConnection, out hv_Area, out hv_Row, out hv_Column); HOperatorSet.TupleLength(hv_Row, out hv_Length); // Criteria for porosity hv_circ_min = 0.5; hv_area_min = 300; // 300 - HDev //1000 hv_index = 0; HTuple end_val36 = hv_Length; HTuple step_val36 = 1; for (hv_i = 1; hv_i.Continue(end_val36, step_val36); hv_i = hv_i.TupleAdd(step_val36)) { if ((int)((new HTuple(((hv_Circularity.TupleSelect(hv_i - 1))).TupleGreater( hv_circ_min))).TupleAnd(new HTuple(((hv_Area.TupleSelect(hv_i - 1))).TupleGreater( hv_area_min)))) != 0) { hv_index = hv_i - 1; hv_found = hv_found + 1; hv_bol = 1; break; } else { hv_bol = 0; } } //HOperatorSet.ClearWindow(hv_porosityWinHandle); //HOperatorSet.DispObj(ho_Image, hv_porosityWinHandle); if ((int)((new HTuple(hv_found.TupleGreater(0))).TupleAnd(new HTuple(hv_bol.TupleEqual( 1)))) != 0) { PorosityIsDetected(); ho_ContCircle.Dispose(); //HOperatorSet.GenCircleContourXld(out ho_ContCircle, hv_Row.TupleSelect(hv_index), // hv_Column.TupleSelect(hv_index), 50, 0, 6.28318, "positive", 1); //HOperatorSet.DispObj(ho_ContCircle, hv_porosityWinHandle); hv_found = 0; hv_cnt = 0; Porositydetectedhor = true; } hv_cnt = hv_cnt + 1; } //HOperatorSet.ClearWindow(hv_porosityWinHandle); HOperatorSet.CloseFramegrabber(hv_AcqHandle); // Open the thread DOOR _waitHandleCam3.Set(); // Dispose all iconic variables ho_Image.Dispose(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_ImageMean.Dispose(); ho_Region.Dispose(); ho_ConnectedRegions.Dispose(); ho_RegionClosing.Dispose(); ho_SmallConnection.Dispose(); ho_ContCircle.Dispose(); }
public void Action(HWindow hv_ExpDefaultWinHandle) { // Stack for temporary objects HObject[] OTemp = new HObject[20]; long SP_O = 0; // Local iconic variables HObject ho_Image, ho_Image1, ho_Image2, ho_Image3; HObject ho_ImageResult1, ho_ImageResult2, ho_ImageResult3; HObject ho_ClassRegionsNotRejected, ho_ObjectSelected, ho_RegionOpening2; HObject ho_ConnectedRegions, ho_SelectedRegions, ho_RegionUnion; HObject ho_RegionOpening, ho_RegionTrans, ho_ImageReduced; HObject ho_ObjectSelected1, ho_RegionOpening1, ho_RegionFillUp; HObject ho_ConnectedRegions1, ho_EmptyObject, ho_ObjectSelected2 = null; HObject ho_RegionDilation = null, ho_RegionUnion1, ho_ConnectedRegions2; HObject ho_SelectedRegions1, ho_Rectangle; // Local control variables HTuple hv_WindowHandle = new HTuple(); HTuple hv_Area2, hv_CircleRow, hv_CircleCol; HTuple hv_Number, hv_R, hv_Index1, hv_Area3 = new HTuple(); HTuple hv_Row2 = new HTuple(), hv_Column2 = new HTuple(), hv_Row1; HTuple hv_Column1, hv_Phi, hv_Length1, hv_Length2, hv_Area; HTuple hv_Row, hv_Column; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_Image1); HOperatorSet.GenEmptyObj(out ho_Image2); HOperatorSet.GenEmptyObj(out ho_Image3); HOperatorSet.GenEmptyObj(out ho_ImageResult1); HOperatorSet.GenEmptyObj(out ho_ImageResult2); HOperatorSet.GenEmptyObj(out ho_ImageResult3); HOperatorSet.GenEmptyObj(out ho_ClassRegionsNotRejected); HOperatorSet.GenEmptyObj(out ho_ObjectSelected); HOperatorSet.GenEmptyObj(out ho_RegionOpening2); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_RegionUnion); HOperatorSet.GenEmptyObj(out ho_RegionOpening); HOperatorSet.GenEmptyObj(out ho_RegionTrans); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_ObjectSelected1); HOperatorSet.GenEmptyObj(out ho_RegionOpening1); HOperatorSet.GenEmptyObj(out ho_RegionFillUp); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1); HOperatorSet.GenEmptyObj(out ho_EmptyObject); HOperatorSet.GenEmptyObj(out ho_ObjectSelected2); HOperatorSet.GenEmptyObj(out ho_RegionDilation); HOperatorSet.GenEmptyObj(out ho_RegionUnion1); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions2); HOperatorSet.GenEmptyObj(out ho_SelectedRegions1); HOperatorSet.GenEmptyObj(out ho_Rectangle); try { ho_Image.Dispose(); //HOperatorSet.ReadImage(out ho_Image, new HTuple(new HTuple("C:/Code/Halcon/检测有无/图片1/") + 13) + ".bmp"); HOperatorSet.GrabImage(out ho_Image, hv_AcqHandle); //HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle); ho_image_Copy = ho_Image.SelectObj(1); HOperatorSet.GetImageSize(ho_Image, out HTuple width, out HTuple height); //HOperatorSet.ReadClassMlp("mlp_1.gmc", out hv_MLPHandle); //HOperatorSet.ReadClassMlp("mlp_3.gmc", out hv_MLPHandle1); HOperatorSet.SetTposition(hv_ExpDefaultWinHandle, 10, 10); set_display_font(hv_ExpDefaultWinHandle, (int)ParaSetting.FontSize, "mono", "true", "false"); ho_Image1.Dispose(); ho_Image2.Dispose(); ho_Image3.Dispose(); HOperatorSet.Decompose3(ho_Image, out ho_Image1, out ho_Image2, out ho_Image3 ); ho_ImageResult1.Dispose(); ho_ImageResult2.Dispose(); ho_ImageResult3.Dispose(); HOperatorSet.TransFromRgb(ho_Image1, ho_Image2, ho_Image3, out ho_ImageResult1, out ho_ImageResult2, out ho_ImageResult3, "hsv"); //HOperatorSet.DispObj(ho_Image3, hv_ExpDefaultWinHandle); //HOperatorSet.SetColored(hv_ExpDefaultWinHandle, 12); ho_ClassRegionsNotRejected.Dispose(); HOperatorSet.ClassifyImageClassMlp(ho_Image, out ho_ClassRegionsNotRejected, hv_MLPHandle, 0.3); ho_ObjectSelected.Dispose(); HOperatorSet.SelectObj(ho_ClassRegionsNotRejected, out ho_ObjectSelected, 1); ho_RegionOpening2.Dispose(); HOperatorSet.OpeningCircle(ho_ObjectSelected, out ho_RegionOpening2, 5); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_RegionOpening2, out ho_ConnectedRegions); //area_center (ConnectedRegions, Area1, Row3, Column3) ho_SelectedRegions.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area", "and", 6000.2, 309205); ho_RegionUnion.Dispose(); HOperatorSet.Union1(ho_SelectedRegions, out ho_RegionUnion); ho_RegionOpening.Dispose(); HOperatorSet.OpeningCircle(ho_RegionUnion, out ho_RegionOpening, 3.5); ho_RegionTrans.Dispose(); HOperatorSet.ShapeTrans(ho_RegionOpening, out ho_RegionTrans, "circle"); //HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "fill"); //HOperatorSet.ClearWindow(hv_ExpDefaultWinHandle); //HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle); //HOperatorSet.DispObj(ho_RegionTrans, hv_ExpDefaultWinHandle); //HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "fill"); ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_RegionTrans, out ho_ImageReduced); HOperatorSet.AreaCenter(ho_RegionTrans, out hv_Area2, out hv_CircleRow, out hv_CircleCol); ho_ClassRegionsNotRejected.Dispose(); HOperatorSet.ClassifyImageClassMlp(ho_ImageReduced, out ho_ClassRegionsNotRejected, hv_MLPHandle1, 0.3); //HOperatorSet.SetColored(hv_ExpDefaultWinHandle, 12); //HOperatorSet.DispObj(ho_ClassRegionsNotRejected, hv_ExpDefaultWinHandle); ho_ObjectSelected1.Dispose(); HOperatorSet.SelectObj(ho_ClassRegionsNotRejected, out ho_ObjectSelected1, 4); ho_RegionOpening1.Dispose(); HOperatorSet.OpeningCircle(ho_ObjectSelected1, out ho_RegionOpening1, 3.5); ho_RegionFillUp.Dispose(); HOperatorSet.FillUp(ho_RegionOpening1, out ho_RegionFillUp); //closing_circle (RegionFillUp, RegionClosing, 10) ho_ConnectedRegions1.Dispose(); HOperatorSet.Connection(ho_RegionFillUp, out ho_ConnectedRegions1); HOperatorSet.CountObj(ho_ConnectedRegions1, out hv_Number); //找除靠近中心的圆 ho_EmptyObject.Dispose(); HOperatorSet.GenEmptyObj(out ho_EmptyObject); hv_R = 200; for (hv_Index1 = 1; hv_Index1.Continue(hv_Number, 1); hv_Index1 = hv_Index1.TupleAdd(1)) { ho_ObjectSelected2.Dispose(); HOperatorSet.SelectObj(ho_ConnectedRegions1, out ho_ObjectSelected2, hv_Index1); HOperatorSet.AreaCenter(ho_ObjectSelected2, out hv_Area3, out hv_Row2, out hv_Column2); if ((int)((new HTuple((new HTuple((new HTuple(hv_Row2.TupleGreater(hv_CircleRow - hv_R))).TupleAnd( new HTuple(hv_Row2.TupleLess(hv_CircleRow + hv_R))))).TupleAnd(new HTuple(hv_Column2.TupleGreater( hv_CircleCol - hv_R))))).TupleAnd(new HTuple(hv_Column2.TupleLess(hv_CircleCol + hv_R)))) != 0) { ho_RegionDilation.Dispose(); HOperatorSet.DilationCircle(ho_ObjectSelected2, out ho_RegionDilation, 10); OTemp[SP_O] = ho_EmptyObject.CopyObj(1, -1); SP_O++; ho_EmptyObject.Dispose(); HOperatorSet.ConcatObj(ho_RegionDilation, OTemp[SP_O - 1], out ho_EmptyObject ); OTemp[SP_O - 1].Dispose(); SP_O = 0; } } ho_RegionUnion1.Dispose(); HOperatorSet.Union1(ho_EmptyObject, out ho_RegionUnion1); ho_ConnectedRegions2.Dispose(); HOperatorSet.Connection(ho_RegionUnion1, out ho_ConnectedRegions2); ho_SelectedRegions1.Dispose(); HOperatorSet.SelectShapeStd(ho_ConnectedRegions2, out ho_SelectedRegions1, "max_area", 70); HOperatorSet.SmallestRectangle2(ho_SelectedRegions1, 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); HOperatorSet.AreaCenter(ho_RegionFillUp, out hv_Area, out hv_Row, out hv_Column); HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "margin"); HOperatorSet.ClearWindow(hv_ExpDefaultWinHandle); lock (SyncData.VisionLock) { if (SyncData.IsNewSizing == false) { if (SyncData.IsNewSizing != SyncData.IsOldSizing) { Thread.Sleep(300); SyncData.IsOldSizing = SyncData.IsNewSizing; Console.WriteLine("Trig"); } HOperatorSet.SetPart(hv_ExpDefaultWinHandle, 0, 0, height, width); HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle); if (hv_Length1 > ParaSetting.MinL1 && hv_Length1 < ParaSetting.MaxL1 && hv_Length2 > ParaSetting.MinL2 && hv_Length2 < ParaSetting.MaxL2) { HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green"); HOperatorSet.DispObj(ho_Rectangle, hv_ExpDefaultWinHandle); HOperatorSet.WriteString(hv_ExpDefaultWinHandle, "OK"); if (ParaSetting.UseOutput == EnumUseOutput.Use) { HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "UserOutputValue", ParaSetting.OutputLogicNG == EnumOutputLogic.False ? 1 : 0); } } else { HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "red"); HOperatorSet.WriteString(hv_ExpDefaultWinHandle, "NG"); if (ParaSetting.UseOutput == EnumUseOutput.Use) { HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "UserOutputValue", ParaSetting.OutputLogicNG == EnumOutputLogic.False ? 0 : 1); } } HOperatorSet.SetTposition(hv_ExpDefaultWinHandle, 160, 10); HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green"); set_display_font(hv_ExpDefaultWinHandle, (int)(ParaSetting.FontSize), "mono", "true", "false"); HOperatorSet.WriteString(hv_ExpDefaultWinHandle, $"L1={hv_Length1}, L2={hv_Length2}"); } } } catch (HalconException HDevExpDefaultException) { lock (SyncData.VisionLock) { if (SyncData.IsNewSizing == false) { if (SyncData.IsNewSizing != SyncData.IsOldSizing) { Thread.Sleep(300); SyncData.IsOldSizing = SyncData.IsNewSizing; Console.WriteLine("Trig"); } HOperatorSet.SetTposition(hv_ExpDefaultWinHandle, 10, 10); set_display_font(hv_ExpDefaultWinHandle, (int)(ParaSetting.FontSize), "mono", "true", "false"); HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "red"); HOperatorSet.WriteString(hv_ExpDefaultWinHandle, "NG"); } } if (ParaSetting.UseOutput == EnumUseOutput.Use) { HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "UserOutputValue", ParaSetting.OutputLogicNG == EnumOutputLogic.False ? 0 : 1); } ho_Image.Dispose(); ho_Image1.Dispose(); ho_Image2.Dispose(); ho_Image3.Dispose(); ho_ImageResult1.Dispose(); ho_ImageResult2.Dispose(); ho_ImageResult3.Dispose(); ho_ClassRegionsNotRejected.Dispose(); ho_ObjectSelected.Dispose(); ho_RegionOpening2.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_RegionUnion.Dispose(); ho_RegionOpening.Dispose(); ho_RegionTrans.Dispose(); ho_ImageReduced.Dispose(); ho_ObjectSelected1.Dispose(); ho_RegionOpening1.Dispose(); ho_RegionFillUp.Dispose(); ho_ConnectedRegions1.Dispose(); ho_EmptyObject.Dispose(); ho_ObjectSelected2.Dispose(); ho_RegionDilation.Dispose(); ho_RegionUnion1.Dispose(); ho_ConnectedRegions2.Dispose(); ho_SelectedRegions1.Dispose(); ho_Rectangle.Dispose(); throw HDevExpDefaultException; } ho_Image.Dispose(); ho_Image1.Dispose(); ho_Image2.Dispose(); ho_Image3.Dispose(); ho_ImageResult1.Dispose(); ho_ImageResult2.Dispose(); ho_ImageResult3.Dispose(); ho_ClassRegionsNotRejected.Dispose(); ho_ObjectSelected.Dispose(); ho_RegionOpening2.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_RegionUnion.Dispose(); ho_RegionOpening.Dispose(); ho_RegionTrans.Dispose(); ho_ImageReduced.Dispose(); ho_ObjectSelected1.Dispose(); ho_RegionOpening1.Dispose(); ho_RegionFillUp.Dispose(); ho_ConnectedRegions1.Dispose(); ho_EmptyObject.Dispose(); ho_ObjectSelected2.Dispose(); ho_RegionDilation.Dispose(); ho_RegionUnion1.Dispose(); ho_ConnectedRegions2.Dispose(); ho_SelectedRegions1.Dispose(); ho_Rectangle.Dispose(); }
// 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 MeasureResult Action() { #region 輸出結果 CircleResult mResult = null; #endregion // Local iconic variables HObject ho_R17_Circle = null; HObject ho_R17_ROI_Image = null, ho_R17_Region = null, ho_R17_ImageReduced = null; HObject ho_R17_Edges = null, ho_R17_ContoursSplit = null, ho_R17_SingleSegment = null; HObject ho_R17_ContEllipse = null; // Local control variables HTuple hv_msgOffsetY, hv_msgOffsetX; HTuple hv_STD_Row; HTuple hv_STD_Col, hv_Img_Row, hv_Img_Col, hv_Img_Rotate_Angle; HTuple hv_OffsetRow, hv_OffsetCol, hv_STD_R17_Row, hv_STD_R17_Col; HTuple hv_STD_R17_V_Row, hv_STD_R17_V_Col, hv_R17_X, hv_R17_Y; HTuple hv_R17_Pos_Row, hv_R17_Pos_Col, hv_R17_R; HTuple hv_alpha = new HTuple(), hv_R17_low = new HTuple(); HTuple hv_R17_high = new HTuple(), hv_R17_NumSegments = new HTuple(); HTuple hv_NumCircles = new HTuple(), hv_Num_Circle_Point = new HTuple(); HTuple hv_R17 = new HTuple(), hv_i = new HTuple(), hv_Attrib = new HTuple(); HTuple hv_R17_Row = new HTuple(), hv_R17_Column = new HTuple(); HTuple hv_R17_Radius = new HTuple(), hv_R17_StartPhi = new HTuple(); HTuple hv_R17_EndPhi = new HTuple(), hv_R17_PointOrder = new HTuple(); HTuple hv_R17_MinDist = new HTuple(), hv_R17_MaxDist = new HTuple(); HTuple hv_R17_AvgDist = new HTuple(), hv_R17_SigmaDist = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_R17_Circle); HOperatorSet.GenEmptyObj(out ho_R17_ROI_Image); HOperatorSet.GenEmptyObj(out ho_R17_Region); HOperatorSet.GenEmptyObj(out ho_R17_ImageReduced); HOperatorSet.GenEmptyObj(out ho_R17_Edges); HOperatorSet.GenEmptyObj(out ho_R17_ContoursSplit); HOperatorSet.GenEmptyObj(out ho_R17_SingleSegment); HOperatorSet.GenEmptyObj(out ho_R17_ContEllipse); //Measure: SDMS_R17 //Author: John Hsieh //Date: 2012 // dev_update_off(...); only in hdevelop HOperatorSet.SetSystem("border_shape_models", "false"); //****Message Args hv_msgOffsetY = 100; hv_msgOffsetX = 100; //****Model Args //STD 中心點 hv_STD_Row = 772; hv_STD_Col = 1003; //目前圖形 中心點 hv_Img_Row = hv_AllModelRow.Clone(); hv_Img_Col = hv_AllModelColumn.Clone(); //目前圖形 Rotate Angle hv_Img_Rotate_Angle = hv_AllModelAngle.Clone(); //目前圖形偏移量 hv_OffsetRow = hv_Img_Row - hv_STD_Row; hv_OffsetCol = hv_Img_Col - hv_STD_Col; //****Display if (HDevWindowStack.IsOpen()) { HOperatorSet.ClearWindow(HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } //*****R17 //STD R17_ 位置 hv_STD_R17_Row = 802; hv_STD_R17_Col = 660; //STD 向量 STD_R17_ hv_STD_R17_V_Row = hv_STD_R17_Row - hv_STD_Row; hv_STD_R17_V_Col = hv_STD_R17_Col - hv_STD_Col; //R17_X, R17_Y 分量 hv_R17_X = (hv_STD_R17_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_R17_V_Row * (hv_Img_Rotate_Angle.TupleSin() )); hv_R17_Y = (hv_STD_R17_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_R17_V_Col * (hv_Img_Rotate_Angle.TupleSin() )); //目前圖形 R17_ 位置 hv_R17_Pos_Row = (hv_STD_Row + hv_R17_Y) + hv_OffsetRow; hv_R17_Pos_Col = (hv_STD_Col + hv_R17_X) + hv_OffsetCol; hv_R17_R = 17; ho_R17_Circle.Dispose(); HOperatorSet.GenCircle(out ho_R17_Circle, hv_R17_Pos_Row, hv_R17_Pos_Col, hv_R17_R); if (HDevWindowStack.IsOpen()) { //dev_display (R17_Circle) } //stop () ho_R17_ROI_Image.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_R17_Circle, out ho_R17_ROI_Image); ho_R17_Region.Dispose(); HOperatorSet.FastThreshold(ho_R17_ROI_Image, out ho_R17_Region, 100, 255, 15); ho_R17_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_R17_ROI_Image, ho_R17_Region, out ho_R17_ImageReduced ); //stop () //sobel_fast 具有較寬的選擇範圍,搭配 alpha 參數 (alpha 越大, 容錯範圍大) hv_alpha = 0.9; hv_R17_low = 10; hv_R17_high = 60; ho_R17_Edges.Dispose(); HOperatorSet.EdgesSubPix(ho_R17_ImageReduced, out ho_R17_Edges, "sobel_fast", hv_alpha, hv_R17_low, hv_R17_high); //stop () //*所有的數值越小,表示容錯範圍大,反之亦然 ho_R17_ContoursSplit.Dispose(); HOperatorSet.SegmentContoursXld(ho_R17_Edges, out ho_R17_ContoursSplit, "lines_circles", 17, 1, 1); //Display the results //=========================================================== HOperatorSet.CountObj(ho_R17_ContoursSplit, out hv_R17_NumSegments); hv_NumCircles = 0; hv_Num_Circle_Point = 0; hv_R17 = 999; for (hv_i = 1; hv_i.Continue(hv_R17_NumSegments, 1); hv_i = hv_i.TupleAdd(1)) { ho_R17_SingleSegment.Dispose(); HOperatorSet.SelectObj(ho_R17_ContoursSplit, out ho_R17_SingleSegment, hv_i); HOperatorSet.GetContourGlobalAttribXld(ho_R17_SingleSegment, "cont_approx", out hv_Attrib); if ((int)(new HTuple(hv_Attrib.TupleEqual(1))) != 0) { HOperatorSet.FitCircleContourXld(ho_R17_SingleSegment, "atukey", -1, 2, hv_Num_Circle_Point, 5, 2, out hv_R17_Row, out hv_R17_Column, out hv_R17_Radius, out hv_R17_StartPhi, out hv_R17_EndPhi, out hv_R17_PointOrder); ho_R17_ContEllipse.Dispose(); HOperatorSet.GenEllipseContourXld(out ho_R17_ContEllipse, hv_R17_Row, hv_R17_Column, 0, hv_R17_Radius, hv_R17_Radius, 0, (new HTuple(360)).TupleRad(), "positive", 1.0); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_R17_ContEllipse, HDevWindowStack.GetActive()); } HOperatorSet.DistEllipseContourXld(ho_R17_SingleSegment, "algebraic", -1, 0, hv_R17_Row, hv_R17_Column, 0, hv_R17_Radius, hv_R17_Radius, out hv_R17_MinDist, out hv_R17_MaxDist, out hv_R17_AvgDist, out hv_R17_SigmaDist); hv_NumCircles = hv_NumCircles + 1; if ((int)(new HTuple(hv_R17.TupleGreater(hv_R17_Radius))) != 0) { hv_R17 = hv_R17_Radius.Clone(); mResult = new CircleResult() { Row = new HTuple(hv_R17_Row), Col = new HTuple(hv_R17_Column), Radius = new HTuple(hv_R17_Radius), StartPhi = new HTuple(hv_R17_StartPhi), EndPhi = new HTuple(hv_R17_EndPhi), PointOrder = new HTuple(hv_R17_PointOrder), }; } //stop () } } ho_R17_Circle.Dispose(); ho_R17_ROI_Image.Dispose(); ho_R17_Region.Dispose(); ho_R17_ImageReduced.Dispose(); ho_R17_Edges.Dispose(); ho_R17_ContoursSplit.Dispose(); ho_R17_SingleSegment.Dispose(); ho_R17_ContEllipse.Dispose(); return mResult; }
//DateTime t1, t2, t3, t4,t5,t6,t7; private void action() { HObject ho_Rectangle, ho_ImageReduced, imagexz, retxz; HObject ho_Border, ho_Circle, ho_ImageReduced2, ho_Region, ho_RegionLines; HObject ho_Rectangle1 = null, ho_ImageReduced1 = null, ho_Border1 = null; HObject ho_SelectedContours = null; // Local control variables HTuple hv_Row1 = null, hv_Column1 = null, hv_Angle = null, hv_l1 = null, hv_l2 = null; HTuple hv_Score = null, hv_zj = null, hv_Area = null, hv_Row3 = null; HTuple hv_Column2 = null, hv_Phi1 = null, hv_zpj = null; HTuple hv_zcj = null, hv_zpr = null, hv_zpc = null, hv_zcr = null; HTuple hv_zcc = null, hv_Tp = null, hv_Tc = null, hv_Length = null; HTuple hv_dx = null, hv_dn = null, hv_dxr = null, hv_dxc = null; HTuple hv_dnr = null, hv_dnc = null, hv_i = null; //HTuple hv_Col = new HTuple(), hv_dis = new HTuple(), hv_dxi = new HTuple(), hv_Row2 = new HTuple(); //HTuple hv_dni = new HTuple(), hv_Ix = new HTuple(), hv_In = new HTuple(); //HTuple hv_Exception = new HTuple(), hv_j = new HTuple(), hv_jh = new HTuple(); HTuple hv_disdxpx = null; HTuple hv_disdnpx = null; HTuple hv_djd = null, hv_djdr = null, hv_djdc = null, hv_djx = null; HTuple hv_djxr = null, hv_djxc = null, hv_xjd = null, hv_xjdr = null; HTuple hv_xjdc = null, hv_xjx = null, hv_xjxr = null, hv_xjxc = null; HTuple hv_ljd = null, hv_ljx = null, hv_dj = null, hv_xj = null; HTuple hv_djn = null, hv_djm = null, hv_xjn = null, hv_xjm = null; HTuple hv_djzd = null, hv_djzx = null, hv_xjzd = null; HTuple hv_xjzx = null, hv_ljtd = null, hv_ljtx = null; HTuple hv_Length3 = null, hv_Length4 = null, hv_ljdx = null; HTuple hv_ljdn = null, hv_ljxx = null, hv_ljxn = null; HTuple hv_ljdm = null, hv_ljxm = null, hv_djiaodu = null; HTuple hv_xjiaodu = null, hv_Length5 = null, hv_Length6 = null; HTuple hv_djiaodu1 = null, hv_xjiaodu1 = null, hv_jjdd = null; HTuple hv_jjdx = null, hv_jjxd = null, hv_jjxx = null; HTuple hv_jjdm = null, hv_jjxm = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_Border); HOperatorSet.GenEmptyObj(out ho_Circle); HOperatorSet.GenEmptyObj(out ho_ImageReduced2); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_Rectangle1); HOperatorSet.GenEmptyObj(out ho_ImageReduced1); HOperatorSet.GenEmptyObj(out ho_Border1); HOperatorSet.GenEmptyObj(out ho_SelectedContours); HOperatorSet.GenEmptyObj(out ho_RegionLines); HOperatorSet.GenEmptyObj(out imagexz); HOperatorSet.GenEmptyObj(out retxz); HOperatorSet.Union1(algorithm.Region, out RegionToDisp); //t3 = DateTime.Now; try { if (hv_ModelID == null) { HOperatorSet.ReadShapeModel(PathHelper.currentProductPath + @"\lwspmd.shm", out hv_ModelID); } retxz.Dispose(); HOperatorSet.GenRectangle2(out retxz, hv1_Row, hv1_Column, hv1_Phi, hv1_Length1, hv1_Length2); imagexz.Dispose(); HOperatorSet.ReduceDomain(Image, retxz, out imagexz); HOperatorSet.FindShapeModel(imagexz, hv_ModelID, -3.14, 6.29, 0.7, 0, 0.1, "least_squares", 0, 0.8, out hv_Row1, out hv_Column1, out hv_Angle, out hv_Score); hv_zj = (((((hv_Row1.TupleMax()) - (hv_Row1.TupleMin())) * ((hv_Row1.TupleMax() ) - (hv_Row1.TupleMin()))) + (((hv_Column1.TupleMax()) - (hv_Column1.TupleMin() )) * ((hv_Column1.TupleMax()) - (hv_Column1.TupleMin()))))).TupleSqrt(); ho_Circle.Dispose(); HOperatorSet.GenCircle(out ho_Circle, hv_Row1.TupleMean(), hv_Column1.TupleMean() , hv_zj / 2); ho_ImageReduced2.Dispose(); HOperatorSet.ReduceDomain(Image, ho_Circle, out ho_ImageReduced2); ho_Region.Dispose(); HOperatorSet.Threshold(ho_ImageReduced2, out ho_Region, 0, thv); HOperatorSet.AreaCenter(ho_Region, out hv_Area, out hv_Row3, out hv_Column2); HOperatorSet.OrientationRegion(ho_Region, out hv_Phi1); hv_zpj = hv_Phi1.Clone(); hv_zcj = hv_Phi1 + ((new HTuple(90)).TupleRad()); hv_zpr = hv_Row3 + ((hv_zcj.TupleSin()) * hv_zj); hv_zpc = hv_Column2 - ((hv_zcj.TupleCos()) * hv_zj); hv_zcr = hv_Row3 + ((hv_zpj.TupleSin()) * hv_zj); hv_zcc = hv_Row3 - ((hv_zpj.TupleCos()) * hv_zj); HOperatorSet.TupleTan(hv_zpj, out hv_Tp); HOperatorSet.TupleTan(hv_zcj, out hv_Tc); HOperatorSet.TupleLength(hv_Row1, out hv_Length); hv_dx = new HTuple(); hv_dn = new HTuple(); hv_dxr = new HTuple(); hv_dxc = new HTuple(); hv_dnr = new HTuple(); hv_dnc = new HTuple(); HTuple end_val29 = hv_Length - 1; HTuple step_val29 = 1; for (hv_i = 0; hv_i.Continue(end_val29, step_val29); hv_i = hv_i.TupleAdd(step_val29)) { //try ho_Rectangle1.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle1, hv_Row1.TupleSelect(hv_i), hv_Column1.TupleSelect(hv_i), hv_Phi + (hv_Angle.TupleSelect(hv_i)), hv_Length1, hv_Length2); ho_ImageReduced1.Dispose(); HOperatorSet.ReduceDomain(Image, ho_Rectangle1, out ho_ImageReduced1 ); ho_Border1.Dispose(); HOperatorSet.ThresholdSubPix(ho_ImageReduced1, out ho_Border1, thv); ho_SelectedContours.Dispose(); HOperatorSet.SelectContoursXld(ho_Border1, out ho_SelectedContours, "contour_length", 30, 2000000, -0.5, 0.5); HOperatorSet.UnionAdjacentContoursXld(ho_SelectedContours, out ho_SelectedContours, 10, 1, "attr_keep"); HOperatorSet.GetContourXld(ho_SelectedContours, out hv_Row2, out hv_Col); hv_dis = ((((((hv_Tp * hv_Col) + hv_Row2) - (hv_Tp * hv_zpc)) - hv_zpr) / (((1 + (hv_Tp * hv_Tp))).TupleSqrt() ))).TupleAbs(); hv_dxi = hv_dis.TupleMax(); hv_dni = hv_dis.TupleMin(); HOperatorSet.TupleFind(hv_dis, hv_dxi, out hv_Ix); HOperatorSet.TupleFind(hv_dis, hv_dni, out hv_In); hv_dx = hv_dx.TupleConcat(hv_dxi); hv_dn = hv_dn.TupleConcat(hv_dni); hv_dxr = hv_dxr.TupleConcat(hv_Row2.TupleSelect(hv_Ix)); hv_dxc = hv_dxc.TupleConcat(hv_Col.TupleSelect(hv_Ix)); hv_dnr = hv_dnr.TupleConcat(hv_Row2.TupleSelect(hv_In)); hv_dnc = hv_dnc.TupleConcat(hv_Col.TupleSelect(hv_In)); //catch (Exception) //endtry //stop () } hv_disdxpx = ((((((hv_Tc * hv_dxc) + hv_dxr) - (hv_Tc * hv_zcc)) - hv_zcr) / (((1 + (hv_Tc * hv_Tc))).TupleSqrt() ))).TupleAbs(); hv_disdnpx = ((((((hv_Tc * hv_dnc) + hv_dnr) - (hv_Tc * hv_zcc)) - hv_zcr) / (((1 + (hv_Tc * hv_Tc))).TupleSqrt() ))).TupleAbs(); HTuple end_val55 = hv_Length - 2; HTuple step_val55 = 1; //t1 = DateTime.Now; for (hv_i = 0; hv_i.Continue(end_val55, step_val55); hv_i = hv_i.TupleAdd(step_val55)) { //try HTuple end_val59 = hv_Length - 1; HTuple step_val59 = 1; for (hv_j = hv_i + 1; hv_j.Continue(end_val59, step_val59); hv_j = hv_j.TupleAdd(step_val59)) { if ((int)(new HTuple(((hv_disdxpx.TupleSelect(hv_i))).TupleGreater(hv_disdxpx.TupleSelect( hv_j)))) != 0) { hv_jh = hv_disdxpx.TupleSelect(hv_i); if (hv_disdxpx == null) { hv_disdxpx = new HTuple(); } hv_disdxpx[hv_i] = hv_disdxpx.TupleSelect(hv_j); if (hv_disdxpx == null) { hv_disdxpx = new HTuple(); } hv_disdxpx[hv_j] = hv_jh; hv_jh = hv_dx.TupleSelect(hv_i); if (hv_dx == null) { hv_dx = new HTuple(); } hv_dx[hv_i] = hv_dx.TupleSelect(hv_j); if (hv_dx == null) { hv_dx = new HTuple(); } hv_dx[hv_j] = hv_jh; hv_jh = hv_dxr.TupleSelect(hv_i); if (hv_dxr == null) { hv_dxr = new HTuple(); } hv_dxr[hv_i] = hv_dxr.TupleSelect(hv_j); if (hv_dxr == null) { hv_dxr = new HTuple(); } hv_dxr[hv_j] = hv_jh; hv_jh = hv_dxc.TupleSelect(hv_i); if (hv_dxc == null) { hv_dxc = new HTuple(); } hv_dxc[hv_i] = hv_dxc.TupleSelect(hv_j); if (hv_dxc == null) { hv_dxc = new HTuple(); } hv_dxc[hv_j] = hv_jh; } if ((int)(new HTuple(((hv_disdnpx.TupleSelect(hv_i))).TupleGreater(hv_disdnpx.TupleSelect( hv_j)))) != 0) { hv_jh = hv_disdnpx.TupleSelect(hv_i); if (hv_disdnpx == null) { hv_disdnpx = new HTuple(); } hv_disdnpx[hv_i] = hv_disdnpx.TupleSelect(hv_j); if (hv_disdnpx == null) { hv_disdnpx = new HTuple(); } hv_disdnpx[hv_j] = hv_jh; hv_jh = hv_dn.TupleSelect(hv_i); if (hv_dn == null) { hv_dn = new HTuple(); } hv_dn[hv_i] = hv_dn.TupleSelect(hv_j); if (hv_dn == null) { hv_dn = new HTuple(); } hv_dn[hv_j] = hv_jh; hv_jh = hv_dnr.TupleSelect(hv_i); if (hv_dnr == null) { hv_dnr = new HTuple(); } hv_dnr[hv_i] = hv_dnr.TupleSelect(hv_j); if (hv_dnr == null) { hv_dnr = new HTuple(); } hv_dnr[hv_j] = hv_jh; hv_jh = hv_dnc.TupleSelect(hv_i); if (hv_dnc == null) { hv_dnc = new HTuple(); } hv_dnc[hv_i] = hv_dnc.TupleSelect(hv_j); if (hv_dnc == null) { hv_dnc = new HTuple(); } hv_dnc[hv_j] = hv_jh; } } //catch (Exception) //endtry } //t2 = DateTime.Now; hv_djd = new HTuple(); hv_djdr = new HTuple(); hv_djdc = new HTuple(); hv_djx = new HTuple(); hv_djxr = new HTuple(); hv_djxc = new HTuple(); hv_xjd = new HTuple(); hv_xjdr = new HTuple(); hv_xjdc = new HTuple(); hv_xjx = new HTuple(); hv_xjxr = new HTuple(); hv_xjxc = new HTuple(); hv_ljd = new HTuple(); hv_ljx = new HTuple(); HTuple end_val107 = hv_Length - 1; HTuple step_val107 = 1; for (hv_i = 0; hv_i.Continue(end_val107, step_val107); hv_i = hv_i.TupleAdd(step_val107)) { if ((int)(new HTuple(((hv_dx.TupleSelect(hv_i))).TupleGreater(hv_zj))) != 0) { for (hv_j = -3; (int)hv_j <= 3; hv_j = (int)hv_j + 1) { if (((hv_i + hv_j) >= 0) && ((hv_i + hv_j) <= end_val107)) { if ((int)((new HTuple(((hv_disdnpx.TupleSelect(hv_i + hv_j))).TupleGreater( hv_disdxpx.TupleSelect(hv_i)))).TupleAnd(new HTuple(((hv_dn.TupleSelect( hv_i + hv_j))).TupleLess(hv_zj)))) != 0) { hv_ljd = hv_ljd.TupleConcat(hv_disdxpx.TupleSelect(hv_i)); hv_djd = hv_djd.TupleConcat(hv_dx.TupleSelect(hv_i)); hv_djdr = hv_djdr.TupleConcat(hv_dxr.TupleSelect(hv_i)); hv_djdc = hv_djdc.TupleConcat(hv_dxc.TupleSelect(hv_i)); hv_djx = hv_djx.TupleConcat(hv_dn.TupleSelect(hv_i + hv_j)); hv_djxr = hv_djxr.TupleConcat(hv_dnr.TupleSelect(hv_i + hv_j)); hv_djxc = hv_djxc.TupleConcat(hv_dnc.TupleSelect(hv_i + hv_j)); break; } } // catch (Exception) //catch (HalconException) //{ // //HDevExpDefaultException1.ToHTuple(out hv_Exception); //} } } if ((int)(new HTuple(((hv_dn.TupleSelect(hv_i))).TupleGreater(hv_zj))) != 0) { for (hv_j = -3; (int)hv_j <= 3; hv_j = (int)hv_j + 1) { //try if (((hv_i + hv_j) >= 0) && ((hv_i + hv_j) <= end_val107)) { if ((int)((new HTuple(((hv_disdxpx.TupleSelect(hv_i + hv_j))).TupleGreater( hv_disdnpx.TupleSelect(hv_i)))).TupleAnd(new HTuple(((hv_dx.TupleSelect( hv_i + hv_j))).TupleLess(hv_zj)))) != 0) { hv_ljx = hv_ljx.TupleConcat(hv_disdnpx.TupleSelect(hv_i)); hv_xjd = hv_xjd.TupleConcat(hv_dn.TupleSelect(hv_i)); hv_xjdr = hv_xjdr.TupleConcat(hv_dnr.TupleSelect(hv_i)); hv_xjdc = hv_xjdc.TupleConcat(hv_dnc.TupleSelect(hv_i)); hv_xjx = hv_xjx.TupleConcat(hv_dx.TupleSelect(hv_i + hv_j)); hv_xjxr = hv_xjxr.TupleConcat(hv_dxr.TupleSelect(hv_i + hv_j)); hv_xjxc = hv_xjxc.TupleConcat(hv_dxc.TupleSelect(hv_i + hv_j)); break; } } // catch (Exception) //catch (HalconException) //{ // //HDevExpDefaultException1.ToHTuple(out hv_Exception); //} } } } HOperatorSet.TupleLength(hv_ljd, out hv_l1); HTuple end_val153 = 1; HTuple step_val153 = -1; for (hv_i = hv_l1 - 1; hv_i.Continue(end_val153, step_val153); hv_i = hv_i.TupleAdd(step_val153)) { if ((int)(new HTuple(((hv_ljd.TupleSelect(hv_i))).TupleEqual(hv_ljd.TupleSelect( hv_i - 1)))) != 0) { HOperatorSet.TupleRemove(hv_ljd, hv_i, out hv_ljd); HOperatorSet.TupleRemove(hv_djd, hv_i, out hv_djd); HOperatorSet.TupleRemove(hv_djdr, hv_i, out hv_djdr); HOperatorSet.TupleRemove(hv_djdc, hv_i, out hv_djdc); HOperatorSet.TupleRemove(hv_djx, hv_i, out hv_djx); HOperatorSet.TupleRemove(hv_djxr, hv_i, out hv_djxr); HOperatorSet.TupleRemove(hv_djxc, hv_i, out hv_djxc); } } HOperatorSet.TupleLength(hv_ljx, out hv_l2); HTuple end_val165 = 1; HTuple step_val165 = -1; for (hv_i = hv_l2 - 1; hv_i.Continue(end_val165, step_val165); hv_i = hv_i.TupleAdd(step_val165)) { if ((int)(new HTuple(((hv_ljd.TupleSelect(hv_i))).TupleEqual(hv_ljd.TupleSelect( hv_i - 1)))) != 0) { HOperatorSet.TupleRemove(hv_ljx, hv_i, out hv_ljx); HOperatorSet.TupleRemove(hv_xjd, hv_i, out hv_xjd); HOperatorSet.TupleRemove(hv_xjdr, hv_i, out hv_xjdr); HOperatorSet.TupleRemove(hv_xjdc, hv_i, out hv_xjdc); HOperatorSet.TupleRemove(hv_xjx, hv_i, out hv_xjx); HOperatorSet.TupleRemove(hv_xjxr, hv_i, out hv_xjxr); HOperatorSet.TupleRemove(hv_xjxc, hv_i, out hv_xjxc); } } HOperatorSet.TupleLength(hv_ljx, out hv_l2); HOperatorSet.TupleLength(hv_ljd, out hv_l1); //t5 = DateTime.Now; hv_dj = hv_djd - hv_djx; hv_xj = hv_xjd - hv_xjx; hv_djx = hv_dj.TupleMax(); hv_djn = hv_dj.TupleMin(); hv_djm = hv_dj.TupleMean(); hv_xjx = hv_xj.TupleMax(); hv_xjn = hv_xj.TupleMin(); hv_xjm = hv_xj.TupleMean(); hv_djzd = (hv_djd.TupleMax()) - (hv_djx.TupleMin()); hv_djzx = (hv_djd.TupleMin()) - (hv_djx.TupleMax()); hv_xjzd = (hv_xjd.TupleMax()) - (hv_xjx.TupleMin()); hv_xjzx = (hv_xjd.TupleMin()) - (hv_xjx.TupleMax()); hv_ljtd = (hv_ljd.TupleConcat(0)) - ((new HTuple(0)).TupleConcat(hv_ljd)); hv_ljtx = (hv_ljx.TupleConcat(0)) - ((new HTuple(0)).TupleConcat(hv_ljx)); HOperatorSet.TupleLength(hv_ljtd, out hv_Length3); HOperatorSet.TupleLength(hv_ljtx, out hv_Length4); hv_ljtd = hv_ljtd.TupleSelectRange(1, hv_Length3 - 2); hv_ljtx = hv_ljtx.TupleSelectRange(1, hv_Length4 - 2); hv_ljdx = hv_ljtd.TupleMax(); hv_ljdn = hv_ljtd.TupleMin(); hv_ljxx = hv_ljtx.TupleMax(); hv_ljxn = hv_ljtx.TupleMin(); hv_ljdm = hv_ljtd.TupleMean(); hv_ljxm = hv_ljtx.TupleMean(); HOperatorSet.TupleAtan((hv_djxr - hv_djdr) / (hv_djdc - hv_djxc), out hv_djiaodu); HOperatorSet.TupleAtan((hv_xjxr - hv_xjdr) / (hv_xjdc - hv_xjxc), out hv_xjiaodu); HOperatorSet.TupleLength(hv_djiaodu, out hv_Length5); HOperatorSet.TupleLength(hv_xjiaodu, out hv_Length6); hv_djiaodu1 = ((hv_djiaodu - hv_zcj)).TupleAbs(); hv_xjiaodu1 = ((hv_xjiaodu - hv_zcj)).TupleAbs(); HTuple end_val177 = hv_Length5 - 1; HTuple step_val177 = 1; //t6 = DateTime.Now; for (hv_i = 0; hv_i.Continue(end_val177, step_val177); hv_i = hv_i.TupleAdd(step_val177)) { while ((int)(new HTuple(hv_djiaodu1.TupleGreater((new HTuple(90)).TupleRad() ))) != 0) { hv_djiaodu1 = hv_djiaodu1 - ((new HTuple(90)).TupleRad()); } if ((int)(new HTuple(hv_djiaodu1.TupleGreater((new HTuple(45)).TupleRad() ))) != 0) { hv_djiaodu1 = (-hv_djiaodu1) + ((new HTuple(90)).TupleRad()); } } HTuple end_val185 = hv_Length6 - 1; HTuple step_val185 = 1; for (hv_i = 0; hv_i.Continue(end_val185, step_val185); hv_i = hv_i.TupleAdd(step_val185)) { while ((int)(new HTuple(hv_xjiaodu1.TupleGreater((new HTuple(90)).TupleRad() ))) != 0) { hv_xjiaodu1 = hv_xjiaodu1 - ((new HTuple(90)).TupleRad()); } if ((int)(new HTuple(hv_xjiaodu1.TupleGreater((new HTuple(45)).TupleRad() ))) != 0) { hv_xjiaodu1 = (-hv_xjiaodu1) + ((new HTuple(90)).TupleRad()); } } hv_jjdd = hv_djiaodu1.TupleMax().TupleDeg(); hv_jjdx = hv_djiaodu1.TupleMin().TupleDeg(); hv_jjxd = hv_xjiaodu1.TupleMax().TupleDeg(); hv_jjxx = hv_xjiaodu1.TupleMin().TupleDeg(); hv_jjdm = hv_djiaodu1.TupleMean().TupleDeg(); hv_jjxm = hv_xjiaodu1.TupleMean().TupleDeg(); ho_RegionLines.Dispose(); HOperatorSet.GenRegionLine(out ho_RegionLines, hv_djdr, hv_djdc, hv_djxr, hv_djxc); HOperatorSet.Union2(ho_RegionLines, ho_RegionLines, out RegionToDisp); HTuple hv_result = GetHv_result(); hv_result = hv_result.TupleConcat("螺纹条数1"); hv_result = hv_result.TupleConcat(hv_l1); hv_result = hv_result.TupleConcat("螺纹条数2"); hv_result = hv_result.TupleConcat(hv_l2); hv_result = hv_result.TupleConcat("大径最大值"); hv_result = hv_result.TupleConcat(hv_djx.D * pixeldist); hv_result = hv_result.TupleConcat("大径最小值"); hv_result = hv_result.TupleConcat(hv_djn.D * pixeldist); hv_result = hv_result.TupleConcat("大径平均值"); hv_result = hv_result.TupleConcat(hv_djm.D * pixeldist); hv_result = hv_result.TupleConcat("小径最大值"); hv_result = hv_result.TupleConcat(hv_xjx.D * pixeldist); hv_result = hv_result.TupleConcat("小径最小值"); hv_result = hv_result.TupleConcat(hv_xjn.D * pixeldist); hv_result = hv_result.TupleConcat("小径平均值"); hv_result = hv_result.TupleConcat(hv_xjm.D * pixeldist); hv_result = hv_result.TupleConcat("整体大径最大值"); hv_result = hv_result.TupleConcat(hv_djzd.D * pixeldist); hv_result = hv_result.TupleConcat("整体大径最小值"); hv_result = hv_result.TupleConcat(hv_djzx.D * pixeldist); hv_result = hv_result.TupleConcat("整体小径最大值"); hv_result = hv_result.TupleConcat(hv_xjzd.D * pixeldist); hv_result = hv_result.TupleConcat("整体小径最小值"); hv_result = hv_result.TupleConcat(hv_xjzx.D * pixeldist); hv_result = hv_result.TupleConcat("螺距最大值"); hv_result = hv_result.TupleConcat(hv_ljdx.D * pixeldist); hv_result = hv_result.TupleConcat("螺距最小值"); hv_result = hv_result.TupleConcat(hv_ljdn.D * pixeldist); hv_result = hv_result.TupleConcat("螺距平均值"); hv_result = hv_result.TupleConcat(hv_ljdm.D * pixeldist); hv_result = hv_result.TupleConcat("角度最大值"); hv_result = hv_result.TupleConcat(hv_jjdd.D); hv_result = hv_result.TupleConcat("角度最大值"); hv_result = hv_result.TupleConcat(hv_jjdx.D); hv_result = hv_result.TupleConcat("角度平均值"); hv_result = hv_result.TupleConcat(hv_jjdm.D); result = hv_result.Clone(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Border.Dispose(); ho_Circle.Dispose(); ho_ImageReduced2.Dispose(); ho_Region.Dispose(); ho_Rectangle1.Dispose(); ho_ImageReduced1.Dispose(); ho_Border1.Dispose(); ho_SelectedContours.Dispose(); imagexz.Dispose(); retxz.Dispose(); ho_RegionLines.Dispose(); algorithm.Region.Dispose(); //t4 = DateTime.Now; } catch { HTuple hv_result = GetHv_result(); hv_result = hv_result.TupleConcat("螺纹条数1"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("螺纹条数2"); 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(0); 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(0); 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(0); 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(0); 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(0); hv_result = hv_result.TupleConcat("角度最大值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("角度平均值"); hv_result = hv_result.TupleConcat(0); result = hv_result.Clone(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Border.Dispose(); ho_Circle.Dispose(); ho_ImageReduced2.Dispose(); ho_Region.Dispose(); ho_Rectangle1.Dispose(); ho_ImageReduced1.Dispose(); ho_Border1.Dispose(); ho_SelectedContours.Dispose(); imagexz.Dispose(); retxz.Dispose(); ho_RegionLines.Dispose(); algorithm.Region.Dispose(); } }
// Procedures public void GetRegionByBinaryThresholdRect(HObject ho_Image, out HObject ho_FoundRegion, HTuple hv_MeanMaskWidth, HTuple hv_MeanMaskHeight, HTuple hv_ScaleAdd, HTuple hv_LightDark, HTuple hv_AreaMin, HTuple hv_AreaMax, HTuple hv_OpeningWidth, HTuple hv_OpeningHeight, HTuple hv_ClosingWidth, HTuple hv_ClosingHeight, HTuple hv_DilationRadius) { // Stack for temporary objects HObject[] OTemp = new HObject[20]; // Local iconic variables HObject ho_Domain, ho_ImageMean, ho_ImageScaled = null; HObject ho_Regions, ho_ConnectedRegions, ho_SelectedRegions; HObject ho_RegionFillUp, ho_RegionUnion; // Local control variables HTuple hv_Value = null, hv_UsedThreshold = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_FoundRegion); HOperatorSet.GenEmptyObj(out ho_Domain); HOperatorSet.GenEmptyObj(out ho_ImageMean); HOperatorSet.GenEmptyObj(out ho_ImageScaled); HOperatorSet.GenEmptyObj(out ho_Regions); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_RegionFillUp); HOperatorSet.GenEmptyObj(out ho_RegionUnion); ho_Domain.Dispose(); HOperatorSet.GetDomain(ho_Image, out ho_Domain); ho_ImageMean.Dispose(); HOperatorSet.MeanImage(ho_Image, out ho_ImageMean, hv_MeanMaskWidth, hv_MeanMaskHeight); HOperatorSet.GrayFeatures(ho_Domain, ho_ImageMean, "mean", out hv_Value); if ((int)(new HTuple(hv_LightDark.TupleEqual("light"))) != 0) { ho_ImageScaled.Dispose(); HOperatorSet.ScaleImage(ho_ImageMean, out ho_ImageScaled, 1, (255 - hv_Value) + hv_ScaleAdd); } else if ((int)(new HTuple(hv_LightDark.TupleEqual("dark"))) != 0) { ho_ImageScaled.Dispose(); HOperatorSet.ScaleImage(ho_ImageMean, out ho_ImageScaled, 1, (-hv_Value) + hv_ScaleAdd); } ho_Regions.Dispose(); HOperatorSet.BinaryThreshold(ho_ImageScaled, out ho_Regions, "max_separability", hv_LightDark, out hv_UsedThreshold); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_Regions, out ho_ConnectedRegions); ho_SelectedRegions.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area", "and", hv_AreaMin, hv_AreaMax); ho_RegionFillUp.Dispose(); HOperatorSet.FillUp(ho_SelectedRegions, out ho_RegionFillUp); if ((int)((new HTuple(hv_OpeningWidth.TupleGreater(0))).TupleAnd(new HTuple(hv_OpeningHeight.TupleGreater( 0)))) != 0) { { HObject ExpTmpOutVar_0; HOperatorSet.OpeningRectangle1(ho_RegionFillUp, out ExpTmpOutVar_0, hv_OpeningWidth, hv_OpeningHeight); ho_RegionFillUp.Dispose(); ho_RegionFillUp = ExpTmpOutVar_0; } } if ((int)((new HTuple(hv_ClosingWidth.TupleGreater(0))).TupleAnd(new HTuple(hv_ClosingHeight.TupleGreater( 0)))) != 0) { { HObject ExpTmpOutVar_0; HOperatorSet.ClosingRectangle1(ho_RegionFillUp, out ExpTmpOutVar_0, hv_ClosingWidth, hv_ClosingHeight); ho_RegionFillUp.Dispose(); ho_RegionFillUp = ExpTmpOutVar_0; } } if ((int)(new HTuple(hv_DilationRadius.TupleGreater(0))) != 0) { { HObject ExpTmpOutVar_0; HOperatorSet.DilationCircle(ho_RegionFillUp, out ExpTmpOutVar_0, hv_DilationRadius); ho_RegionFillUp.Dispose(); ho_RegionFillUp = ExpTmpOutVar_0; } } ho_RegionUnion.Dispose(); HOperatorSet.Union1(ho_RegionFillUp, out ho_RegionUnion); ho_FoundRegion.Dispose(); HOperatorSet.MoveRegion(ho_RegionUnion, out ho_FoundRegion, 0, 0); ho_Domain.Dispose(); ho_ImageMean.Dispose(); ho_ImageScaled.Dispose(); ho_Regions.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_RegionFillUp.Dispose(); ho_RegionUnion.Dispose(); return; }
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); } }
private void action() { // Stack for temporary objects HObject[] OTemp = new HObject[20]; // Local iconic variables HObject ho_Rectangle, ho_ImageReduced; HObject ho_Region1, ho_Region, ho_Contours, ho_SelectedContours; HObject ho_SmoothedContours1, ho_ClippedContoursu, ho_ClippedContoursd; HObject ho_RegionLinesa, ho_RegionLinesb, ho_RegionLinesc; HObject ho_RegionLinesd, ho_RegionUniona, ho_RegionUnionb; HObject ho_RegionUnionc, ho_RegionLines1 = null; // Local control variables HTuple hv_Width = null, hv_Height = null, hv_Row1m = null; HTuple hv_Col1m = null, hv_Row2m = null, hv_Col2m = null; HTuple hv_Row = null, hv_Column = null, hv_Phi = null; HTuple hv_Length1 = null, hv_Length2 = null, hv_Rown = null; HTuple hv_Rowu = null, hv_Columnu = null, hv_Phiu = null; HTuple hv_Length1u = null, hv_Length2u = null, hv_MeasureHandleu = null; HTuple hv_GrayValuesu = null, hv_Functionu = null, hv_SmoothedFunctionu = null; HTuple hv_FirstDerivativeu = null, hv_ZeroCrossingsu = null; HTuple hv_RowStartu = null, hv_ColStartu = null, hv_RowEndu = null; HTuple hv_ColEndu = null, hv_iu = null, hv_Rowd = null; HTuple hv_Columnd = null, hv_Phid = null, hv_Length1d = null; HTuple hv_Length2d = null, hv_MeasureHandled = null, hv_GrayValuesd = null; HTuple hv_Functiond = null, hv_SmoothedFunctiond = null; HTuple hv_FirstDerivatived = null, hv_ZeroCrossingsd = null; HTuple hv_RowStartd = null, hv_ColStartd = null, hv_RowEndd = null; HTuple hv_ColEndd = null, hv_id = null, hv_p = null, hv_Length3 = null; HTuple hv_Cos = null, hv_Sin = null, hv_RT_X = null, hv_RT_Y = null; HTuple hv_AX = null, hv_AY = null, hv_RB_X = null, hv_RB_Y = null; HTuple hv_BX = null, hv_BY = null, hv_LB_X = null, hv_LB_Y = null; HTuple hv_CX = null, hv_CY = null, hv_LT_X = null, hv_LT_Y = null; HTuple hv_DX = null, hv_DY = null, hv_RowIntus = null; HTuple hv_ColumnIntus = null, hv_RowIntuAll = null, hv_ColumnIntuAll = null; HTuple hv_RowIntud = null, hv_ColumnIntud = null, hv_i = null; HTuple hv_RowIntu = new HTuple(), hv_ColumnIntu = new HTuple(); HTuple hv_IsOverlappingu = new HTuple(), hv_Distance1 = new HTuple(); HTuple hv_RowIntdAll = null, hv_ColumnIntdAll = null, hv_RowIntds = null; HTuple hv_ColumnIntds = null, hv_RowIntdd = null, hv_ColumnIntdd = null; HTuple hv_j = null, hv_RowIntd = new HTuple(), hv_ColumnIntd = new HTuple(); HTuple hv_IsOverlappingd = new HTuple(), hv_Distance2 = new HTuple(); HTuple hv_i1 = null, hv_j1 = null, hv_Deg1All = null, hv_Distance3All = null; HTuple hv_Min2 = null, hv_k = null, hv_Angle = new HTuple(); HTuple hv_Deg1 = new HTuple(), hv_Distance3 = new HTuple(); HTuple hv_Sorted1 = null, hv_Mean1 = null, hv_dajingmin = null; HTuple hv_dajingmax = null, hv_Distance5 = null, hv_Distance4 = null; HTuple hv_Distance6 = null, hv_yaju = null, hv_Sorted2 = null; HTuple hv_Mean2 = null, hv_Deg1min = null, hv_Deg1max = null; HTuple hv_i2 = null, hv_j2 = null, hv_Distance7All = null; HTuple hv_Min3 = null, hv_ki = null, hv_Distance7 = new HTuple(); HTuple hv_Sorted3 = null, hv_Mean3 = null, hv_xiaojingmin = null; HTuple hv_xiaojingmax = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_Region1); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_Contours); HOperatorSet.GenEmptyObj(out ho_SelectedContours); HOperatorSet.GenEmptyObj(out ho_SmoothedContours1); HOperatorSet.GenEmptyObj(out ho_ClippedContoursu); HOperatorSet.GenEmptyObj(out ho_ClippedContoursd); HOperatorSet.GenEmptyObj(out ho_RegionLinesa); HOperatorSet.GenEmptyObj(out ho_RegionLinesb); HOperatorSet.GenEmptyObj(out ho_RegionLinesc); HOperatorSet.GenEmptyObj(out ho_RegionLinesd); HOperatorSet.GenEmptyObj(out ho_RegionUniona); HOperatorSet.GenEmptyObj(out ho_RegionUnionb); HOperatorSet.GenEmptyObj(out ho_RegionUnionc); 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_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(Image, ho_Rectangle, out ho_ImageReduced); ho_Region1.Dispose(); HOperatorSet.Threshold(ho_ImageReduced, out ho_Region1, 0, Dthv); HOperatorSet.SmallestRectangle2(ho_Region1, out hv_Row, out hv_Column, out hv_Phi, out hv_Length1, out hv_Length2); //HOperatorSet.TupleDeg(hv_Phi, out hv_Deg); //*rotate_image (Image, Image1, -Deg, 'constant') //** ho_Region.Dispose(); HOperatorSet.Threshold(Image, out ho_Region, 0, Dthv); ho_Contours.Dispose(); HOperatorSet.GenContourRegionXld(ho_Region, out ho_Contours, "border"); ho_SelectedContours.Dispose(); HOperatorSet.SelectContoursXld(ho_Contours, out ho_SelectedContours, "contour_length", 20, 200000, -1, 1); ho_SmoothedContours1.Dispose(); HOperatorSet.SmoothContoursXld(ho_SelectedContours, out ho_SmoothedContours1, 11); hv_Rown = (DRow1m + DRow2m) / 2; //*gen_rectangle1(Rectangle2, Row1m, Col1m, Rown, Col2m) //*gen_rectangle1(Rectangle3, Rown, Col1m, Row2m, Col2m) ho_ClippedContoursu.Dispose(); HOperatorSet.ClipContoursXld(ho_SmoothedContours1, out ho_ClippedContoursu, DRow1m, DCol1m, hv_Rown, DCol2m); ho_ClippedContoursd.Dispose(); HOperatorSet.ClipContoursXld(ho_SmoothedContours1, out ho_ClippedContoursd, hv_Rown, DCol1m, DRow2m, DCol2m); hv_Rowu = (DRow1m + hv_Rown) / 2; hv_Columnu = (DCol1m + DCol2m) / 2; hv_Phiu = 0; hv_Length1u = (DCol2m - DCol1m) / 2; hv_Length2u = (hv_Rown - DRow1m) / 2; HOperatorSet.GenMeasureRectangle2(hv_Rowu, hv_Columnu, hv_Phiu, hv_Length1u, hv_Length2u, hv_Width, hv_Height, "bilinear", out hv_MeasureHandleu); HOperatorSet.MeasureProjection(Image, hv_MeasureHandleu, out hv_GrayValuesu); //hv_Sigmau = 5; HOperatorSet.CreateFunct1dArray(hv_GrayValuesu, out hv_Functionu); HOperatorSet.SmoothFunct1dGauss(hv_Functionu, 5, out hv_SmoothedFunctionu); HOperatorSet.DerivateFunct1d(hv_SmoothedFunctionu, "first", out hv_FirstDerivativeu); HOperatorSet.ZeroCrossingsFunct1d(hv_FirstDerivativeu, out hv_ZeroCrossingsu); //*disp_cross(3600, Rowu, Columnu, 6, Phiu)//在一个窗口中显示交叉 hv_RowStartu = hv_Rowu - (hv_Length2u / 2); hv_ColStartu = (hv_Columnu - hv_Length1u) + hv_ZeroCrossingsu; hv_RowEndu = hv_Rowu + (hv_Length2u / 2); hv_ColEndu = (hv_Columnu - hv_Length1u) + hv_ZeroCrossingsu; hv_iu = new HTuple(hv_ZeroCrossingsu.TupleLength()); hv_Rowd = (hv_Rown + DRow2m) / 2; hv_Columnd = (DCol1m + DCol2m) / 2; hv_Phid = 0; hv_Length1d = (DCol2m - DCol1m) / 2; hv_Length2d = (DRow2m - hv_Rown) / 2; HOperatorSet.GenMeasureRectangle2(hv_Rowd, hv_Columnd, hv_Phid, hv_Length1d, hv_Length2d, hv_Width, hv_Height, "bilinear", out hv_MeasureHandled); HOperatorSet.MeasureProjection(Image, hv_MeasureHandled, out hv_GrayValuesd); //hv_Sigmad = 5; HOperatorSet.CreateFunct1dArray(hv_GrayValuesd, out hv_Functiond); HOperatorSet.SmoothFunct1dGauss(hv_Functiond, 5, out hv_SmoothedFunctiond); HOperatorSet.DerivateFunct1d(hv_SmoothedFunctiond, "first", out hv_FirstDerivatived); HOperatorSet.ZeroCrossingsFunct1d(hv_FirstDerivatived, out hv_ZeroCrossingsd); //*stop() // //*disp_cross(3600, Rowd, Columnd, 6, Phid)//在一个窗口中显示交叉 hv_RowStartd = hv_Rowd - (hv_Length2d / 2); hv_ColStartd = (hv_Columnd - hv_Length1d) + hv_ZeroCrossingsd; hv_RowEndd = hv_Rowd + (hv_Length2d / 2); hv_ColEndd = (hv_Columnd - hv_Length1d) + hv_ZeroCrossingsd; //*dev_clear_window() hv_id = new HTuple(hv_ZeroCrossingsd.TupleLength()); //**螺距 hv_p = hv_Length1 / hv_id; hv_Length3 = hv_Length2 - (hv_p * 1.5); //*gen_rectangle2(Rectanglezj,Row, Column, Phi, Length1, Length3) HOperatorSet.TupleCos(hv_Phi, out hv_Cos); HOperatorSet.TupleSin(hv_Phi, out hv_Sin); //*dev_set_color('green') hv_RT_X = ((-hv_Length1) * hv_Cos) - (hv_Length3 * hv_Sin); hv_RT_Y = ((-hv_Length1) * hv_Sin) + (hv_Length3 * hv_Cos); //*gen_circle (Circle, Row-RT_Y, Column+RT_X, 10) //*最小矩形的顶点A hv_AX = ((-hv_Length1) * hv_Cos) - (hv_Length2 * hv_Sin); hv_AY = ((-hv_Length1) * hv_Sin) + (hv_Length2 * hv_Cos); //*dev_set_color('red') hv_RB_X = (hv_Length1 * hv_Cos) - (hv_Length3 * hv_Sin); hv_RB_Y = (hv_Length1 * hv_Sin) + (hv_Length3 * hv_Cos); //*gen_circle (Circle, Row-RB_Y, Column+RB_X, 10) //*最小矩形的顶点B hv_BX = (hv_Length1 * hv_Cos) - (hv_Length2 * hv_Sin); hv_BY = (hv_Length1 * hv_Sin) + (hv_Length2 * hv_Cos); //*dev_set_color('yellow') hv_LB_X = (hv_Length1 * hv_Cos) + (hv_Length3 * hv_Sin); hv_LB_Y = (hv_Length1 * hv_Sin) - (hv_Length3 * hv_Cos); //*gen_circle (Circle, Row-LB_Y, Column+LB_X, 10) //*最小矩形的顶点C hv_CX = (hv_Length1 * hv_Cos) + (hv_Length2 * hv_Sin); hv_CY = (hv_Length1 * hv_Sin) - (hv_Length2 * hv_Cos); //*dev_set_color('pink') hv_LT_X = ((-hv_Length1) * hv_Cos) + (hv_Length3 * hv_Sin); hv_LT_Y = ((-hv_Length1) * hv_Sin) - (hv_Length3 * hv_Cos); //*gen_circle (Circle, Row-LT_Y, Column+LT_X, 10) //*最小矩形的顶点D hv_DX = ((-hv_Length1) * hv_Cos) + (hv_Length2 * hv_Sin); hv_DY = ((-hv_Length1) * hv_Sin) - (hv_Length2 * hv_Cos); //*disp_line(3600, (Row-RT_Y+Row-LT_Y)/2, (Column+RT_X+Column+LT_X)/2, Row, Column) ho_RegionLinesa.Dispose(); HOperatorSet.GenRegionLine(out ho_RegionLinesa, hv_Row - hv_AY, hv_Column + hv_AX, hv_Row - hv_BY, hv_Column + hv_BX); ho_RegionLinesb.Dispose(); HOperatorSet.GenRegionLine(out ho_RegionLinesb, hv_Row - hv_AY, hv_Column + hv_AX, hv_Row - hv_DY, hv_Column + hv_DX); ho_RegionLinesc.Dispose(); HOperatorSet.GenRegionLine(out ho_RegionLinesc, hv_Row - hv_BY, hv_Column + hv_BX, hv_Row - hv_CY, hv_Column + hv_CX); ho_RegionLinesd.Dispose(); HOperatorSet.GenRegionLine(out ho_RegionLinesd, hv_Row - hv_CY, hv_Column + hv_CX, hv_Row - hv_DY, hv_Column + hv_DX); ho_RegionUniona.Dispose(); HOperatorSet.Union2(ho_RegionLinesa, ho_RegionLinesb, out ho_RegionUniona); ho_RegionUnionb.Dispose(); HOperatorSet.Union2(ho_RegionLinesc, ho_RegionUniona, out ho_RegionUnionb); ho_RegionUnionc.Dispose(); HOperatorSet.Union2(ho_RegionLinesd, ho_RegionUnionb, out ho_RegionUnionc); //*dev_set_line_width (2) //*dev_set_color ('green') hv_RowIntus = new HTuple(); hv_ColumnIntus = new HTuple(); hv_RowIntuAll = new HTuple(); hv_ColumnIntuAll = new HTuple(); hv_RowIntud = new HTuple(); hv_ColumnIntud = new HTuple(); //hv_u1 = 0; HTuple end_val128 = hv_iu - 1; HTuple step_val128 = 1; for (hv_i = 0; hv_i.Continue(end_val128, step_val128); hv_i = hv_i.TupleAdd(step_val128)) { HOperatorSet.IntersectionLineContourXld(ho_ClippedContoursu, hv_RowStartu, hv_ColStartu.TupleSelect(hv_i), hv_RowEndu, hv_ColEndu.TupleSelect(hv_i), out hv_RowIntu, out hv_ColumnIntu, out hv_IsOverlappingu); //ho_Cross1u.Dispose(); //HOperatorSet.GenCrossContourXld(out ho_Cross1u, hv_RowIntu, hv_ColumnIntu, // 12, 0); hv_RowIntuAll = hv_RowIntuAll.TupleConcat(hv_RowIntu); hv_ColumnIntuAll = hv_ColumnIntuAll.TupleConcat(hv_ColumnIntu); HOperatorSet.DistancePl(hv_RowIntu, hv_ColumnIntu, (((hv_Row - hv_RT_Y) + hv_Row) - hv_LT_Y) / 2, (((hv_Column + hv_RT_X) + hv_Column) + hv_LT_X) / 2, hv_Row, hv_Column, out hv_Distance1); if ((int)(new HTuple(hv_Distance1.TupleGreater(hv_Length3))) != 0) { hv_RowIntus = hv_RowIntus.TupleConcat(hv_RowIntu); hv_ColumnIntus = hv_ColumnIntus.TupleConcat(hv_ColumnIntu); } if ((int)(new HTuple(hv_Distance1.TupleLessEqual(hv_Length3))) != 0) { hv_RowIntud = hv_RowIntud.TupleConcat(hv_RowIntu); hv_ColumnIntud = hv_ColumnIntud.TupleConcat(hv_ColumnIntu); } //hv_u1 = hv_u1 + 1; } //*dev_display (Cross1u) hv_RowIntdAll = new HTuple(); hv_ColumnIntdAll = new HTuple(); hv_RowIntds = new HTuple(); hv_ColumnIntds = new HTuple(); hv_RowIntdd = new HTuple(); hv_ColumnIntdd = new HTuple(); //hv_d1 = 0; HTuple end_val153 = hv_id - 1; HTuple step_val153 = 1; for (hv_j = 0; hv_j.Continue(end_val153, step_val153); hv_j = hv_j.TupleAdd(step_val153)) { HOperatorSet.IntersectionLineContourXld(ho_ClippedContoursd, hv_RowStartd, hv_ColStartd.TupleSelect(hv_j), hv_RowEndd, hv_ColEndd.TupleSelect(hv_j), out hv_RowIntd, out hv_ColumnIntd, out hv_IsOverlappingd); //ho_Cross1d.Dispose(); //HOperatorSet.GenCrossContourXld(out ho_Cross1d, hv_RowIntd, hv_ColumnIntd, // 12, 0); hv_RowIntdAll = hv_RowIntdAll.TupleConcat(hv_RowIntd); hv_ColumnIntdAll = hv_ColumnIntdAll.TupleConcat(hv_ColumnIntd); HOperatorSet.DistancePl(hv_RowIntd, hv_ColumnIntd, (((hv_Row - hv_RT_Y) + hv_Row) - hv_LT_Y) / 2, (((hv_Column + hv_RT_X) + hv_Column) + hv_LT_X) / 2, hv_Row, hv_Column, out hv_Distance2); if ((int)(new HTuple(hv_Distance2.TupleGreater(hv_Length3))) != 0) { hv_RowIntds = hv_RowIntds.TupleConcat(hv_RowIntd); hv_ColumnIntds = hv_ColumnIntds.TupleConcat(hv_ColumnIntd); } if ((int)(new HTuple(hv_Distance2.TupleLessEqual(hv_Length3))) != 0) { hv_RowIntdd = hv_RowIntdd.TupleConcat(hv_RowIntd); hv_ColumnIntdd = hv_ColumnIntdd.TupleConcat(hv_ColumnIntd); } //hv_d1 = hv_d1 + 1; } hv_i1 = new HTuple(hv_ColumnIntus.TupleLength()); hv_j1 = new HTuple(hv_ColumnIntds.TupleLength()); //hv_k1 = 0; hv_Deg1All = new HTuple(); hv_Distance3All = new HTuple(); HOperatorSet.TupleMin2(hv_i1, hv_j1, out hv_Min2); HTuple end_val175 = hv_Min2 - 1; HTuple step_val175 = 1; for (hv_k = 0; hv_k.Continue(end_val175, step_val175); hv_k = hv_k.TupleAdd(step_val175)) { ho_RegionLines1.Dispose(); HOperatorSet.GenRegionLine(out ho_RegionLines1, hv_RowIntus.TupleSelect(hv_k), hv_ColumnIntus.TupleSelect(hv_k), hv_RowIntds.TupleSelect(hv_k), hv_ColumnIntds.TupleSelect( hv_k)); { HObject ExpTmpOutVar_0; HOperatorSet.Union2(ho_RegionUnionc, ho_RegionLines1, out ExpTmpOutVar_0); ho_RegionUnionc.Dispose(); ho_RegionUnionc = ExpTmpOutVar_0; } HOperatorSet.AngleLl(hv_RowIntus.TupleSelect(hv_k), hv_ColumnIntus.TupleSelect( hv_k), hv_RowIntds.TupleSelect(hv_k), hv_ColumnIntds.TupleSelect(hv_k), (((hv_Row - hv_RT_Y) + hv_Row) - hv_LT_Y) / 2, (((hv_Column + hv_RT_X) + hv_Column) + hv_LT_X) / 2, hv_Row, hv_Column, out hv_Angle); HOperatorSet.TupleDeg(hv_Angle, out hv_Deg1); hv_Deg1All = hv_Deg1All.TupleConcat(hv_Deg1); if ((int)(new HTuple(hv_k.TupleGreater(0))) != 0) { HOperatorSet.DistancePl(hv_RowIntus.TupleSelect(hv_k), hv_ColumnIntus.TupleSelect( hv_k), hv_RowIntds.TupleSelect(hv_k), hv_ColumnIntds.TupleSelect(hv_k), hv_RowIntds.TupleSelect(hv_k - 1), hv_ColumnIntds.TupleSelect(hv_k - 1), out hv_Distance3); hv_Distance3All = hv_Distance3All.TupleConcat(hv_Distance3); } //hv_k1 = hv_k1 + 1; } HOperatorSet.TupleSort(hv_Distance3All, out hv_Sorted1); HOperatorSet.TupleMean(hv_Distance3All, out hv_Mean1); hv_dajingmin = hv_Sorted1[0]; hv_dajingmax = hv_Sorted1[(new HTuple(hv_Sorted1.TupleLength())) - 1]; HOperatorSet.DistancePp(hv_RowIntus.TupleSelect(0), hv_ColumnIntus.TupleSelect( 0), hv_RowIntus.TupleSelect(hv_Min2 - 1), hv_ColumnIntus.TupleSelect(hv_Min2 - 1), out hv_Distance5); HOperatorSet.DistancePp(hv_RowIntds.TupleSelect(0), hv_ColumnIntds.TupleSelect( 0), hv_RowIntds.TupleSelect(hv_Min2 - 1), hv_ColumnIntds.TupleSelect(hv_Min2 - 1), out hv_Distance4); hv_Distance6 = (hv_Distance5 + hv_Distance4) / 2; hv_yaju = hv_Distance6 / hv_Min2; HOperatorSet.TupleSort(hv_Deg1All, out hv_Sorted2); HOperatorSet.TupleMean(hv_Deg1All, out hv_Mean2); hv_Deg1min = hv_Sorted2[0]; hv_Deg1max = hv_Sorted2[(new HTuple(hv_Sorted2.TupleLength())) - 1]; //***小径 hv_i2 = new HTuple(hv_ColumnIntud.TupleLength()); hv_j2 = new HTuple(hv_ColumnIntdd.TupleLength()); //hv_k2 = 0; hv_Distance7All = new HTuple(); HOperatorSet.TupleMin2(hv_i2, hv_j2, out hv_Min3); HTuple end_val205 = hv_Min3 - 1; HTuple step_val205 = 1; for (hv_ki = 0; hv_ki.Continue(end_val205, step_val205); hv_ki = hv_ki.TupleAdd(step_val205)) { if ((int)(new HTuple(hv_ki.TupleGreater(0))) != 0) { HOperatorSet.DistancePl(hv_RowIntud.TupleSelect(hv_ki), hv_ColumnIntud.TupleSelect( hv_ki), hv_RowIntdd.TupleSelect(hv_ki), hv_ColumnIntdd.TupleSelect( hv_ki), hv_RowIntdd.TupleSelect(hv_ki - 1), hv_ColumnIntdd.TupleSelect( hv_ki - 1), out hv_Distance7); hv_Distance7All = hv_Distance7All.TupleConcat(hv_Distance7); } //hv_k2 = hv_k2 + 1; } HOperatorSet.TupleSort(hv_Distance7All, out hv_Sorted3); HOperatorSet.TupleMean(hv_Distance7All, out hv_Mean3); hv_xiaojingmin = hv_Sorted3[0]; hv_xiaojingmax = hv_Sorted3[(new HTuple(hv_Sorted3.TupleLength())) - 1]; HOperatorSet.Union1(ho_RegionUnionc, out RegionToDisp); HTuple hv_result = GetHv_result(); hv_result = hv_result.TupleConcat("齿数"); hv_result = hv_result.TupleConcat(hv_Min2.D); hv_result = hv_result.TupleConcat("螺纹长度"); hv_result = hv_result.TupleConcat(hv_Distance6.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹大径平均值"); hv_result = hv_result.TupleConcat(hv_Mean1.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹大径最小值"); hv_result = hv_result.TupleConcat(hv_dajingmin.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹大径最大值"); hv_result = hv_result.TupleConcat(hv_dajingmax.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹小径平均值"); hv_result = hv_result.TupleConcat(hv_Mean3.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹小径最小值"); hv_result = hv_result.TupleConcat(hv_xiaojingmin.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹小径最大值"); hv_result = hv_result.TupleConcat(hv_xiaojingmax.D * pixeldist); hv_result = hv_result.TupleConcat("牙距"); hv_result = hv_result.TupleConcat(hv_yaju.D * pixeldist); hv_result = hv_result.TupleConcat("平均牙倾角"); hv_result = hv_result.TupleConcat(hv_Mean2.D); hv_result = hv_result.TupleConcat("最小牙倾角"); hv_result = hv_result.TupleConcat(hv_Deg1min.D); hv_result = hv_result.TupleConcat("最大牙倾角"); hv_result = hv_result.TupleConcat(hv_Deg1max.D); result = hv_result.Clone(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Region1.Dispose(); ho_Region.Dispose(); ho_Contours.Dispose(); ho_SelectedContours.Dispose(); ho_SmoothedContours1.Dispose(); ho_ClippedContoursu.Dispose(); ho_ClippedContoursd.Dispose(); ho_RegionLinesa.Dispose(); ho_RegionLinesb.Dispose(); ho_RegionLinesc.Dispose(); ho_RegionLinesd.Dispose(); ho_RegionUniona.Dispose(); ho_RegionUnionb.Dispose(); ho_RegionUnionc.Dispose(); ho_RegionLines1.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(0); 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(0); 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(0); 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(0); result = hv_result.Clone(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Region1.Dispose(); ho_Region.Dispose(); ho_Contours.Dispose(); ho_SelectedContours.Dispose(); ho_SmoothedContours1.Dispose(); ho_ClippedContoursu.Dispose(); ho_ClippedContoursd.Dispose(); ho_RegionLinesa.Dispose(); ho_RegionLinesb.Dispose(); ho_RegionLinesc.Dispose(); ho_RegionLinesd.Dispose(); ho_RegionUniona.Dispose(); ho_RegionUnionb.Dispose(); ho_RegionUnionc.Dispose(); ho_RegionLines1.Dispose(); algorithm.Region.Dispose(); } finally { ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Region1.Dispose(); ho_Region.Dispose(); ho_Contours.Dispose(); ho_SelectedContours.Dispose(); ho_SmoothedContours1.Dispose(); ho_ClippedContoursu.Dispose(); ho_ClippedContoursd.Dispose(); ho_RegionLinesa.Dispose(); ho_RegionLinesb.Dispose(); ho_RegionLinesc.Dispose(); ho_RegionLinesd.Dispose(); ho_RegionUniona.Dispose(); ho_RegionUnionb.Dispose(); ho_RegionUnionc.Dispose(); ho_RegionLines1.Dispose(); algorithm.Region.Dispose(); } }
// Procedures // Local procedures public void DistanceOfLineToLine(HTuple hv_FirstLineStartPoint_Row, HTuple hv_FirstLineStartPoint_Column, HTuple hv_FirstLineEndPoint_Row, HTuple hv_FirstLineEndPoint_Column, HTuple hv_SecondLineStartPoint_Row, HTuple hv_SecondLineStartPoint_Column, HTuple hv_SecondLineEndPoint_Row, HTuple hv_SecondLineEndPoint_Column, out HTuple hv_LineToLine_Distance, out HTuple hv_DistanceStartPoint_Row, out HTuple hv_DistanceStartPoint_Column, out HTuple hv_DistanceEndPoint_Row, out HTuple hv_DistanceEndPoint_Column, out HTuple hv_DottedLinePoint_Row, out HTuple hv_DottedLinePoint_Column, out HTuple hv_LineToLineAngle) { // Local control variables HTuple hv_FirstLineCenterPoint_Row = null; HTuple hv_FirstLineCenterPoint_Column = null, hv_MiddleOfFirstLineToSecondLineRowProj = null; HTuple hv_MiddleOfFirstLineToSecondLineColProj = null; HTuple hv_Max_Column = new HTuple(), hv_Max_Column_Row = new HTuple(); HTuple hv_Min_Column = new HTuple(), hv_Min_Column_Row = new HTuple(); // Initialize local and output iconic variables hv_DottedLinePoint_Row = new HTuple(); hv_DottedLinePoint_Column = new HTuple(); //center point hv_FirstLineCenterPoint_Row = 0.5 * ((hv_FirstLineStartPoint_Row.TupleSelect(0)) + (hv_FirstLineEndPoint_Row.TupleSelect( 0))); hv_FirstLineCenterPoint_Column = 0.5 * ((hv_FirstLineStartPoint_Column.TupleSelect( 0)) + (hv_FirstLineEndPoint_Column.TupleSelect(0))); // HOperatorSet.ProjectionPl(hv_FirstLineCenterPoint_Row.TupleSelect(0), hv_FirstLineCenterPoint_Column.TupleSelect( 0), hv_SecondLineStartPoint_Row.TupleSelect(0), hv_SecondLineStartPoint_Column.TupleSelect( 0), hv_SecondLineEndPoint_Row.TupleSelect(0), hv_SecondLineEndPoint_Column.TupleSelect( 0), out hv_MiddleOfFirstLineToSecondLineRowProj, out hv_MiddleOfFirstLineToSecondLineColProj); // HOperatorSet.DistancePp(hv_FirstLineCenterPoint_Row.TupleSelect(0), hv_FirstLineCenterPoint_Column.TupleSelect( 0), hv_MiddleOfFirstLineToSecondLineRowProj, hv_MiddleOfFirstLineToSecondLineColProj, out hv_LineToLine_Distance); HOperatorSet.AngleLl(hv_FirstLineStartPoint_Row.TupleSelect(0), hv_FirstLineStartPoint_Column.TupleSelect( 0), hv_FirstLineEndPoint_Row.TupleSelect(0), hv_FirstLineEndPoint_Column.TupleSelect( 0), hv_SecondLineStartPoint_Row.TupleSelect(0), hv_SecondLineStartPoint_Column.TupleSelect( 0), hv_SecondLineEndPoint_Row.TupleSelect(0), hv_SecondLineEndPoint_Column.TupleSelect( 0), out hv_LineToLineAngle); hv_DistanceStartPoint_Row = hv_FirstLineCenterPoint_Row.Clone(); hv_DistanceStartPoint_Column = hv_FirstLineCenterPoint_Column.Clone(); hv_DistanceEndPoint_Row = hv_MiddleOfFirstLineToSecondLineRowProj.Clone(); hv_DistanceEndPoint_Column = hv_MiddleOfFirstLineToSecondLineColProj.Clone(); // if ((int)(new HTuple(hv_SecondLineStartPoint_Column.TupleEqual(hv_SecondLineEndPoint_Column))) != 0) { if ((int)(new HTuple(hv_SecondLineStartPoint_Row.TupleLess(hv_SecondLineEndPoint_Row))) != 0) { hv_Max_Column = hv_SecondLineEndPoint_Column.Clone(); hv_Max_Column_Row = hv_SecondLineEndPoint_Row.Clone(); hv_Min_Column = hv_SecondLineStartPoint_Column.Clone(); hv_Min_Column_Row = hv_SecondLineStartPoint_Row.Clone(); } else { hv_Max_Column = hv_SecondLineStartPoint_Column.Clone(); hv_Max_Column_Row = hv_SecondLineStartPoint_Row.Clone(); hv_Min_Column = hv_SecondLineEndPoint_Column.Clone(); hv_Min_Column_Row = hv_SecondLineEndPoint_Row.Clone(); } if ((int)(new HTuple(hv_MiddleOfFirstLineToSecondLineRowProj.TupleLess(hv_Min_Column_Row))) != 0) { hv_DottedLinePoint_Row = hv_Min_Column_Row.Clone(); hv_DottedLinePoint_Column = hv_Min_Column.Clone(); } else if ((int)(new HTuple(hv_MiddleOfFirstLineToSecondLineRowProj.TupleGreater( hv_Max_Column_Row))) != 0) { hv_DottedLinePoint_Row = hv_Max_Column_Row.Clone(); hv_DottedLinePoint_Column = hv_Max_Column.Clone(); } else { hv_DottedLinePoint_Row = hv_MiddleOfFirstLineToSecondLineRowProj.Clone(); hv_DottedLinePoint_Column = hv_MiddleOfFirstLineToSecondLineColProj.Clone(); } } else { if ((int)(new HTuple(hv_SecondLineStartPoint_Column.TupleLess(hv_SecondLineEndPoint_Column))) != 0) { hv_Max_Column = hv_SecondLineEndPoint_Column.Clone(); hv_Max_Column_Row = hv_SecondLineEndPoint_Row.Clone(); hv_Min_Column = hv_SecondLineStartPoint_Column.Clone(); hv_Min_Column_Row = hv_SecondLineStartPoint_Row.Clone(); } else { hv_Max_Column = hv_SecondLineStartPoint_Column.Clone(); hv_Max_Column_Row = hv_SecondLineStartPoint_Row.Clone(); hv_Min_Column = hv_SecondLineEndPoint_Column.Clone(); hv_Min_Column_Row = hv_SecondLineEndPoint_Row.Clone(); } if ((int)(new HTuple(hv_MiddleOfFirstLineToSecondLineColProj.TupleLess(hv_Min_Column))) != 0) { hv_DottedLinePoint_Row = hv_Min_Column_Row.Clone(); hv_DottedLinePoint_Column = hv_Min_Column.Clone(); } else if ((int)(new HTuple(hv_MiddleOfFirstLineToSecondLineColProj.TupleGreater( hv_Max_Column))) != 0) { hv_DottedLinePoint_Row = hv_Max_Column_Row.Clone(); hv_DottedLinePoint_Column = hv_Max_Column.Clone(); } else { hv_DottedLinePoint_Row = hv_MiddleOfFirstLineToSecondLineRowProj.Clone(); hv_DottedLinePoint_Column = hv_MiddleOfFirstLineToSecondLineColProj.Clone(); } } return; }
// Procedures public void GetBlobsByDynThresholdWithMeanExcluded(HObject ho_Image, out HObject ho_FoundRegion, HTuple hv_MedianRadius, HTuple hv_DynMeanMaskWidth, HTuple hv_DynMeanMaskHeight, HTuple hv_DynOffset, HTuple hv_DynLightDark) { // Local iconic variables HObject ho_Domain, ho_ExpandedImage, ho_ImageMedian; HObject ho_ImageMean = null, ho_ImageMedianReduced, ho_ImageMeanReduced; HObject ho_DynRegion; // Local control variables HTuple hv_Width = null, hv_Height = null, hv_Area = null; HTuple hv_ExpandRange = null, hv_Value = null, hv_MeanWidth = new HTuple(); HTuple hv_MeanHeight = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_FoundRegion); HOperatorSet.GenEmptyObj(out ho_Domain); HOperatorSet.GenEmptyObj(out ho_ExpandedImage); HOperatorSet.GenEmptyObj(out ho_ImageMedian); HOperatorSet.GenEmptyObj(out ho_ImageMean); HOperatorSet.GenEmptyObj(out ho_ImageMedianReduced); HOperatorSet.GenEmptyObj(out ho_ImageMeanReduced); HOperatorSet.GenEmptyObj(out ho_DynRegion); ho_Domain.Dispose(); HOperatorSet.GetDomain(ho_Image, out ho_Domain); HOperatorSet.RegionFeatures(ho_Domain, "width", out hv_Width); HOperatorSet.RegionFeatures(ho_Domain, "height", out hv_Height); HOperatorSet.RegionFeatures(ho_Domain, "area", out hv_Area); hv_ExpandRange = hv_MedianRadius + 1; if ((int)(new HTuple(hv_DynMeanMaskWidth.TupleGreater(hv_MedianRadius))) != 0) { hv_ExpandRange = hv_DynMeanMaskWidth.Clone(); } else if ((int)(new HTuple(hv_DynMeanMaskHeight.TupleGreater(hv_MedianRadius))) != 0) { hv_ExpandRange = hv_DynMeanMaskHeight.Clone(); } hv_ExpandRange = hv_ExpandRange * 2; ho_ExpandedImage.Dispose(); HOperatorSet.ExpandDomainGray(ho_Image, out ho_ExpandedImage, hv_ExpandRange); ho_ImageMedian.Dispose(); HOperatorSet.MedianImage(ho_ExpandedImage, out ho_ImageMedian, "circle", hv_MedianRadius, "mirrored"); HOperatorSet.GrayFeatures(ho_Domain, ho_ImageMedian, "mean", out hv_Value); if ((int)(new HTuple(hv_DynMeanMaskWidth.TupleGreater(0))) != 0) { hv_MeanWidth = hv_DynMeanMaskWidth.Clone(); } else { hv_MeanWidth = hv_Width.Clone(); } if ((int)(new HTuple(hv_DynMeanMaskHeight.TupleGreater(0))) != 0) { hv_MeanHeight = hv_DynMeanMaskHeight.Clone(); } else { hv_MeanHeight = hv_Height.Clone(); } if ((int)((new HTuple(hv_DynMeanMaskWidth.TupleGreater(0))).TupleOr(new HTuple(hv_DynMeanMaskHeight.TupleGreater( 0)))) != 0) { ho_ImageMean.Dispose(); HOperatorSet.MeanImage(ho_ImageMedian, out ho_ImageMean, hv_MeanWidth, hv_MeanHeight); } else if ((int)(new HTuple(hv_Area.TupleEqual(hv_Area))) != 0) { ho_ImageMean.Dispose(); HOperatorSet.MeanImage(ho_ImageMedian, out ho_ImageMean, hv_MeanWidth, hv_MeanHeight); } else { ho_ImageMean.Dispose(); HOperatorSet.RegionToMean(ho_Domain, ho_ImageMedian, out ho_ImageMean); } ho_ImageMedianReduced.Dispose(); HOperatorSet.ReduceDomain(ho_ImageMedian, ho_Domain, out ho_ImageMedianReduced ); ho_ImageMeanReduced.Dispose(); HOperatorSet.ReduceDomain(ho_ImageMean, ho_Domain, out ho_ImageMeanReduced); ho_DynRegion.Dispose(); HOperatorSet.DynThreshold(ho_ImageMedianReduced, ho_ImageMeanReduced, out ho_DynRegion, hv_DynOffset, hv_DynLightDark); ho_FoundRegion.Dispose(); HOperatorSet.Connection(ho_DynRegion, out ho_FoundRegion); ho_Domain.Dispose(); ho_ExpandedImage.Dispose(); ho_ImageMedian.Dispose(); ho_ImageMean.Dispose(); ho_ImageMedianReduced.Dispose(); ho_ImageMeanReduced.Dispose(); ho_DynRegion.Dispose(); return; }
// Chapter: Develop // Short Description: Open a new graphics window that preserves the aspect ratio of the given image size. public void dev_open_window_fit_size(HTuple hv_Row, HTuple hv_Column, HTuple hv_Width, HTuple hv_Height, HTuple hv_WidthLimit, HTuple hv_HeightLimit, out HTuple hv_WindowHandle) { // Local iconic variables // Local control variables HTuple hv_MinWidth = new HTuple(), hv_MaxWidth = new HTuple(); HTuple hv_MinHeight = new HTuple(), hv_MaxHeight = new HTuple(); HTuple hv_ResizeFactor = null, hv_TempWidth = null, hv_TempHeight = null; HTuple hv_WindowWidth = new HTuple(), hv_WindowHeight = null; // Initialize local and output iconic variables hv_WindowHandle = new HTuple(); //This procedure open a new graphic window //such that it fits into the limits specified by WidthLimit //and HeightLimit, but also maintains the correct aspect ratio //given by Width and Height. // //If it is impossible to match the minimum and maximum extent requirements //at the same time (f.e. if the image is very long but narrow), //the maximum value gets a higher priority. // //Parse input tuple WidthLimit if ((int)((new HTuple((new HTuple(hv_WidthLimit.TupleLength())).TupleEqual(0))).TupleOr( new HTuple(hv_WidthLimit.TupleLess(0)))) != 0) { hv_MinWidth = 500; hv_MaxWidth = 800; } else if ((int)(new HTuple((new HTuple(hv_WidthLimit.TupleLength())).TupleEqual( 1))) != 0) { hv_MinWidth = 0; hv_MaxWidth = hv_WidthLimit.Clone(); } else { hv_MinWidth = hv_WidthLimit[0]; hv_MaxWidth = hv_WidthLimit[1]; } //Parse input tuple HeightLimit if ((int)((new HTuple((new HTuple(hv_HeightLimit.TupleLength())).TupleEqual(0))).TupleOr( new HTuple(hv_HeightLimit.TupleLess(0)))) != 0) { hv_MinHeight = 400; hv_MaxHeight = 600; } else if ((int)(new HTuple((new HTuple(hv_HeightLimit.TupleLength())).TupleEqual( 1))) != 0) { hv_MinHeight = 0; hv_MaxHeight = hv_HeightLimit.Clone(); } else { hv_MinHeight = hv_HeightLimit[0]; hv_MaxHeight = hv_HeightLimit[1]; } // //Test, if window size has to be changed. hv_ResizeFactor = 1; //First, expand window to the minimum extents (if necessary). if ((int)((new HTuple(hv_MinWidth.TupleGreater(hv_Width))).TupleOr(new HTuple(hv_MinHeight.TupleGreater( hv_Height)))) != 0) { hv_ResizeFactor = (((((hv_MinWidth.TupleReal()) / hv_Width)).TupleConcat((hv_MinHeight.TupleReal() ) / hv_Height))).TupleMax(); } hv_TempWidth = hv_Width * hv_ResizeFactor; hv_TempHeight = hv_Height * hv_ResizeFactor; //Then, shrink window to maximum extents (if necessary). if ((int)((new HTuple(hv_MaxWidth.TupleLess(hv_TempWidth))).TupleOr(new HTuple(hv_MaxHeight.TupleLess( hv_TempHeight)))) != 0) { hv_ResizeFactor = hv_ResizeFactor * ((((((hv_MaxWidth.TupleReal()) / hv_TempWidth)).TupleConcat( (hv_MaxHeight.TupleReal()) / hv_TempHeight))).TupleMin()); } hv_WindowWidth = hv_Width * hv_ResizeFactor; hv_WindowHeight = hv_Height * hv_ResizeFactor; //Resize window //dev_open_window(...); HOperatorSet.SetPart(hv_ExpDefaultWinHandle, 0, 0, hv_Height - 1, hv_Width - 1); return; }
public static void dev_display_shape_matching_results(HTuple hv_WindowHandle, HTuple hv_ModelID, HTuple hv_Color, HTuple hv_Row, HTuple hv_Column, HTuple hv_Angle, HTuple hv_ScaleR, HTuple hv_ScaleC, HTuple hv_Model) { if (hv_WindowHandle == null) { return; } // Local iconic variables HObject ho_ModelContours = null, ho_ContoursAffinTrans = null; // Local control variables HTuple hv_NumMatches, hv_Index = new HTuple(); HTuple hv_Match = new HTuple(), hv_HomMat2DIdentity = new HTuple(); HTuple hv_HomMat2DScale = new HTuple(), hv_HomMat2DRotate = new HTuple(); HTuple hv_HomMat2DTranslate = new HTuple(); HTuple hv_Model_COPY_INP_TMP = hv_Model.Clone(); HTuple hv_ScaleC_COPY_INP_TMP = hv_ScaleC.Clone(); HTuple hv_ScaleR_COPY_INP_TMP = hv_ScaleR.Clone(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_ModelContours); HOperatorSet.GenEmptyObj(out ho_ContoursAffinTrans); try { //This procedure displays the results of Shape-Based Matching. // hv_NumMatches = new HTuple(hv_Row.TupleLength()); if ((int)(new HTuple(hv_NumMatches.TupleGreater(0))) != 0) { if ((int)(new HTuple((new HTuple(hv_ScaleR_COPY_INP_TMP.TupleLength())).TupleEqual( 1))) != 0) { HOperatorSet.TupleGenConst(hv_NumMatches, hv_ScaleR_COPY_INP_TMP, out hv_ScaleR_COPY_INP_TMP); } if ((int)(new HTuple((new HTuple(hv_ScaleC_COPY_INP_TMP.TupleLength())).TupleEqual( 1))) != 0) { HOperatorSet.TupleGenConst(hv_NumMatches, hv_ScaleC_COPY_INP_TMP, out hv_ScaleC_COPY_INP_TMP); } if ((int)(new HTuple((new HTuple(hv_Model_COPY_INP_TMP.TupleLength())).TupleEqual( 0))) != 0) { HOperatorSet.TupleGenConst(hv_NumMatches, 0, out hv_Model_COPY_INP_TMP); } else if ((int)(new HTuple((new HTuple(hv_Model_COPY_INP_TMP.TupleLength() )).TupleEqual(1))) != 0) { HOperatorSet.TupleGenConst(hv_NumMatches, hv_Model_COPY_INP_TMP, out hv_Model_COPY_INP_TMP); } for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_ModelID.TupleLength() )) - 1); hv_Index = (int)hv_Index + 1) { ho_ModelContours.Dispose(); HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelID.TupleSelect( hv_Index), 1); if (hv_WindowHandle > 0) { HOperatorSet.SetColor(hv_WindowHandle, hv_Color.TupleSelect( hv_Index % (new HTuple(hv_Color.TupleLength())))); } for (hv_Match = 0; hv_Match.Continue(hv_NumMatches - 1, 1); hv_Match = hv_Match.TupleAdd(1)) { if ((int)(new HTuple(hv_Index.TupleEqual(hv_Model_COPY_INP_TMP.TupleSelect( hv_Match)))) != 0) { HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity); HOperatorSet.HomMat2dScale(hv_HomMat2DIdentity, hv_ScaleR_COPY_INP_TMP.TupleSelect( hv_Match), hv_ScaleC_COPY_INP_TMP.TupleSelect(hv_Match), 0, 0, out hv_HomMat2DScale); HOperatorSet.HomMat2dRotate(hv_HomMat2DScale, hv_Angle.TupleSelect( hv_Match), 0, 0, out hv_HomMat2DRotate); HOperatorSet.HomMat2dTranslate(hv_HomMat2DRotate, hv_Row.TupleSelect( hv_Match), hv_Column.TupleSelect(hv_Match), out hv_HomMat2DTranslate); ho_ContoursAffinTrans.Dispose(); HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_ContoursAffinTrans, hv_HomMat2DTranslate); HOperatorSet.DispObj(ho_ContoursAffinTrans, hv_WindowHandle); } } } } ho_ModelContours.Dispose(); ho_ContoursAffinTrans.Dispose(); return; } catch (HalconException HDevExpDefaultException) { ho_ModelContours.Dispose(); ho_ContoursAffinTrans.Dispose(); throw HDevExpDefaultException; } }
public void PZT_Detection_ext(HObject ho_RegionPZT_T, HObject ho_R, out HObject ho_Arrow, HTuple hv_mesureNumber, HTuple hv_angle, HTuple hv_mes_width, HTuple hv_sigma, HTuple hv_threshold, HTuple hv_dist_STD, out HTuple hv_dist_PZT, out HTuple hv_pZTOkNg, out HTuple hv_Rows1, out HTuple hv_Cols1, out HTuple hv_Rows2, out HTuple hv_Cols2) { HTuple hv_Dist = null, hv_Row = null, hv_Column = null; HTuple hv_Phi = null, hv_Length1 = null, hv_Length2 = null; HTuple hv_Inter = null, hv_RowStart = null, hv_ColStart = null; HTuple hv_Width = null, hv_Height = null, hv_i = null; HTuple hv_Row_Mes = new HTuple(), hv_Col_Mes = new HTuple(); HTuple hv_MeasureHandle = new HTuple(), hv_RowEdge1 = new HTuple(); HTuple hv_ColumnEdge1 = new HTuple(), hv_Amplitude1 = new HTuple(); HTuple hv_Distance1 = new HTuple(), hv_RowEdge2 = new HTuple(); HTuple hv_ColumnEdge2 = new HTuple(), hv_Amplitude2 = new HTuple(); HTuple hv_Distance2 = new HTuple(), hv_L1 = new HTuple(); HTuple hv_L2 = new HTuple(), hv_Distance = new HTuple(); HTuple hv_Dist_Index = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Arrow); hv_pZTOkNg = new HTuple(); hv_Rows1 = new HTuple(); hv_Cols1 = new HTuple(); hv_Rows2 = new HTuple(); hv_Cols2 = new HTuple(); hv_Dist = new HTuple(); ho_Arrow.Dispose(); HOperatorSet.GenEmptyObj(out ho_Arrow); HOperatorSet.SmallestRectangle2(ho_RegionPZT_T, out hv_Row, out hv_Column, out hv_Phi, out hv_Length1, out hv_Length2); hv_Inter = (((2 * hv_Length2) / hv_mesureNumber)).TupleFloor(); hv_RowStart = hv_Row + ((hv_Length2 - (hv_Inter / 2)) * (((hv_angle + ((new HTuple(90)).TupleRad() ))).TupleSin())); hv_ColStart = hv_Column - ((hv_Length2 - (hv_Inter / 2)) * (((hv_angle + ((new HTuple(90)).TupleRad() ))).TupleCos())); HOperatorSet.GetImageSize(ho_R, out hv_Width, out hv_Height); HTuple end_val11 = hv_mesureNumber - 1; HTuple step_val11 = 1; for (hv_i = 0; hv_i.Continue(end_val11, step_val11); hv_i = hv_i.TupleAdd(step_val11)) { hv_Row_Mes = hv_RowStart - ((hv_Inter * hv_i) * (((hv_angle + ((new HTuple(90)).TupleRad() ))).TupleSin())); hv_Col_Mes = hv_ColStart + ((hv_Inter * hv_i) * (((hv_angle + ((new HTuple(90)).TupleRad() ))).TupleCos())); HOperatorSet.GenMeasureRectangle2(hv_Row_Mes, hv_Col_Mes, hv_angle, hv_Length1, hv_mes_width, hv_Width, hv_Height, "nearest_neighbor", out hv_MeasureHandle); HOperatorSet.MeasurePos(ho_R, hv_MeasureHandle, hv_sigma, hv_threshold, "positive", "first", out hv_RowEdge1, out hv_ColumnEdge1, out hv_Amplitude1, out hv_Distance1); HOperatorSet.MeasurePos(ho_R, hv_MeasureHandle, hv_sigma, hv_threshold, "negative", "last", out hv_RowEdge2, out hv_ColumnEdge2, out hv_Amplitude2, out hv_Distance2); HOperatorSet.CloseMeasure(hv_MeasureHandle); HOperatorSet.TupleLength(hv_RowEdge1, out hv_L1); HOperatorSet.TupleLength(hv_RowEdge2, out hv_L2); if ((int)((new HTuple(hv_L1.TupleNotEqual(1))).TupleOr(new HTuple(hv_L2.TupleNotEqual( 1)))) != 0) { continue; } hv_Rows1 = hv_Rows1.TupleConcat(hv_RowEdge1); hv_Cols1 = hv_Cols1.TupleConcat(hv_ColumnEdge1); hv_Rows2 = hv_Rows2.TupleConcat(hv_RowEdge2); hv_Cols2 = hv_Cols2.TupleConcat(hv_ColumnEdge2); HOperatorSet.DistancePp(hv_RowEdge1, hv_ColumnEdge1, hv_RowEdge2, hv_ColumnEdge2, out hv_Distance); hv_Dist = hv_Dist.TupleConcat(hv_Distance); } ho_Arrow.Dispose(); gen_arrow_contour_xld(out ho_Arrow, hv_Rows1, hv_Cols1, hv_Rows2, hv_Cols2, 20, 20); HOperatorSet.TupleSortIndex(hv_Dist, out hv_Dist_Index); hv_dist_PZT = hv_Dist.TupleSelect(hv_Dist_Index.TupleSelectRange(1, (new HTuple(hv_Dist_Index.TupleLength() )) - 2)); hv_dist_PZT = hv_dist_PZT.TupleMean(); if ((int)(new HTuple(hv_dist_PZT.TupleGreater(hv_dist_STD))) != 0) { hv_pZTOkNg = 1; } else { hv_pZTOkNg = 0; } return; }
// Procedures public void GetRegionBySobelAmp2(HObject ho_Image, out HObject ho_FoundRegion, HTuple hv_MedianRadius, HTuple hv_MeanMaskWidth, HTuple hv_MeanMaskHeight, HTuple hv_SobelAmpSize, HTuple hv_ScaleMult, HTuple hv_ScaleAdd, HTuple hv_AreaMin, HTuple hv_AreaMax, HTuple hv_OpeningWidth, HTuple hv_OpeningHeight, HTuple hv_ClosingWidth, HTuple hv_ClosingHeight, HTuple hv_ErosionWidth, HTuple hv_ErosionHeight, HTuple hv_DilationWidth, HTuple hv_DilationHeight) { // Stack for temporary objects HObject[] OTemp = new HObject[20]; // Local iconic variables HObject ho_Domain, ho_ImageMedian, ho_ImageMean; HObject ho_EdgeAmplitude, ho_ImageScaled1, ho_ImageScaled; HObject ho_Region, ho_ConnectedRegions, ho_SelectedRegions; HObject ho_RegionFillUp, ho_RegionUnion; // Local control variables HTuple hv_Value = null, hv_UsedThreshold = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_FoundRegion); HOperatorSet.GenEmptyObj(out ho_Domain); HOperatorSet.GenEmptyObj(out ho_ImageMedian); HOperatorSet.GenEmptyObj(out ho_ImageMean); HOperatorSet.GenEmptyObj(out ho_EdgeAmplitude); HOperatorSet.GenEmptyObj(out ho_ImageScaled1); HOperatorSet.GenEmptyObj(out ho_ImageScaled); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_RegionFillUp); HOperatorSet.GenEmptyObj(out ho_RegionUnion); ho_Domain.Dispose(); HOperatorSet.GetDomain(ho_Image, out ho_Domain); ho_ImageMedian.Dispose(); HOperatorSet.MedianImage(ho_Image, out ho_ImageMedian, "circle", hv_MedianRadius, "mirrored"); ho_ImageMean.Dispose(); HOperatorSet.MeanImage(ho_ImageMedian, out ho_ImageMean, hv_MeanMaskWidth, hv_MeanMaskHeight); ho_EdgeAmplitude.Dispose(); HOperatorSet.SobelAmp(ho_ImageMean, out ho_EdgeAmplitude, "sum_abs", hv_SobelAmpSize); HOperatorSet.GrayFeatures(ho_Domain, ho_EdgeAmplitude, "mean", out hv_Value); ho_ImageScaled1.Dispose(); HOperatorSet.ScaleImage(ho_EdgeAmplitude, out ho_ImageScaled1, 1, -hv_Value); ho_ImageScaled.Dispose(); HOperatorSet.ScaleImage(ho_ImageScaled1, out ho_ImageScaled, hv_ScaleMult, hv_ScaleAdd); ho_Region.Dispose(); HOperatorSet.BinaryThreshold(ho_ImageScaled, out ho_Region, "max_separability", "dark", out hv_UsedThreshold); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions); ho_SelectedRegions.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area", "and", hv_AreaMin, hv_AreaMax); ho_RegionFillUp.Dispose(); HOperatorSet.FillUp(ho_SelectedRegions, out ho_RegionFillUp); if ((int)((new HTuple(hv_OpeningWidth.TupleGreater(0))).TupleAnd(new HTuple(hv_OpeningHeight.TupleGreater( 0)))) != 0) { { HObject ExpTmpOutVar_0; HOperatorSet.OpeningRectangle1(ho_RegionFillUp, out ExpTmpOutVar_0, hv_OpeningWidth, hv_OpeningHeight); ho_RegionFillUp.Dispose(); ho_RegionFillUp = ExpTmpOutVar_0; } } if ((int)((new HTuple(hv_ClosingWidth.TupleGreater(0))).TupleAnd(new HTuple(hv_ClosingHeight.TupleGreater( 0)))) != 0) { { HObject ExpTmpOutVar_0; HOperatorSet.ClosingRectangle1(ho_RegionFillUp, out ExpTmpOutVar_0, hv_ClosingWidth, hv_ClosingHeight); ho_RegionFillUp.Dispose(); ho_RegionFillUp = ExpTmpOutVar_0; } } if ((int)((new HTuple(hv_ErosionWidth.TupleGreater(0))).TupleAnd(new HTuple(hv_ErosionHeight.TupleGreater( 0)))) != 0) { { HObject ExpTmpOutVar_0; HOperatorSet.ErosionRectangle1(ho_RegionFillUp, out ExpTmpOutVar_0, hv_ErosionWidth, hv_ErosionHeight); ho_RegionFillUp.Dispose(); ho_RegionFillUp = ExpTmpOutVar_0; } } if ((int)((new HTuple(hv_DilationWidth.TupleGreater(0))).TupleAnd(new HTuple(hv_DilationHeight.TupleGreater( 0)))) != 0) { { HObject ExpTmpOutVar_0; HOperatorSet.DilationRectangle1(ho_RegionFillUp, out ExpTmpOutVar_0, hv_DilationWidth, hv_DilationHeight); ho_RegionFillUp.Dispose(); ho_RegionFillUp = ExpTmpOutVar_0; } } ho_RegionUnion.Dispose(); HOperatorSet.Union1(ho_RegionFillUp, out ho_RegionUnion); ho_FoundRegion.Dispose(); HOperatorSet.MoveRegion(ho_RegionUnion, out ho_FoundRegion, 0, 0); ho_Domain.Dispose(); ho_ImageMedian.Dispose(); ho_ImageMean.Dispose(); ho_EdgeAmplitude.Dispose(); ho_ImageScaled1.Dispose(); ho_ImageScaled.Dispose(); ho_Region.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_RegionFillUp.Dispose(); ho_RegionUnion.Dispose(); return; }
// Main procedure private void action() { // Local iconic variables HObject ho_Image, ho_DarkRegion = null, ho_CharRegion = null; HObject ho_CharConnected = null, ho_CharOrig = null, ho_CharSel = null; HObject ho_Characters = null, ho_CharRectangle = null, ho_CharROI = null; HObject ho_ImageReduced = null, ho_ImageScaled = null, ho_CharConvex = null; HObject ho_SingleChar = null; // Local control variables HTuple hv_OCRFontName, hv_FontName, hv_FontImages; HTuple hv_NumImages, hv_Width, hv_Height, hv_WindowID; HTuple hv_OCRHandle, hv_WidthCharacter, hv_HeightCharacter; HTuple hv_Interpolation, hv_Features, hv_Char, hv_NumHidden; HTuple hv_Preprocessing, hv_NumComponents, hv_NumChar; HTuple hv_NumSamples, hv_i, hv_Min = new HTuple(), hv_Max = new HTuple(); HTuple hv_Range = new HTuple(), hv_WindowChar = new HTuple(); HTuple hv_DoLoop = new HTuple(), hv_Row = new HTuple(), hv_Column = new HTuple(); HTuple hv_Button = new HTuple(), hv_Index = new HTuple(), hv_RecChar = new HTuple(); HTuple hv_Confidence = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_DarkRegion); HOperatorSet.GenEmptyObj(out ho_CharRegion); HOperatorSet.GenEmptyObj(out ho_CharConnected); HOperatorSet.GenEmptyObj(out ho_CharOrig); HOperatorSet.GenEmptyObj(out ho_CharSel); HOperatorSet.GenEmptyObj(out ho_Characters); HOperatorSet.GenEmptyObj(out ho_CharRectangle); HOperatorSet.GenEmptyObj(out ho_CharROI); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_ImageScaled); HOperatorSet.GenEmptyObj(out ho_CharConvex); HOperatorSet.GenEmptyObj(out ho_SingleChar); try { // //OCR // // dev_update_window(...); only in hdevelop //Specify the name of the font to use for reading the date on the bottle. //It is easiest to use the pre-trained font Document. If you have run the //program fontt.hdev in this directory, you can activate the second line //to use the font trained with this program. hv_OCRFontName = "Document"; //OCRFontName := 'arial' hv_FontName = "arial"; hv_FontImages = new HTuple(); hv_FontImages[0] = "a1"; hv_FontImages[1] = "a2"; hv_FontImages[2] = "a3"; hv_FontImages[3] = "a4"; hv_FontImages[4] = "a5"; hv_FontImages[5] = "a6"; hv_FontImages[6] = "a7"; hv_FontImages[7] = "a8"; hv_FontImages[8] = "a9"; hv_NumImages = new HTuple(hv_FontImages.TupleLength()); ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, (("fonts/" + hv_FontName) + "_") + (hv_FontImages.TupleSelect( 0))); if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); HOperatorSet.SetWindowAttr("background_color", "black"); HOperatorSet.OpenWindow(0, 0, hv_Width / 2, hv_Height / 2, 0, "", "", out hv_WindowID); HDevWindowStack.Push(hv_WindowID); set_display_font(hv_WindowID, 14, "mono", "true", "false"); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } disp_continue_message(hv_WindowID, "black", "true"); //stop () //---------- read OCR classifier ---------- HOperatorSet.ReadOcrClassMlp(hv_OCRFontName, out hv_OCRHandle); HOperatorSet.GetParamsOcrClassMlp(hv_OCRHandle, out hv_WidthCharacter, out hv_HeightCharacter, out hv_Interpolation, out hv_Features, out hv_Char, out hv_NumHidden, out hv_Preprocessing, out hv_NumComponents); hv_NumChar = new HTuple(hv_Char.TupleLength()); disp_continue_message(hv_WindowID, "black", "true"); HDevelopStop(); //---------- segmentation of characters ---------- if (HDevWindowStack.IsOpen()) { HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetShape(HDevWindowStack.GetActive(), "rectangle1"); } hv_NumSamples = 0; for (hv_i = 1; hv_i.Continue(hv_NumImages, 1); hv_i = hv_i.TupleAdd(1)) { if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColored(HDevWindowStack.GetActive(), 12); } ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, (("fonts/" + hv_FontName) + "_") + (hv_FontImages.TupleSelect( hv_i - 1))); ho_DarkRegion.Dispose(); HOperatorSet.BinThreshold(ho_Image, out ho_DarkRegion); ho_CharRegion.Dispose(); HOperatorSet.ClosingCircle(ho_DarkRegion, out ho_CharRegion, 8.5); ho_CharConnected.Dispose(); HOperatorSet.Connection(ho_CharRegion, out ho_CharConnected); ho_CharOrig.Dispose(); HOperatorSet.Intersection(ho_CharConnected, ho_DarkRegion, out ho_CharOrig ); ho_CharSel.Dispose(); HOperatorSet.SelectShape(ho_CharOrig, out ho_CharSel, "area", "and", 50, 99999); ho_Characters.Dispose(); HOperatorSet.SortRegion(ho_CharSel, out ho_Characters, "character", "true", "row"); //adapt to different brigthness ho_CharRectangle.Dispose(); HOperatorSet.ShapeTrans(ho_CharRegion, out ho_CharRectangle, "rectangle1"); ho_CharROI.Dispose(); HOperatorSet.DilationRectangle1(ho_CharRectangle, out ho_CharROI, 10, 10); ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_CharROI, out ho_ImageReduced); HOperatorSet.MinMaxGray(ho_CharROI, ho_Image, 5, out hv_Min, out hv_Max, out hv_Range); ho_ImageScaled.Dispose(); HOperatorSet.ScaleImage(ho_ImageReduced, out ho_ImageScaled, 255 / hv_Range, ((-hv_Min) * 255) / hv_Range); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Characters, HDevWindowStack.GetActive()); } disp_continue_message(hv_WindowID, "black", "true"); HDevelopStop(); //interactive OCR HOperatorSet.SetWindowAttr("background_color", "black"); HOperatorSet.OpenWindow(0, (hv_Width / 2) + 50, 128, 128, 0, "", "", out hv_WindowChar); HDevWindowStack.Push(hv_WindowChar); set_display_font(hv_WindowChar, 27, "mono", "true", "false"); HDevWindowStack.SetActive(hv_WindowID); ho_CharConvex.Dispose(); HOperatorSet.ShapeTrans(ho_Characters, out ho_CharConvex, "rectangle1"); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } hv_DoLoop = 1; while ((int)(new HTuple(hv_DoLoop.TupleEqual(1))) != 0) { if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red"); } disp_message(hv_WindowID, "Left-click on a character to read; right mouse button to proceed to next image", "image", 8, 10, "blue", "false"); HOperatorSet.GetMbutton(hv_WindowID, out hv_Row, out hv_Column, out hv_Button); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } HOperatorSet.GetRegionIndex(ho_CharConvex, hv_Row, hv_Column, out hv_Index); if ((int)(new HTuple(hv_Index.TupleGreater(0))) != 0) { ho_SingleChar.Dispose(); HOperatorSet.SelectObj(ho_Characters, out ho_SingleChar, hv_Index); HOperatorSet.DoOcrSingleClassMlp(ho_SingleChar, ho_ImageScaled, hv_OCRHandle, 1, out hv_RecChar, out hv_Confidence); HDevWindowStack.SetActive(hv_WindowChar); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, 127, 127); } if (HDevWindowStack.IsOpen()) { HOperatorSet.ClearWindow(HDevWindowStack.GetActive()); } disp_message(hv_WindowChar, hv_RecChar.TupleSelect(0), "image", 40, 53, "green", "false"); HDevWindowStack.SetActive(hv_WindowID); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, hv_Height - 1, hv_Width - 1); } } if ((int)(new HTuple(hv_Button.TupleEqual(4))) != 0) { hv_DoLoop = 0; HDevWindowStack.SetActive(hv_WindowChar); if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } HDevWindowStack.SetActive(hv_WindowID); } } } HOperatorSet.ClearOcrClassMlp(hv_OCRHandle); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetShape(HDevWindowStack.GetActive(), "original"); } // dev_update_window(...); only in hdevelop } catch (HalconException HDevExpDefaultException) { ho_Image.Dispose(); ho_DarkRegion.Dispose(); ho_CharRegion.Dispose(); ho_CharConnected.Dispose(); ho_CharOrig.Dispose(); ho_CharSel.Dispose(); ho_Characters.Dispose(); ho_CharRectangle.Dispose(); ho_CharROI.Dispose(); ho_ImageReduced.Dispose(); ho_ImageScaled.Dispose(); ho_CharConvex.Dispose(); ho_SingleChar.Dispose(); throw HDevExpDefaultException; } ho_Image.Dispose(); ho_DarkRegion.Dispose(); ho_CharRegion.Dispose(); ho_CharConnected.Dispose(); ho_CharOrig.Dispose(); ho_CharSel.Dispose(); ho_Characters.Dispose(); ho_CharRectangle.Dispose(); ho_CharROI.Dispose(); ho_ImageReduced.Dispose(); ho_ImageScaled.Dispose(); ho_CharConvex.Dispose(); ho_SingleChar.Dispose(); }
private void checkModel(HObject ho_Image1) { HTuple hv_Row = new HTuple(); HTuple hv_Column = new HTuple(); HTuple hv_Width = new HTuple(), hv_Height = new HTuple(); HTuple hv_ModelID1 = new HTuple(), hv_ModelID2 = new HTuple(); HTuple hv_ModelID3 = new HTuple(), hv_ModelID4 = new HTuple(); HTuple hv_ModelIDs = new HTuple(), hv_Angle = new HTuple(); HTuple hv_Score = new HTuple(), hv_ModelIndex = new HTuple(); hv_ModelID1.Dispose(); HOperatorSet.ReadShapeModel("f:modelFiles/" + modelList.SelectedItem.ToString() + "/modle1.shm", out hv_ModelID1); hv_ModelID2.Dispose(); HOperatorSet.ReadShapeModel("f:modelFiles/" + modelList.SelectedItem.ToString() + "/modle2.shm", out hv_ModelID2); hv_ModelID3.Dispose(); HOperatorSet.ReadShapeModel("f:modelFiles/" + modelList.SelectedItem.ToString() + "/modle3.shm", out hv_ModelID3); hv_ModelID4.Dispose(); HOperatorSet.ReadShapeModel("f:modelFiles/" + modelList.SelectedItem.ToString() + "/modle4.shm", out hv_ModelID4); hv_ModelIDs.Dispose(); using (HDevDisposeHelper dh = new HDevDisposeHelper()) { hv_ModelIDs = new HTuple(); hv_ModelIDs = hv_ModelIDs.TupleConcat(hv_ModelID1); hv_ModelIDs = hv_ModelIDs.TupleConcat(hv_ModelID2); hv_ModelIDs = hv_ModelIDs.TupleConcat(hv_ModelID3); hv_ModelIDs = hv_ModelIDs.TupleConcat(hv_ModelID4); } HOperatorSet.GetImageSize(ho_Image1, out hv_Width, out hv_Height); if (hWindowControl1.HalconWindow.ToString() == "") { return; } HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, hv_Width + 1, hv_Height + 1); HOperatorSet.DispObj(ho_Image1, hWindowControl1.HalconWindow); hv_Row.Dispose(); hv_Column.Dispose(); hv_Angle.Dispose(); hv_Score.Dispose(); hv_ModelIndex.Dispose(); HOperatorSet.FindShapeModels(ho_Image1, hv_ModelIDs, 0, (new HTuple(360)).TupleRad() , 0.5, 8, 0.5, "least_squares", 0, 0.8, out hv_Row, out hv_Column, out hv_Angle, out hv_Score, out hv_ModelIndex); if ((int)(new HTuple(hv_Score.TupleGreater(0))) != 0) { pictureBox1.BackColor = Color.Green; a++; TrueNum.Text = a + "个"; } else { pictureBox1.BackColor = Color.Red; b++; WrongNum.Text = b + "个"; } c = a + b; allNum.Text = c + "个"; /* ho_Image1.Dispose(); * hv_AcqHandle.Dispose(); * hv_Row.Dispose(); * hv_Column.Dispose(); * hv_ModelID1.Dispose(); * hv_ModelID2.Dispose(); * hv_ModelID3.Dispose(); * hv_ModelID4.Dispose(); * hv_ModelIDs.Dispose(); * hv_Angle.Dispose(); * hv_Score.Dispose(); * hv_ModelIndex.Dispose();*/ }
public void FindBond(HObject ho_GrayImage, out HObject ho_xldBond, out HObject ho_ObjectSelected1, out HObject ho_ObjectSelected2, out HObject ho_ObjectSelected3, HTuple hv_Row, HTuple hv_Column, HTuple hv_model, out HTuple hv_Row_1, out HTuple hv_Column_1, out HTuple hv_Row_2, out HTuple hv_Column_2, out HTuple hv_Row_3, out HTuple hv_Column_3, out HTuple hv_flag, out HTuple hv_string) { // Local iconic variables HObject ho_Rectangle, ho_ImageReduced1, ho_Contours; HObject ho_Cross = null, ho_Region = null, ho_ConnectedRegions = null; HObject ho_SortedRegions = null, ho_Cross1 = null; // Local control variables HTuple hv_Row1 = null, hv_Column1 = null, hv_Angle = null; HTuple hv_Score = null, hv_Model = null, hv_MinDistance1 = new HTuple(); HTuple hv_MinDistance2 = new HTuple(), hv_Row_21 = new HTuple(); HTuple hv_Column_21 = new HTuple(), hv_MinDistance3 = new HTuple(); HTuple hv_Row11 = new HTuple(), hv_Column11 = new HTuple(); HTuple hv_Row32 = new HTuple(), hv_Column32 = new HTuple(); HTuple hv_Dis1 = new HTuple(), hv_Dis2 = new HTuple(); HTuple hv_Dis3 = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_xldBond); HOperatorSet.GenEmptyObj(out ho_ObjectSelected1); HOperatorSet.GenEmptyObj(out ho_ObjectSelected2); HOperatorSet.GenEmptyObj(out ho_ObjectSelected3); HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_ImageReduced1); HOperatorSet.GenEmptyObj(out ho_Contours); HOperatorSet.GenEmptyObj(out ho_Cross); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SortedRegions); HOperatorSet.GenEmptyObj(out ho_Cross1); hv_Row_1 = new HTuple(); hv_Column_1 = new HTuple(); hv_Row_2 = new HTuple(); hv_Column_2 = new HTuple(); hv_Row_3 = new HTuple(); hv_Column_3 = new HTuple(); hv_flag = new HTuple(); hv_string = new HTuple(); ho_xldBond.Dispose(); HOperatorSet.GenEmptyObj(out ho_xldBond); ho_Rectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle, hv_Row - 10, hv_Column - 20, hv_Row + 30, hv_Column + 70); ho_ImageReduced1.Dispose(); HOperatorSet.ReduceDomain(ho_GrayImage, ho_Rectangle, out ho_ImageReduced1); ho_Contours.Dispose(); HOperatorSet.GenContourRegionXld(ho_Rectangle, out ho_Contours, "border"); HOperatorSet.FindNccModels(ho_ImageReduced1, hv_model, 0, 1.57, 0.8, 3, 0.5, "true", 0, out hv_Row1, out hv_Column1, out hv_Angle, out hv_Score, out hv_Model); if ((int)(new HTuple((new HTuple(hv_Score.TupleLength())).TupleEqual(3))) != 0) { ho_Cross.Dispose(); HOperatorSet.GenCrossContourXld(out ho_Cross, hv_Row1, hv_Column1, 16, hv_Angle); ho_Region.Dispose(); HOperatorSet.GenRegionPoints(out ho_Region, hv_Row1, hv_Column1); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions); ho_SortedRegions.Dispose(); HOperatorSet.SortRegion(ho_ConnectedRegions, out ho_SortedRegions, "first_point", "true", "column"); ho_ObjectSelected1.Dispose(); HOperatorSet.SelectObj(ho_SortedRegions, out ho_ObjectSelected1, 1); ho_ObjectSelected2.Dispose(); HOperatorSet.SelectObj(ho_SortedRegions, out ho_ObjectSelected2, 2); ho_ObjectSelected3.Dispose(); HOperatorSet.SelectObj(ho_SortedRegions, out ho_ObjectSelected3, 3); HOperatorSet.DistanceRrMin(ho_ObjectSelected1, ho_ObjectSelected2, out hv_MinDistance1, out hv_Row_1, out hv_Column_1, out hv_Row_2, out hv_Column_2); HOperatorSet.DistanceRrMin(ho_ObjectSelected3, ho_ObjectSelected2, out hv_MinDistance2, out hv_Row_3, out hv_Column_3, out hv_Row_21, out hv_Column_21); HOperatorSet.DistanceRrMin(ho_ObjectSelected1, ho_ObjectSelected3, out hv_MinDistance3, out hv_Row11, out hv_Column11, out hv_Row32, out hv_Column32); hv_Dis1 = hv_MinDistance1 - 10; hv_Dis2 = hv_MinDistance2 - 30; hv_Dis3 = hv_MinDistance2 - 30; ho_Cross1.Dispose(); HOperatorSet.GenCrossContourXld(out ho_Cross1, hv_Row_1, hv_Column_1, 16, hv_Angle); ho_Cross1.Dispose(); HOperatorSet.GenCrossContourXld(out ho_Cross1, hv_Row_2, hv_Column_2, 16, hv_Angle); if ((int)((new HTuple((new HTuple(hv_Dis1.TupleGreater(0))).TupleAnd(new HTuple(hv_Dis2.TupleGreater( 0))))).TupleAnd(new HTuple(hv_Dis3.TupleGreater(0)))) != 0) { hv_flag = 1; hv_string = "OK"; ho_xldBond.Dispose(); ho_xldBond = ho_Cross.CopyObj(1, -1); } else { hv_flag = 6; hv_string = "Find Bond Wrong"; ho_xldBond.Dispose(); ho_xldBond = ho_Contours.CopyObj(1, -1); } } else { hv_flag = 6; hv_string = "Find Bond Wrong"; ho_xldBond.Dispose(); ho_xldBond = ho_Contours.CopyObj(1, -1); } ho_Rectangle.Dispose(); ho_ImageReduced1.Dispose(); ho_Contours.Dispose(); ho_Cross.Dispose(); ho_Region.Dispose(); ho_ConnectedRegions.Dispose(); ho_SortedRegions.Dispose(); ho_Cross1.Dispose(); return; }
/// <summary> /// 初始化後才能叫用 Action /// </summary> public MeasureResult Action() { #region 輸出結果, CircleResult or Distance Result CircleResult mResult = null; #endregion // Local iconic variables HObject ho_R1_Circle = null; HObject ho_R1_ROI_Image = null, ho_R1_Region = null, ho_R1_ImageReduced = null; HObject ho_R1_Edges = null, ho_R1_ContoursSplit = null, ho_R1_SingleSegment = null; //HObject ho_R1_ContEllipse = null; // Local control variables HTuple hv_msgOffsetY, hv_msgOffsetX; //HTuple hv_AllModelId; HTuple hv_STD_Row; HTuple hv_STD_Col, hv_Img_Row, hv_Img_Col, hv_Img_Rotate_Angle; HTuple hv_OffsetRow, hv_OffsetCol, hv_STD_R1_Row, hv_STD_R1_Col; HTuple hv_STD_R1_V_Row, hv_STD_R1_V_Col, hv_R1_X, hv_R1_Y; HTuple hv_R1_Pos_Row, hv_R1_Pos_Col, hv_R1_R; HTuple hv_alpha = new HTuple(), hv_R1_low = new HTuple(), hv_R1_high = new HTuple(); HTuple hv_R1_NumSegments = new HTuple(), hv_NumCircles = new HTuple(); HTuple hv_Num_Circle_Point = new HTuple(), hv_R1 = new HTuple(); HTuple hv_R1_limit = new HTuple(), hv_i = new HTuple(), hv_Attrib = new HTuple(); HTuple hv_R1_Row = new HTuple(), hv_R1_Column = new HTuple(); HTuple hv_R1_Radius = new HTuple(), hv_R1_StartPhi = new HTuple(); HTuple hv_R1_EndPhi = new HTuple(), hv_R1_PointOrder = new HTuple(); HTuple hv_R1_MinDist = new HTuple(), hv_R1_MaxDist = new HTuple(); HTuple hv_R1_AvgDist = new HTuple(), hv_R1_SigmaDist = new HTuple(); HTuple hv_ResultText = new HTuple(), hv_MeasureReasult; // Initialize local and output iconic variables //HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_R1_Circle); HOperatorSet.GenEmptyObj(out ho_R1_ROI_Image); HOperatorSet.GenEmptyObj(out ho_R1_Region); HOperatorSet.GenEmptyObj(out ho_R1_ImageReduced); HOperatorSet.GenEmptyObj(out ho_R1_Edges); HOperatorSet.GenEmptyObj(out ho_R1_ContoursSplit); HOperatorSet.GenEmptyObj(out ho_R1_SingleSegment); //HOperatorSet.GenEmptyObj(out ho_R1_ContEllipse); //Measure: SDMS_R1 //Author: John Hsieh //Date: 2012 //ho_Image.Dispose(); //HOperatorSet.ReadImage(out ho_Image, "D:/Projects/Halcon/SDMS/SDMS_Measure/Images/E-1/E1-1.bmp"); //dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle); //dev_update_off () // dev_update_window(...); only in hdevelop HOperatorSet.SetSystem("border_shape_models", "false"); //****Message Args hv_msgOffsetY = 100; hv_msgOffsetX = 100; //****Model All //HOperatorSet.ReadShapeModel("D:/Projects/Halcon/SDMS/SDMS_Measure/Model/MatchingAll.shm", // out hv_AllModelId); //ho_AllModelContours.Dispose(); //HOperatorSet.GetShapeModelContours(out ho_AllModelContours, hv_AllModelId, 1); //HOperatorSet.FindShapeModel(ho_Image, hv_AllModelId, (new HTuple(0)).TupleRad() // , (new HTuple(360)).TupleRad(), 0.5, 1, 0.5, "least_squares", 6, 0.75, out hv_AllModelRow, // out hv_AllModelColumn, out hv_AllModelAngle, out modelScore); //****Model Args //STD 中心點 hv_STD_Row = 772; hv_STD_Col = 1003; //目前圖形 中心點 hv_Img_Row = hv_AllModelRow.Clone(); hv_Img_Col = hv_AllModelColumn.Clone(); //目前圖形 Rotate Angle hv_Img_Rotate_Angle = hv_AllModelAngle.Clone(); //目前圖形偏移量 hv_OffsetRow = hv_Img_Row - hv_STD_Row; hv_OffsetCol = hv_Img_Col - hv_STD_Col; //****R1 //STD R1_ 位置 hv_STD_R1_Row = 589; hv_STD_R1_Col = 705; //STD 向量 STD_R1_ hv_STD_R1_V_Row = hv_STD_R1_Row - hv_STD_Row; hv_STD_R1_V_Col = hv_STD_R1_Col - hv_STD_Col; //R1_X, R1_Y 分量 hv_R1_X = (hv_STD_R1_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_R1_V_Row * (hv_Img_Rotate_Angle.TupleSin() )); hv_R1_Y = (hv_STD_R1_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_R1_V_Col * (hv_Img_Rotate_Angle.TupleSin() )); //目前圖形 R1_ 位置 hv_R1_Pos_Row = (hv_STD_Row + hv_R1_Y) + hv_OffsetRow; hv_R1_Pos_Col = (hv_STD_Col + hv_R1_X) + hv_OffsetCol; hv_R1_R = 20; //*ROI ho_R1_Circle.Dispose(); HOperatorSet.GenCircle(out ho_R1_Circle, hv_R1_Pos_Row, hv_R1_Pos_Col, hv_R1_R); ho_R1_ROI_Image.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_R1_Circle, out ho_R1_ROI_Image); ho_R1_Region.Dispose(); HOperatorSet.FastThreshold(ho_R1_ROI_Image, out ho_R1_Region, 100, 255, 20); ho_R1_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_R1_ROI_Image, ho_R1_Region, out ho_R1_ImageReduced ); //sobel_fast 具有較寬的選擇範圍,搭配 alpha 參數 (alpha 越大, 容錯範圍大) hv_alpha = 0.9; hv_R1_low = 20; hv_R1_high = 40; ho_R1_Edges.Dispose(); HOperatorSet.EdgesSubPix(ho_R1_ImageReduced, out ho_R1_Edges, "sobel_fast", hv_alpha, hv_R1_low, hv_R1_high); //stop () //*所有的數值越小,表示容錯範圍大,反之亦然 ho_R1_ContoursSplit.Dispose(); HOperatorSet.SegmentContoursXld(ho_R1_Edges, out ho_R1_ContoursSplit, "lines_circles", 17, 1, 1); //Display the results //=========================================================== HOperatorSet.CountObj(ho_R1_ContoursSplit, out hv_R1_NumSegments); hv_NumCircles = 0; hv_Num_Circle_Point = 5; hv_R1 = 999; hv_R1_limit = 10; for (hv_i = 1; hv_i.Continue(hv_R1_NumSegments, 1); hv_i = hv_i.TupleAdd(1)) { ho_R1_SingleSegment.Dispose(); HOperatorSet.SelectObj(ho_R1_ContoursSplit, out ho_R1_SingleSegment, hv_i); HOperatorSet.GetContourGlobalAttribXld(ho_R1_SingleSegment, "cont_approx", out hv_Attrib); if ((int)(new HTuple(hv_Attrib.TupleEqual(1))) != 0) { HOperatorSet.FitCircleContourXld(ho_R1_SingleSegment, "atukey", -1, 2, hv_Num_Circle_Point, 5, 2, out hv_R1_Row, out hv_R1_Column, out hv_R1_Radius, out hv_R1_StartPhi, out hv_R1_EndPhi, out hv_R1_PointOrder); //ho_R1_ContEllipse.Dispose(); //HOperatorSet.GenEllipseContourXld(out ho_R1_ContEllipse, hv_R1_Row, hv_R1_Column, // 0, hv_R1_Radius, hv_R1_Radius, 0, (new HTuple(360)).TupleRad(), "positive", // 1.0); //HOperatorSet.DistEllipseContourXld(ho_R1_SingleSegment, "algebraic", -1, // 0, hv_R1_Row, hv_R1_Column, 0, hv_R1_Radius, hv_R1_Radius, out hv_R1_MinDist, // out hv_R1_MaxDist, out hv_R1_AvgDist, out hv_R1_SigmaDist); hv_NumCircles = hv_NumCircles + 1; if ((int)(new HTuple(hv_R1.TupleGreater(hv_R1_Radius))) != 0) { hv_R1 = hv_R1_Radius.Clone(); //hv_ResultText = (((("C" + hv_NumCircles) + ": Radius = ") + (hv_R1_Radius.TupleString( // ".3"))) + " / MaxDeviation: ") + (hv_R1_MaxDist.TupleString(".3")); //HOperatorSet.SetTposition(hv_WindowHandle, hv_R1_Pos_Row - hv_msgOffsetY, // hv_R1_Pos_Col - hv_msgOffsetX); //HOperatorSet.WriteString(hv_WindowHandle, "R1"); #region 組合結果 mResult = new CircleResult(new HTuple(hv_R1_Row) , new HTuple(hv_R1_Column) , new HTuple(hv_R1_Radius) , new HTuple(hv_R1_StartPhi) , new HTuple(hv_R1_EndPhi) , new HTuple(hv_R1_PointOrder)) { }; #endregion } } } hv_MeasureReasult = hv_R1.Clone(); //****R1 End //ho_Image.Dispose(); ho_R1_Circle.Dispose(); ho_R1_ROI_Image.Dispose(); ho_R1_Region.Dispose(); ho_R1_ImageReduced.Dispose(); ho_R1_Edges.Dispose(); ho_R1_ContoursSplit.Dispose(); ho_R1_SingleSegment.Dispose(); //ho_R1_ContEllipse.Dispose(); return mResult; }
// Procedures // External procedures // Chapter: Develop // Short Description: Open a new graphics window that preserves the aspect ratio of the given image. public void dev_open_window_fit_image(HObject ho_Image, HTuple hv_Row, HTuple hv_Column, HTuple hv_WidthLimit, HTuple hv_HeightLimit, out HTuple hv_WindowHandle) { // Local control variables HTuple hv_MinWidth = new HTuple(), hv_MaxWidth = new HTuple(); HTuple hv_MinHeight = new HTuple(), hv_MaxHeight = new HTuple(); HTuple hv_ResizeFactor, hv_ImageWidth, hv_ImageHeight; HTuple hv_TempWidth, hv_TempHeight, hv_WindowWidth, hv_WindowHeight; // Initialize local and output iconic variables //This procedure opens a new graphics window and adjusts the size //such that it fits into the limits specified by WidthLimit //and HeightLimit, but also maintains the correct image aspect ratio. // //If it is impossible to match the minimum and maximum extent requirements //at the same time (f.e. if the image is very long but narrow), //the maximum value gets a higher priority, // //Parse input tuple WidthLimit if ((int)((new HTuple((new HTuple(hv_WidthLimit.TupleLength())).TupleEqual(0))).TupleOr( new HTuple(hv_WidthLimit.TupleLess(0)))) != 0) { hv_MinWidth = 500; hv_MaxWidth = 800; } else if ((int)(new HTuple((new HTuple(hv_WidthLimit.TupleLength())).TupleEqual( 1))) != 0) { hv_MinWidth = 0; hv_MaxWidth = hv_WidthLimit.Clone(); } else { hv_MinWidth = hv_WidthLimit[0]; hv_MaxWidth = hv_WidthLimit[1]; } //Parse input tuple HeightLimit if ((int)((new HTuple((new HTuple(hv_HeightLimit.TupleLength())).TupleEqual(0))).TupleOr( new HTuple(hv_HeightLimit.TupleLess(0)))) != 0) { hv_MinHeight = 400; hv_MaxHeight = 600; } else if ((int)(new HTuple((new HTuple(hv_HeightLimit.TupleLength())).TupleEqual( 1))) != 0) { hv_MinHeight = 0; hv_MaxHeight = hv_HeightLimit.Clone(); } else { hv_MinHeight = hv_HeightLimit[0]; hv_MaxHeight = hv_HeightLimit[1]; } // //Test, if window size has to be changed. hv_ResizeFactor = 1; HOperatorSet.GetImageSize(ho_Image, out hv_ImageWidth, out hv_ImageHeight); //First, expand window to the minimum extents (if necessary). if ((int)((new HTuple(hv_MinWidth.TupleGreater(hv_ImageWidth))).TupleOr(new HTuple(hv_MinHeight.TupleGreater( hv_ImageHeight)))) != 0) { hv_ResizeFactor = (((((hv_MinWidth.TupleReal()) / hv_ImageWidth)).TupleConcat( (hv_MinHeight.TupleReal()) / hv_ImageHeight))).TupleMax(); } hv_TempWidth = hv_ImageWidth * hv_ResizeFactor; hv_TempHeight = hv_ImageHeight * hv_ResizeFactor; //Then, shrink window to maximum extents (if necessary). if ((int)((new HTuple(hv_MaxWidth.TupleLess(hv_TempWidth))).TupleOr(new HTuple(hv_MaxHeight.TupleLess( hv_TempHeight)))) != 0) { hv_ResizeFactor = hv_ResizeFactor * ((((((hv_MaxWidth.TupleReal()) / hv_TempWidth)).TupleConcat( (hv_MaxHeight.TupleReal()) / hv_TempHeight))).TupleMin()); } hv_WindowWidth = hv_ImageWidth * hv_ResizeFactor; hv_WindowHeight = hv_ImageHeight * hv_ResizeFactor; //Resize window HOperatorSet.SetWindowAttr("background_color", "black"); HOperatorSet.OpenWindow(hv_Row, hv_Column, hv_WindowWidth, hv_WindowHeight, 0, "", "", out hv_WindowHandle); HDevWindowStack.Push(hv_WindowHandle); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, hv_ImageHeight - 1, hv_ImageWidth - 1); } return; }
public MeasureResult Action() { #region 輸出結果 LineResult mResult = null; #endregion HObject ho_A2_Region = null; HObject ho_A2_Reduced = null, ho_A2Region = null, ho_A2_RegionBorder = null; HObject ho_A2_RegionDilation = null, ho_A2_Edges = null, ho_A2_Rectangles = null; HObject ho_A2Cross = null; // Local control variables HTuple hv_msgOffsetY, hv_msgOffsetX; //HTuple hv_AllModelId, hv_AllModelRow, hv_AllModelColumn; HTuple hv_STD_Row; HTuple hv_STD_Col, hv_Img_Row, hv_Img_Col, hv_Img_Rotate_Angle; HTuple hv_OffsetRow, hv_OffsetCol, hv_CL_X, hv_CL_Y, hv_STD_CL_1_Row; HTuple hv_STD_CL_1_Col, hv_STD_CL_1_V_Row, hv_STD_CL_1_V_Col; HTuple hv_CL_1_X, hv_CL_1_Y, hv_CL_1_Pos_Row, hv_CL_1_Pos_Col; HTuple hv_A2_Center_X, hv_A2_Center_Y, hv_STD_A2_1_1_Row; HTuple hv_STD_A2_1_1_Col, hv_STD_A2_1_1_V_Row, hv_STD_A2_1_1_V_Col; HTuple hv_A2_1_1_X, hv_A2_1_1_Y, hv_A2_1_1_Pos_Row, hv_A2_1_1_Pos_Col; HTuple hv_A2_ROI_W = new HTuple(), hv_A2_ROI_H = new HTuple(); HTuple hv_Rec_W = new HTuple(), hv_Rec_H = new HTuple(), hv_A2_Alpha = new HTuple(); HTuple hv_a2low = new HTuple(), hv_a2high = new HTuple(), hv_a2Limit = new HTuple(); HTuple hv_A2_RecNumber = new HTuple(), hv_A2Row = new HTuple(); HTuple hv_A2Column = new HTuple(), hv_A2Phi = new HTuple(); HTuple hv_A2Length1 = new HTuple(), hv_A2Length2 = new HTuple(); HTuple hv_A2PointOrder = new HTuple(), hv_A2Number = new HTuple(); HTuple hv_A2_Dist, hv_X1 = new HTuple(), hv_X2 = new HTuple(); HTuple hv_Y1 = new HTuple(), hv_Y2 = new HTuple(), hv_STD_Mark_A2_C_Row = new HTuple(); HTuple hv_STD_Mark_A2_C_Col = new HTuple(), hv_STD_Mark_A2_Start_Row = new HTuple(); HTuple hv_STD_Mark_A2_Start_Col = new HTuple(), hv_STD_Mark_A2_End_Row = new HTuple(); HTuple hv_STD_Mark_A2_End_Col = new HTuple(), hv_STD_Mark_A2_C_V_Row = new HTuple(); HTuple hv_STD_Mark_A2_C_V_Col = new HTuple(), hv_Mark_A2_C_X = new HTuple(); HTuple hv_Mark_A2_C_Y = new HTuple(), hv_Mark_A2_C_Pos_Row = new HTuple(); HTuple hv_Mark_A2_C_Pos_Col = new HTuple(), hv_STD_Mark_A2_Start_V_Row = new HTuple(); HTuple hv_STD_Mark_A2_Start_V_Col = new HTuple(), hv_Mark_A2_Start_X = new HTuple(); HTuple hv_Mark_A2_Start_Y = new HTuple(), hv_Mark_A2_Start_Pos_Row = new HTuple(); HTuple hv_Mark_A2_Start_Pos_Col = new HTuple(), hv_STD_Mark_A2_End_V_Row = new HTuple(); HTuple hv_STD_Mark_A2_End_V_Col = new HTuple(), hv_Mark_A2_End_X = new HTuple(); HTuple hv_Mark_A2_End_Y = new HTuple(), hv_Mark_A2_End_Pos_Row = new HTuple(); HTuple hv_Mark_A2_End_Pos_Col = new HTuple(); // Initialize local and output iconic variables //HOperatorSet.GenEmptyObj(out ho_Image); //HOperatorSet.GenEmptyObj(out ho_AllModelContours); HOperatorSet.GenEmptyObj(out ho_A2_Region); HOperatorSet.GenEmptyObj(out ho_A2_Reduced); HOperatorSet.GenEmptyObj(out ho_A2Region); HOperatorSet.GenEmptyObj(out ho_A2_RegionBorder); HOperatorSet.GenEmptyObj(out ho_A2_RegionDilation); HOperatorSet.GenEmptyObj(out ho_A2_Edges); HOperatorSet.GenEmptyObj(out ho_A2_Rectangles); HOperatorSet.GenEmptyObj(out ho_A2Cross); //Measure: SDMS_A2 //Author: John Hsieh //Date: 2012 //ho_Image.Dispose(); //HOperatorSet.ReadImage(out ho_Image, "Images/STD.bmp"); //dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle); // dev_update_off(...); only in hdevelop HOperatorSet.SetSystem("border_shape_models", "false"); //****Message Args hv_msgOffsetY = 100; hv_msgOffsetX = 100; ////****Model All //HOperatorSet.ReadShapeModel("D:/Projects/Halcon/SDMS/SDMS_Measure/Model/MatchingAll.shm", // out hv_AllModelId); //ho_AllModelContours.Dispose(); //HOperatorSet.GetShapeModelContours(out ho_AllModelContours, hv_AllModelId, 1); //HOperatorSet.FindShapeModel(ho_Image, hv_AllModelId, (new HTuple(0)).TupleRad() // , (new HTuple(360)).TupleRad(), 0.5, 1, 0.5, "least_squares", 6, 0.75, out hv_AllModelRow, // out hv_AllModelColumn, out hv_AllModelAngle, out hv_AllModelScore); //****Model Args //STD 中心點 hv_STD_Row = 772; hv_STD_Col = 1003; //目前圖形 中心點 hv_Img_Row = hv_AllModelRow.Clone(); hv_Img_Col = hv_AllModelColumn.Clone(); //目前圖形 Rotate Angle hv_Img_Rotate_Angle = hv_AllModelAngle.Clone(); //目前圖形偏移量 hv_OffsetRow = hv_Img_Row - hv_STD_Row; hv_OffsetCol = hv_Img_Col - hv_STD_Col; ////****Display //if (HDevWindowStack.IsOpen()) //{ // HOperatorSet.ClearWindow(HDevWindowStack.GetActive()); //} //if (HDevWindowStack.IsOpen()) //{ // HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); //} //*****A2 //step 1. //尋找中心點 CL model matching (CL_X, CL_Y) hv_CL_X = 0; hv_CL_Y = 0; //****CL_1 //STD CL_1_ 位置 hv_STD_CL_1_Row = 403; hv_STD_CL_1_Col = 1054; //STD 向量 STD_CL_1_ hv_STD_CL_1_V_Row = hv_STD_CL_1_Row - hv_STD_Row; hv_STD_CL_1_V_Col = hv_STD_CL_1_Col - hv_STD_Col; //CL_1_X, CL_1_Y 分量 hv_CL_1_X = (hv_STD_CL_1_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_CL_1_V_Row * (hv_Img_Rotate_Angle.TupleSin() )); hv_CL_1_Y = (hv_STD_CL_1_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_CL_1_V_Col * (hv_Img_Rotate_Angle.TupleSin() )); //目前圖形 CL_1_ 位置 hv_CL_1_Pos_Row = (hv_STD_Row + hv_CL_1_Y) + hv_OffsetRow; hv_CL_1_Pos_Col = (hv_STD_Col + hv_CL_1_X) + hv_OffsetCol; hv_CL_X = hv_CL_1_Pos_Col.Clone(); hv_CL_Y = hv_CL_1_Pos_Row.Clone(); //Step 1 //找左邊數來第一個金手指的中心點 Finger hv_A2_Center_X = 0; hv_A2_Center_Y = 0; //****A2_1_1 //STD A2_1_1_ 位置 hv_STD_A2_1_1_Row = 410; hv_STD_A2_1_1_Col = 780; //STD 向量 STD_A2_1_1_ hv_STD_A2_1_1_V_Row = hv_STD_A2_1_1_Row - hv_STD_Row; hv_STD_A2_1_1_V_Col = hv_STD_A2_1_1_Col - hv_STD_Col; //A2_1_1_X, A2_1_1_Y 分量 hv_A2_1_1_X = (hv_STD_A2_1_1_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_A2_1_1_V_Row * (hv_Img_Rotate_Angle.TupleSin() )); hv_A2_1_1_Y = (hv_STD_A2_1_1_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_A2_1_1_V_Col * (hv_Img_Rotate_Angle.TupleSin() )); //目前圖形 A2_1_1_ 位置 hv_A2_1_1_Pos_Row = (hv_STD_Row + hv_A2_1_1_Y) + hv_OffsetRow; hv_A2_1_1_Pos_Col = (hv_STD_Col + hv_A2_1_1_X) + hv_OffsetCol; //for (hv_MatchingObjIdx = 0; (int)hv_MatchingObjIdx <= (int)((new HTuple(hv_AllModelScore.TupleLength() // )) - 1); hv_MatchingObjIdx = (int)hv_MatchingObjIdx + 1) //{ //A2_ROI hv_A2_ROI_W = 40; hv_A2_ROI_H = 120; ho_A2_Region.Dispose(); HOperatorSet.GenRectangle2(out ho_A2_Region, hv_A2_1_1_Pos_Row, hv_A2_1_1_Pos_Col, hv_Img_Rotate_Angle, hv_A2_ROI_W, hv_A2_ROI_H); //stop () ho_A2_Reduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_A2_Region, out ho_A2_Reduced); //fit_Rectangle //if (HDevWindowStack.IsOpen()) //{ // //dev_display (Image) //} //if (HDevWindowStack.IsOpen()) //{ // //dev_display (A2_Region) //} //stop () // ho_A2Region.Dispose(); HOperatorSet.FastThreshold(ho_A2_Reduced, out ho_A2Region, 50, 150, 20); ho_A2_RegionBorder.Dispose(); HOperatorSet.Boundary(ho_A2Region, out ho_A2_RegionBorder, "inner"); if (HDevWindowStack.IsOpen()) { //dev_display (A2_RegionBorder) } hv_Rec_W = 11; hv_Rec_H = 11; ho_A2_RegionDilation.Dispose(); HOperatorSet.DilationRectangle1(ho_A2_RegionBorder, out ho_A2_RegionDilation, hv_Rec_W, hv_Rec_H); hv_A2_Alpha = 0.9; hv_a2low = 20; hv_a2high = 110; ho_A2_Edges.Dispose(); HOperatorSet.EdgesSubPix(ho_A2_Reduced, out ho_A2_Edges, "canny", hv_A2_Alpha, hv_a2low, hv_a2high); //stop () hv_a2Limit = 200; ho_A2_Rectangles.Dispose(); HOperatorSet.SelectShapeXld(ho_A2_Edges, out ho_A2_Rectangles, "contlength", "and", hv_a2Limit, 99999); HOperatorSet.CountObj(ho_A2_Rectangles, out hv_A2_RecNumber); while ((int)(new HTuple(hv_A2_RecNumber.TupleGreater(1))) != 0) { hv_a2Limit = hv_a2Limit + 10; ho_A2_Rectangles.Dispose(); HOperatorSet.SelectShapeXld(ho_A2_Edges, out ho_A2_Rectangles, "contlength", "and", hv_a2Limit, 99999); HOperatorSet.CountObj(ho_A2_Rectangles, out hv_A2_RecNumber); } //stop () HOperatorSet.FitRectangle2ContourXld(ho_A2_Rectangles, "regression", -1, 0, 0, 3, 2, out hv_A2Row, out hv_A2Column, out hv_A2Phi, out hv_A2Length1, out hv_A2Length2, out hv_A2PointOrder); HOperatorSet.CountObj(ho_A2_Rectangles, out hv_A2Number); //取A2 if ((int)(new HTuple(hv_A2Number.TupleGreater(0))) != 0) { hv_A2_Center_X = hv_A2Column[0]; hv_A2_Center_Y = hv_A2Row[0]; ho_A2Cross.Dispose(); HOperatorSet.GenCrossContourXld(out ho_A2Cross, hv_A2Row, hv_A2Column, 10, 0); //if (HDevWindowStack.IsOpen()) //{ // HOperatorSet.DispObj(ho_A2Cross, HDevWindowStack.GetActive()); //} //stop () } //} HOperatorSet.DistancePp(hv_A2_Center_Y, hv_A2_Center_X, hv_CL_Y, hv_CL_X, out hv_A2_Dist); mResult = new LineResult(hv_A2_Center_Y, hv_A2_Center_X, hv_CL_Y, hv_CL_X, hv_A2_Dist); //step 3 //show Result //hv_A2_Dist = 0; //if ((int)((new HTuple(hv_A2_Center_X.TupleGreater(0))).TupleAnd(new HTuple(hv_A2_Center_Y.TupleGreater( // 0)))) != 0) //{ // //轉正 // hv_X1 = (hv_A2_Center_X * (hv_AllModelAngle.TupleCos())) - (hv_A2_Center_Y * (hv_AllModelAngle.TupleSin() // )); // hv_X2 = (hv_CL_X * (hv_AllModelAngle.TupleCos())) - (hv_CL_Y * (hv_AllModelAngle.TupleSin() // )); // hv_Y1 = (hv_A2_Center_X * (hv_AllModelAngle.TupleSin())) + (hv_A2_Center_Y * (hv_AllModelAngle.TupleCos() // )); // hv_Y2 = (hv_CL_X * (hv_AllModelAngle.TupleSin())) + (hv_CL_Y * (hv_AllModelAngle.TupleCos() // )); // //disp_cross (WindowHandle, CL_Y, CL_X, 10, 0) // //disp_cross (WindowHandle, A2_Center_Y, A2_Center_X, 10, 0) // hv_A2_Dist = ((hv_X1 - hv_X2)).TupleAbs(); // //****Mark_A2_C // //STD Mark_A2_C_ 位置 // hv_STD_Mark_A2_C_Row = 420; // hv_STD_Mark_A2_C_Col = 890; // //STD Mark_A2_Start_ 位置 // hv_STD_Mark_A2_Start_Row = 420; // hv_STD_Mark_A2_Start_Col = 780; // //STD Mark_A2_End_ 位置 // hv_STD_Mark_A2_End_Row = 420; // hv_STD_Mark_A2_End_Col = 1055; // //STD 向量 STD_Mark_A2_C_ // hv_STD_Mark_A2_C_V_Row = hv_STD_Mark_A2_C_Row - hv_STD_Row; // hv_STD_Mark_A2_C_V_Col = hv_STD_Mark_A2_C_Col - hv_STD_Col; // //Mark_A2_C_X, Mark_A2_C_Y 分量 // hv_Mark_A2_C_X = (hv_STD_Mark_A2_C_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_Mark_A2_C_V_Row * (hv_Img_Rotate_Angle.TupleSin() // )); // hv_Mark_A2_C_Y = (hv_STD_Mark_A2_C_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_Mark_A2_C_V_Col * (hv_Img_Rotate_Angle.TupleSin() // )); // //目前圖形 Mark_A2_C_ 位置 // hv_Mark_A2_C_Pos_Row = (hv_STD_Row + hv_Mark_A2_C_Y) + hv_OffsetRow; // hv_Mark_A2_C_Pos_Col = (hv_STD_Col + hv_Mark_A2_C_X) + hv_OffsetCol; // //STD 向量 STD_Mark_A2_Start_ // hv_STD_Mark_A2_Start_V_Row = hv_STD_Mark_A2_Start_Row - hv_STD_Row; // hv_STD_Mark_A2_Start_V_Col = hv_STD_Mark_A2_Start_Col - hv_STD_Col; // //Mark_A2_Start_X, Mark_A2_Start_Y 分量 // hv_Mark_A2_Start_X = (hv_STD_Mark_A2_Start_V_Col * (hv_Img_Rotate_Angle.TupleCos() // )) + (hv_STD_Mark_A2_Start_V_Row * (hv_Img_Rotate_Angle.TupleSin())); // hv_Mark_A2_Start_Y = (hv_STD_Mark_A2_Start_V_Row * (hv_Img_Rotate_Angle.TupleCos() // )) - (hv_STD_Mark_A2_Start_V_Col * (hv_Img_Rotate_Angle.TupleSin())); // //目前圖形 Mark_A2_Start_ 位置 // hv_Mark_A2_Start_Pos_Row = (hv_STD_Row + hv_Mark_A2_Start_Y) + hv_OffsetRow; // hv_Mark_A2_Start_Pos_Col = (hv_STD_Col + hv_Mark_A2_Start_X) + hv_OffsetCol; // //STD 向量 STD_Mark_A2_End_ // hv_STD_Mark_A2_End_V_Row = hv_STD_Mark_A2_End_Row - hv_STD_Row; // hv_STD_Mark_A2_End_V_Col = hv_STD_Mark_A2_End_Col - hv_STD_Col; // //Mark_A2_End_X, Mark_A2_End_Y 分量 // hv_Mark_A2_End_X = (hv_STD_Mark_A2_End_V_Col * (hv_Img_Rotate_Angle.TupleCos() // )) + (hv_STD_Mark_A2_End_V_Row * (hv_Img_Rotate_Angle.TupleSin())); // hv_Mark_A2_End_Y = (hv_STD_Mark_A2_End_V_Row * (hv_Img_Rotate_Angle.TupleCos() // )) - (hv_STD_Mark_A2_End_V_Col * (hv_Img_Rotate_Angle.TupleSin())); // //目前圖形 Mark_A2_End_ 位置 // hv_Mark_A2_End_Pos_Row = (hv_STD_Row + hv_Mark_A2_End_Y) + hv_OffsetRow; // hv_Mark_A2_End_Pos_Col = (hv_STD_Col + hv_Mark_A2_End_X) + hv_OffsetCol; // //HOperatorSet.SetTposition(hv_WindowHandle, hv_Mark_A2_C_Pos_Row - 25, hv_Mark_A2_C_Pos_Col); // //HOperatorSet.WriteString(hv_WindowHandle, "A2"); // //HOperatorSet.DispArrow(hv_WindowHandle, hv_Mark_A2_C_Pos_Row, hv_Mark_A2_C_Pos_Col - 50, // // hv_Mark_A2_Start_Pos_Row, hv_Mark_A2_Start_Pos_Col, 5); // //HOperatorSet.DispArrow(hv_WindowHandle, hv_Mark_A2_C_Pos_Row, hv_Mark_A2_C_Pos_Col + 50, // // hv_Mark_A2_End_Pos_Row, hv_Mark_A2_End_Pos_Col, 5); //} //hv_MeasureReasult = hv_A2_Dist.Clone(); //*****A2 End //ho_Image.Dispose(); //ho_AllModelContours.Dispose(); ho_A2_Region.Dispose(); ho_A2_Reduced.Dispose(); ho_A2Region.Dispose(); ho_A2_RegionBorder.Dispose(); ho_A2_RegionDilation.Dispose(); ho_A2_Edges.Dispose(); ho_A2_Rectangles.Dispose(); ho_A2Cross.Dispose(); return mResult; }