public bool FindRegion(HObject srcImg, RectangleF serchArea, out int area, out int contLength, out double centerX, out double centerY) { contLength = 0; area = 0; centerX = 0; centerY = 0; System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); try { HTuple row1, row2, column1, column2, regionArea; HTuple mean, deviation, number, length; HObject imgReduced, targetRegion; row1 = serchArea.Y; row2 = serchArea.Y + serchArea.Height; column1 = serchArea.X; column2 = serchArea.X + serchArea.Width; HObject region, grayRegion, selectedRegion, connectedRegion, regionBorder; HOperatorSet.GenRectangle1(out region, row1, column1, row2, column2); HOperatorSet.ReduceDomain(srcImg, region, out imgReduced); HOperatorSet.Intensity(region, imgReduced, out mean, out deviation); mean = mean + 20; mean = mean < 200 ? mean : new HTuple(200); HOperatorSet.Threshold(imgReduced, out grayRegion, 0, mean); HOperatorSet.Connection(grayRegion, out connectedRegion); HOperatorSet.SelectShape(connectedRegion, out selectedRegion, "area", "and", 250000, 1000000); HOperatorSet.CountObj(selectedRegion, out number); connectedRegion.Dispose(); grayRegion.Dispose(); imgReduced.Dispose(); if (number.I == 1) { HOperatorSet.SelectObj(selectedRegion, out targetRegion, 1); HOperatorSet.AreaCenter(targetRegion, out regionArea, out row1, out column1); HOperatorSet.Boundary(targetRegion, out regionBorder, "inner"); HOperatorSet.Contlength(regionBorder, out length); area = (int)regionArea.D; contLength = (int)length.D; centerX = column1.D; centerY = row1.D; selectedRegion.Dispose(); targetRegion.Dispose(); regionBorder.Dispose(); } else { Logger.PopError("轮廓长度算法异常,轮廓数量=" + number.ToString()); return(false); } } catch (Exception ex) { Logger.PopError("轮廓长度算法异常:" + ex.Message.ToString()); throw; } return(true); }
private void getRectCenter() { // Local iconic variables HObject ho_image, ho_Region, ho_RegionTrans; HObject ho_RegionBorder; // Local control variables HTuple hv_Width = null, hv_Height = null, hv_WindowHandle = hWindowControl1.HalconWindow; HTuple hv_Area = null, hv_Row = null, hv_Column = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_image); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_RegionTrans); HOperatorSet.GenEmptyObj(out ho_RegionBorder); //if (HDevWindowStack.IsOpen()) //{ // HOperatorSet.CloseWindow(HDevWindowStack.Pop()); //} //ho_image.Dispose(); image_path = "G:/blazarlin/my_halcon_function/rect.png"; HOperatorSet.ReadImage(out ho_image, image_path); HOperatorSet.GetImageSize(ho_image, out hv_Width, out hv_Height); HOperatorSet.SetWindowAttr("background_color", "black"); //HOperatorSet.OpenWindow(0, 0, hv_Width / 1.2, hv_Height / 1.22, 0, "visible", "", out hv_WindowHandle); HDevWindowStack.Push(hv_WindowHandle); //阈值 //ho_Region.Dispose(); HOperatorSet.Threshold(ho_image, out ho_Region, 128, 255); // 外接矩形 // ho_RegionTrans.Dispose(); HOperatorSet.ShapeTrans(ho_Region, out ho_RegionTrans, "rectangle1"); // 获取中心 HOperatorSet.AreaCenter(ho_RegionTrans, out hv_Area, out hv_Row, out hv_Column); // 外边界 // ho_RegionBorder.Dispose(); HOperatorSet.Boundary(ho_RegionTrans, out ho_RegionBorder, "outer"); HOperatorSet.DispObj(ho_image, HDevWindowStack.GetActive()); // 显示边界 // 设置绘制器的颜色 HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red"); HOperatorSet.DispCross(hv_WindowHandle, hv_Row, hv_Column, 6, 0); HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green"); HOperatorSet.DispObj(ho_RegionBorder, HDevWindowStack.GetActive()); HOperatorSet.SetTposition(hv_WindowHandle, hv_Row, hv_Column); HOperatorSet.WriteString(hv_WindowHandle, (hv_Row + new HTuple(",")) + hv_Column); //HOperatorSet.DispObj(ho_RegionBorder, hWindowControl1.HalconWindow); ho_image.Dispose(); ho_Region.Dispose(); ho_RegionTrans.Dispose(); ho_RegionBorder.Dispose(); }
// Main procedure private void action() { // Local iconic variables HObject ho_Image, ho_Domain, ho_ImageReduced; HObject ho_FoundRegion, ho_RegionBorder; // Local control variables HTuple hv_Width = null, hv_Height = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_Domain); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_FoundRegion); HOperatorSet.GenEmptyObj(out ho_RegionBorder); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin"); } ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, "2014-12-24_22.27.49_B_Hole_Left-1.tif"); HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); ho_Domain.Dispose(); HOperatorSet.GetDomain(ho_Image, out ho_Domain); ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_Domain, out ho_ImageReduced); ho_FoundRegion.Dispose(); GetRegionByGrayAndArea(ho_ImageReduced, out ho_FoundRegion, 7, hv_Width, hv_Height, 3, 0, 180, 60000, 99999999, 5, 2); ho_RegionBorder.Dispose(); HOperatorSet.Boundary(ho_FoundRegion, out ho_RegionBorder, "inner"); // stop(); only in hdevelop ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_FoundRegion, out ho_ImageReduced); // stop(); only in hdevelop ho_Image.Dispose(); ho_Domain.Dispose(); ho_ImageReduced.Dispose(); ho_FoundRegion.Dispose(); ho_RegionBorder.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); }
public MeasureResult Action() { #region 輸出結果 CircleResult mResult = null; #endregion // Local iconic variables HObject ho_R6_Circle = null; HObject ho_R6_ROI_Image = null, ho_R6_Region = null, ho_R6_RegionBorder = null; HObject ho_R6_RegionDilation = null, ho_R6_ImageReduced = null; HObject ho_R6_Edges = null, ho_R6_ContoursSplit = null, ho_R6_SingleSegment = null; HObject ho_R6_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_R6; HTuple hv_STD_R6_Row = new HTuple(), hv_STD_R6_Col = new HTuple(); HTuple hv_STD_R6_V_Row = new HTuple(), hv_STD_R6_V_Col = new HTuple(); HTuple hv_R6_X = new HTuple(), hv_R6_Y = new HTuple(), hv_R6_Pos_Row = new HTuple(); HTuple hv_R6_Pos_Col = new HTuple(), hv_R6_R = new HTuple(); HTuple hv_R6_R_Max = new HTuple(), hv_R6_R_Inc = new HTuple(); HTuple hv_R6_NumSegments = new HTuple(), hv_NumCircles = new HTuple(); HTuple hv_Num_Circle_Point = new HTuple(), hv_i = new HTuple(); HTuple hv_Attrib = new HTuple(), hv_R6_Row = new HTuple(); HTuple hv_R6_Column = new HTuple(), hv_R6_Radius = new HTuple(); HTuple hv_R6_StartPhi = new HTuple(), hv_R6_EndPhi = new HTuple(); HTuple hv_R6_PointOrder = new HTuple(), hv_R6_MinDist = new HTuple(); HTuple hv_R6_MaxDist = new HTuple(), hv_R6_AvgDist = new HTuple(); HTuple hv_R6_SigmaDist = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_R6_Circle); HOperatorSet.GenEmptyObj(out ho_R6_ROI_Image); HOperatorSet.GenEmptyObj(out ho_R6_Region); HOperatorSet.GenEmptyObj(out ho_R6_RegionBorder); HOperatorSet.GenEmptyObj(out ho_R6_RegionDilation); HOperatorSet.GenEmptyObj(out ho_R6_ImageReduced); HOperatorSet.GenEmptyObj(out ho_R6_Edges); HOperatorSet.GenEmptyObj(out ho_R6_ContoursSplit); HOperatorSet.GenEmptyObj(out ho_R6_SingleSegment); HOperatorSet.GenEmptyObj(out ho_R6_ContEllipse); //Measure: SDMS_R6 //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()); } //*****R6 hv_R6 = 999; //STD R6_ 位置 hv_STD_R6_Row = 1292; hv_STD_R6_Col = 639; //STD 向量 STD_R6_ hv_STD_R6_V_Row = hv_STD_R6_Row - hv_STD_Row; hv_STD_R6_V_Col = hv_STD_R6_Col - hv_STD_Col; //R6_X, R6_Y 分量 hv_R6_X = (hv_STD_R6_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_R6_V_Row * (hv_Img_Rotate_Angle.TupleSin() )); hv_R6_Y = (hv_STD_R6_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_R6_V_Col * (hv_Img_Rotate_Angle.TupleSin() )); //目前圖形 R4 位置 hv_R6_Pos_Row = (hv_STD_Row + hv_R6_Y) + hv_OffsetRow; hv_R6_Pos_Col = (hv_STD_Col + hv_R6_X) + hv_OffsetCol; //R6_Region 由半徑 29 開始搜尋, 最大搜尋至 41, Inc =2 hv_R6_R = 29; hv_R6_R_Max = 41; hv_R6_R_Inc = 2; while ((int)((new HTuple(hv_R6.TupleEqual(999))).TupleAnd(new HTuple(hv_R6_R.TupleLess( hv_R6_R_Max)))) != 0) { //******************************* ho_R6_Circle.Dispose(); HOperatorSet.GenCircle(out ho_R6_Circle, hv_R6_Pos_Row, hv_R6_Pos_Col, hv_R6_R); ho_R6_ROI_Image.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_R6_Circle, out ho_R6_ROI_Image); ho_R6_Region.Dispose(); HOperatorSet.Threshold(ho_R6_ROI_Image, out ho_R6_Region, 100, 255); ho_R6_RegionBorder.Dispose(); HOperatorSet.Boundary(ho_R6_Region, out ho_R6_RegionBorder, "inner"); ho_R6_RegionDilation.Dispose(); HOperatorSet.DilationCircle(ho_R6_RegionBorder, out ho_R6_RegionDilation, 1.5); ho_R6_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_R6_ROI_Image, ho_R6_RegionDilation, out ho_R6_ImageReduced ); ho_R6_Edges.Dispose(); HOperatorSet.EdgesSubPix(ho_R6_ImageReduced, out ho_R6_Edges, "lanser2", 0.3, 40, 90); //fast_threshold (R6_ROI_Image, R6_Region, 80, 255, 15) //reduce_domain (R6_ROI_Image, R6_Region, R6_ImageReduced) //sobel_fast 具有較寬的選擇範圍,搭配 alpha 參數 (alpha 越大, 容錯範圍大) //alpha := 10 //R6_low := 10 //R6_high := 60 //edges_sub_pix (R6_ImageReduced, R6_Edges, 'sobel_fast', alpha, R6_low, R6_high) //stop () //*所有的數值越小,表示容錯範圍大,反之亦然 ho_R6_ContoursSplit.Dispose(); HOperatorSet.SegmentContoursXld(ho_R6_Edges, out ho_R6_ContoursSplit, "lines_circles", 6, 4, 4); //Display the results //=========================================================== HOperatorSet.CountObj(ho_R6_ContoursSplit, out hv_R6_NumSegments); hv_NumCircles = 0; hv_Num_Circle_Point = 0; for (hv_i = 1; hv_i.Continue(hv_R6_NumSegments, 1); hv_i = hv_i.TupleAdd(1)) { ho_R6_SingleSegment.Dispose(); HOperatorSet.SelectObj(ho_R6_ContoursSplit, out ho_R6_SingleSegment, hv_i); HOperatorSet.GetContourGlobalAttribXld(ho_R6_SingleSegment, "cont_approx", out hv_Attrib); if ((int)(new HTuple(hv_Attrib.TupleEqual(1))) != 0) { HOperatorSet.FitCircleContourXld(ho_R6_SingleSegment, "atukey", -1, 2, hv_Num_Circle_Point, 5, 2, out hv_R6_Row, out hv_R6_Column, out hv_R6_Radius, out hv_R6_StartPhi, out hv_R6_EndPhi, out hv_R6_PointOrder); ho_R6_ContEllipse.Dispose(); HOperatorSet.GenEllipseContourXld(out ho_R6_ContEllipse, hv_R6_Row, hv_R6_Column, 0, hv_R6_Radius, hv_R6_Radius, 0, (new HTuple(360)).TupleRad(), "positive", 1.0); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_R6_ContEllipse, HDevWindowStack.GetActive() ); } HOperatorSet.DistEllipseContourXld(ho_R6_SingleSegment, "algebraic", -1, 0, hv_R6_Row, hv_R6_Column, 0, hv_R6_Radius, hv_R6_Radius, out hv_R6_MinDist, out hv_R6_MaxDist, out hv_R6_AvgDist, out hv_R6_SigmaDist); hv_NumCircles = hv_NumCircles + 1; if ((int)(new HTuple(hv_R6.TupleGreater(hv_R6_Radius))) != 0) { hv_R6 = hv_R6_Radius.Clone(); mResult = new CircleResult() { Row = new HTuple(hv_R6_Row), Col = new HTuple(hv_R6_Column), Radius = new HTuple(hv_R6_Radius), StartPhi = new HTuple(hv_R6_StartPhi), EndPhi = new HTuple(hv_R6_EndPhi), PointOrder = new HTuple(hv_R6_PointOrder), }; } } } hv_R6_R = hv_R6_R + hv_R6_R_Inc; } ho_R6_Circle.Dispose(); ho_R6_ROI_Image.Dispose(); ho_R6_Region.Dispose(); ho_R6_RegionBorder.Dispose(); ho_R6_RegionDilation.Dispose(); ho_R6_ImageReduced.Dispose(); ho_R6_Edges.Dispose(); ho_R6_ContoursSplit.Dispose(); ho_R6_SingleSegment.Dispose(); ho_R6_ContEllipse.Dispose(); return(mResult); }
/// <summary> /// Extracts the calibration plate and the marks on this plate /// for the supplied image /// </summary> public void find_caltab_edges(HObject ho_Image, out HObject ho_Caltab, out HObject ho_Marks, HTuple hv_DescriptionFileName) { // Stack for temporary objects HObject[] OTemp = new HObject[20]; long SP_O = 0; // Local iconic variables HObject ho_ImageMean, ho_RegionDynThresh, ho_RegionBorder; HObject ho_RegionOpening1, ho_ConnectedRegions1, ho_SelectedRegions4; HObject ho_SelectedRegions5, ho_RegionBorder2, ho_RegionTrans; HObject ho_RegionErosion, ho_RegionBorder1, ho_RegionDilation2; HObject ho_RegionDifference1, ho_RegionOpening, ho_ConnectedRegions; HObject ho_SelectedRegions2, ho_SelectedRegions, ho_RegionFillUp; HObject ho_SelectedRegions1, ho_RegionIntersection, ho_RegionFillUp1; HObject ho_RegionDifference, ho_CaltabCandidates, ho_ObjectSelected = null; HObject ho_ConnectedMarks = null, ho_ObjectSelectedCaltab = null; HObject ho_RegionFillUpCand, ho_MarksCand, ho_RegionDilation1; HObject ho_ImageReduced, ho_DefaultEdges, ho_UnionContours; HObject ho_SelectedXLD, ho_SelectedXLD1; // Local control variables HTuple hv_ExpDefaultCtrlDummyVar, hv_Width; HTuple hv_Height, hv_EstimatedCaltabSize, hv_EstimatedMarkSize; HTuple hv_Number, hv_X, hv_Y, hv_Z, hv_NumDescrMarks, hv_Index; HTuple hv_NumberMarks = new HTuple(), hv_Anisometry = new HTuple(); HTuple hv_Bulkiness = new HTuple(), hv_StructureFactor = new HTuple(); HTuple hv_AreaMarks = new HTuple(), hv_Row = new HTuple(); HTuple hv_Column = new HTuple(), hv_Rectangularity, hv_SortedIndex; HTuple hv_IndexBest, hv_MinContrast, hv_NumberCand, hv_Area; HTuple hv_Dummy, hv_DummyS, hv_AreaMedian; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Caltab); HOperatorSet.GenEmptyObj(out ho_Marks); HOperatorSet.GenEmptyObj(out ho_ImageMean); HOperatorSet.GenEmptyObj(out ho_RegionDynThresh); HOperatorSet.GenEmptyObj(out ho_RegionBorder); HOperatorSet.GenEmptyObj(out ho_RegionOpening1); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1); HOperatorSet.GenEmptyObj(out ho_SelectedRegions4); HOperatorSet.GenEmptyObj(out ho_SelectedRegions5); HOperatorSet.GenEmptyObj(out ho_RegionBorder2); HOperatorSet.GenEmptyObj(out ho_RegionTrans); HOperatorSet.GenEmptyObj(out ho_RegionErosion); HOperatorSet.GenEmptyObj(out ho_RegionBorder1); HOperatorSet.GenEmptyObj(out ho_RegionDilation2); HOperatorSet.GenEmptyObj(out ho_RegionDifference1); HOperatorSet.GenEmptyObj(out ho_RegionOpening); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions2); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_RegionFillUp); HOperatorSet.GenEmptyObj(out ho_SelectedRegions1); HOperatorSet.GenEmptyObj(out ho_RegionIntersection); HOperatorSet.GenEmptyObj(out ho_RegionFillUp1); HOperatorSet.GenEmptyObj(out ho_RegionDifference); HOperatorSet.GenEmptyObj(out ho_CaltabCandidates); HOperatorSet.GenEmptyObj(out ho_ObjectSelected); HOperatorSet.GenEmptyObj(out ho_ConnectedMarks); HOperatorSet.GenEmptyObj(out ho_ObjectSelectedCaltab); HOperatorSet.GenEmptyObj(out ho_RegionFillUpCand); HOperatorSet.GenEmptyObj(out ho_MarksCand); HOperatorSet.GenEmptyObj(out ho_RegionDilation1); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_DefaultEdges); HOperatorSet.GenEmptyObj(out ho_UnionContours); HOperatorSet.GenEmptyObj(out ho_SelectedXLD); HOperatorSet.GenEmptyObj(out ho_SelectedXLD1); // ho_Marks.Dispose(); HOperatorSet.GenEmptyObj(out ho_Marks); ho_Caltab.Dispose(); HOperatorSet.GenEmptyObj(out ho_Caltab); HOperatorSet.GetImagePointer1(ho_Image, out hv_ExpDefaultCtrlDummyVar, out hv_ExpDefaultCtrlDummyVar, out hv_Width, out hv_Height); hv_EstimatedCaltabSize = (((((hv_Width.TupleConcat(hv_Height))).TupleMax()) / 2.5)).TupleRound() ; hv_EstimatedMarkSize = ((hv_EstimatedCaltabSize / 12.0)).TupleRound(); ho_ImageMean.Dispose(); HOperatorSet.MeanImage(ho_Image, out ho_ImageMean, hv_EstimatedMarkSize * 3, hv_EstimatedMarkSize * 3); ho_RegionDynThresh.Dispose(); HOperatorSet.DynThreshold(ho_Image, ho_ImageMean, out ho_RegionDynThresh, 3, "light"); ho_RegionBorder.Dispose(); HOperatorSet.DynThreshold(ho_Image, ho_ImageMean, out ho_RegionBorder, 20, "dark"); ho_RegionOpening1.Dispose(); HOperatorSet.OpeningCircle(ho_RegionBorder, out ho_RegionOpening1, 1.5); ho_ConnectedRegions1.Dispose(); HOperatorSet.Connection(ho_RegionOpening1, out ho_ConnectedRegions1); ho_SelectedRegions4.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions1, out ho_SelectedRegions4, "compactness", "and", 17, 50); ho_SelectedRegions5.Dispose(); HOperatorSet.SelectShape(ho_SelectedRegions4, out ho_SelectedRegions5, "anisometry", "and", 1, 1.4); ho_RegionBorder2.Dispose(); HOperatorSet.Boundary(ho_SelectedRegions5, out ho_RegionBorder2, "outer"); ho_SelectedRegions5.Dispose(); HOperatorSet.SelectShape(ho_RegionBorder2, out ho_SelectedRegions5, "circularity", "and", 0.006, 1); ho_RegionTrans.Dispose(); HOperatorSet.ShapeTrans(ho_SelectedRegions5, out ho_RegionTrans, "rectangle2"); ho_RegionErosion.Dispose(); HOperatorSet.ErosionCircle(ho_RegionTrans, out ho_RegionErosion, (hv_Width / 640.0) * 5.5); ho_RegionBorder1.Dispose(); HOperatorSet.Boundary(ho_RegionErosion, out ho_RegionBorder1, "inner"); ho_RegionDilation2.Dispose(); HOperatorSet.DilationCircle(ho_RegionBorder1, out ho_RegionDilation2, 3.5); ho_RegionDifference1.Dispose(); HOperatorSet.Difference(ho_RegionDynThresh, ho_RegionDilation2, out ho_RegionDifference1 ); ho_RegionOpening.Dispose(); HOperatorSet.OpeningCircle(ho_RegionDifference1, out ho_RegionOpening, (hv_Width / 640) * 1.5); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_RegionOpening, out ho_ConnectedRegions); ho_SelectedRegions2.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions2, "area", "and", (hv_EstimatedCaltabSize.TuplePow(2)) / 10, (hv_EstimatedCaltabSize.TuplePow( 2)) * 5); ho_SelectedRegions.Dispose(); HOperatorSet.SelectShape(ho_SelectedRegions2, out ho_SelectedRegions, "compactness", "and", 1.4, 10); ho_RegionFillUp.Dispose(); HOperatorSet.FillUp(ho_SelectedRegions, out ho_RegionFillUp); ho_SelectedRegions1.Dispose(); HOperatorSet.SelectShape(ho_RegionFillUp, out ho_SelectedRegions1, "rectangularity", "and", 0.6, 1); ho_RegionIntersection.Dispose(); HOperatorSet.Intersection(ho_SelectedRegions1, ho_RegionDynThresh, out ho_RegionIntersection ); ho_RegionFillUp1.Dispose(); HOperatorSet.FillUp(ho_RegionIntersection, out ho_RegionFillUp1); ho_RegionDifference.Dispose(); HOperatorSet.Difference(ho_RegionFillUp1, ho_RegionIntersection, out ho_RegionDifference ); HOperatorSet.CountObj(ho_RegionDifference, out hv_Number); ho_CaltabCandidates.Dispose(); HOperatorSet.GenEmptyObj(out ho_CaltabCandidates); HOperatorSet.CaltabPoints(hv_DescriptionFileName, out hv_X, out hv_Y, out hv_Z); hv_NumDescrMarks = new HTuple(hv_X.TupleLength()); for (hv_Index = 1; hv_Index.Continue(hv_Number, 1); hv_Index = hv_Index.TupleAdd(1)) { ho_ObjectSelected.Dispose(); HOperatorSet.SelectObj(ho_RegionDifference, out ho_ObjectSelected, hv_Index); ho_ConnectedMarks.Dispose(); HOperatorSet.Connection(ho_ObjectSelected, out ho_ConnectedMarks); HOperatorSet.CountObj(ho_ConnectedMarks, out hv_NumberMarks); HOperatorSet.Eccentricity(ho_ConnectedMarks, out hv_Anisometry, out hv_Bulkiness, out hv_StructureFactor); HOperatorSet.AreaCenter(ho_ConnectedMarks, out hv_AreaMarks, out hv_Row, out hv_Column); ho_ObjectSelectedCaltab.Dispose(); HOperatorSet.SelectObj(ho_RegionIntersection, out ho_ObjectSelectedCaltab, hv_Index); if ((int)((new HTuple((new HTuple((new HTuple(hv_NumberMarks.TupleGreaterEqual( 10))).TupleAnd(new HTuple(hv_NumberMarks.TupleLess(hv_NumDescrMarks + 20))))).TupleAnd( new HTuple(((((hv_Anisometry.TupleSort())).TupleSelect((new HTuple(hv_Anisometry.TupleLength() )) / 2))).TupleLess(2))))).TupleAnd(new HTuple((new HTuple(hv_AreaMarks.TupleMean() )).TupleGreater(20)))) != 0) { OTemp[SP_O] = ho_CaltabCandidates.CopyObj(1, -1); SP_O++; ho_CaltabCandidates.Dispose(); HOperatorSet.ConcatObj(OTemp[SP_O - 1], ho_ObjectSelectedCaltab, out ho_CaltabCandidates ); OTemp[SP_O - 1].Dispose(); SP_O = 0; } } ho_RegionFillUpCand.Dispose(); HOperatorSet.FillUp(ho_CaltabCandidates, out ho_RegionFillUpCand); HOperatorSet.Rectangularity(ho_RegionFillUpCand, out hv_Rectangularity); if ((int)(new HTuple((new HTuple(hv_Rectangularity.TupleLength())).TupleEqual( 0))) != 0) { ho_ImageMean.Dispose(); ho_RegionDynThresh.Dispose(); ho_RegionBorder.Dispose(); ho_RegionOpening1.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions4.Dispose(); ho_SelectedRegions5.Dispose(); ho_RegionBorder2.Dispose(); ho_RegionTrans.Dispose(); ho_RegionErosion.Dispose(); ho_RegionBorder1.Dispose(); ho_RegionDilation2.Dispose(); ho_RegionDifference1.Dispose(); ho_RegionOpening.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions2.Dispose(); ho_SelectedRegions.Dispose(); ho_RegionFillUp.Dispose(); ho_SelectedRegions1.Dispose(); ho_RegionIntersection.Dispose(); ho_RegionFillUp1.Dispose(); ho_RegionDifference.Dispose(); ho_CaltabCandidates.Dispose(); ho_ObjectSelected.Dispose(); ho_ConnectedMarks.Dispose(); ho_ObjectSelectedCaltab.Dispose(); ho_RegionFillUpCand.Dispose(); ho_MarksCand.Dispose(); ho_RegionDilation1.Dispose(); ho_ImageReduced.Dispose(); ho_DefaultEdges.Dispose(); ho_UnionContours.Dispose(); ho_SelectedXLD.Dispose(); ho_SelectedXLD1.Dispose(); return; } hv_SortedIndex = hv_Rectangularity.TupleSortIndex(); hv_IndexBest = (((hv_SortedIndex.TupleInverse())).TupleSelect(0)) + 1; ho_Caltab.Dispose(); HOperatorSet.SelectObj(ho_RegionFillUpCand, out ho_Caltab, hv_IndexBest); ho_RegionFillUp.Dispose(); HOperatorSet.FillUp(ho_Caltab, out ho_RegionFillUp); ho_MarksCand.Dispose(); HOperatorSet.Difference(ho_RegionFillUp, ho_RegionDynThresh, out ho_MarksCand ); ho_RegionBorder.Dispose(); HOperatorSet.Boundary(ho_MarksCand, out ho_RegionBorder, "inner"); ho_RegionDilation1.Dispose(); HOperatorSet.DilationCircle(ho_RegionBorder, out ho_RegionDilation1, 9.5); ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_RegionDilation1, out ho_ImageReduced); hv_MinContrast = 10; ho_DefaultEdges.Dispose(); HOperatorSet.EdgesSubPix(ho_ImageReduced, out ho_DefaultEdges, "canny", 2, hv_MinContrast / 2, hv_MinContrast); HOperatorSet.CountObj(ho_DefaultEdges, out hv_NumberCand); if ((int)(new HTuple(hv_NumberCand.TupleLess(10))) != 0) { ho_ImageMean.Dispose(); ho_RegionDynThresh.Dispose(); ho_RegionBorder.Dispose(); ho_RegionOpening1.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions4.Dispose(); ho_SelectedRegions5.Dispose(); ho_RegionBorder2.Dispose(); ho_RegionTrans.Dispose(); ho_RegionErosion.Dispose(); ho_RegionBorder1.Dispose(); ho_RegionDilation2.Dispose(); ho_RegionDifference1.Dispose(); ho_RegionOpening.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions2.Dispose(); ho_SelectedRegions.Dispose(); ho_RegionFillUp.Dispose(); ho_SelectedRegions1.Dispose(); ho_RegionIntersection.Dispose(); ho_RegionFillUp1.Dispose(); ho_RegionDifference.Dispose(); ho_CaltabCandidates.Dispose(); ho_ObjectSelected.Dispose(); ho_ConnectedMarks.Dispose(); ho_ObjectSelectedCaltab.Dispose(); ho_RegionFillUpCand.Dispose(); ho_MarksCand.Dispose(); ho_RegionDilation1.Dispose(); ho_ImageReduced.Dispose(); ho_DefaultEdges.Dispose(); ho_UnionContours.Dispose(); ho_SelectedXLD.Dispose(); ho_SelectedXLD1.Dispose(); return; } ho_UnionContours.Dispose(); HOperatorSet.UnionCocircularContoursXld(ho_DefaultEdges, out ho_UnionContours, 0.5, 0.1, 0.2, 30, 10, 10, "true", 1); ho_SelectedXLD.Dispose(); HOperatorSet.SelectShapeXld(ho_UnionContours, out ho_SelectedXLD, "area", "and", 30, 10000); ho_SelectedXLD1.Dispose(); HOperatorSet.SelectShapeXld(ho_SelectedXLD, out ho_SelectedXLD1, "circularity", "and", 0.4, 1); ho_MarksCand.Dispose(); HOperatorSet.SelectShapeXld(ho_SelectedXLD1, out ho_MarksCand, "compactness", "and", 1, 1.5); HOperatorSet.AreaCenterXld(ho_MarksCand, out hv_Area, out hv_Dummy, out hv_Dummy, out hv_DummyS); HOperatorSet.CountObj(ho_MarksCand, out hv_Number); if ((int)(new HTuple(hv_Number.TupleLess(4))) != 0) { ho_ImageMean.Dispose(); ho_RegionDynThresh.Dispose(); ho_RegionBorder.Dispose(); ho_RegionOpening1.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions4.Dispose(); ho_SelectedRegions5.Dispose(); ho_RegionBorder2.Dispose(); ho_RegionTrans.Dispose(); ho_RegionErosion.Dispose(); ho_RegionBorder1.Dispose(); ho_RegionDilation2.Dispose(); ho_RegionDifference1.Dispose(); ho_RegionOpening.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions2.Dispose(); ho_SelectedRegions.Dispose(); ho_RegionFillUp.Dispose(); ho_SelectedRegions1.Dispose(); ho_RegionIntersection.Dispose(); ho_RegionFillUp1.Dispose(); ho_RegionDifference.Dispose(); ho_CaltabCandidates.Dispose(); ho_ObjectSelected.Dispose(); ho_ConnectedMarks.Dispose(); ho_ObjectSelectedCaltab.Dispose(); ho_RegionFillUpCand.Dispose(); ho_MarksCand.Dispose(); ho_RegionDilation1.Dispose(); ho_ImageReduced.Dispose(); ho_DefaultEdges.Dispose(); ho_UnionContours.Dispose(); ho_SelectedXLD.Dispose(); ho_SelectedXLD1.Dispose(); return; } hv_AreaMedian = ((hv_Area.TupleSort())).TupleSelect(hv_Number / 2); ho_Marks.Dispose(); HOperatorSet.SelectShapeXld(ho_MarksCand, out ho_Marks, "area", "and", hv_AreaMedian - (hv_AreaMedian * 0.5), hv_AreaMedian + (hv_AreaMedian * 0.5)); ho_ImageMean.Dispose(); ho_RegionDynThresh.Dispose(); ho_RegionBorder.Dispose(); ho_RegionOpening1.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions4.Dispose(); ho_SelectedRegions5.Dispose(); ho_RegionBorder2.Dispose(); ho_RegionTrans.Dispose(); ho_RegionErosion.Dispose(); ho_RegionBorder1.Dispose(); ho_RegionDilation2.Dispose(); ho_RegionDifference1.Dispose(); ho_RegionOpening.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions2.Dispose(); ho_SelectedRegions.Dispose(); ho_RegionFillUp.Dispose(); ho_SelectedRegions1.Dispose(); ho_RegionIntersection.Dispose(); ho_RegionFillUp1.Dispose(); ho_RegionDifference.Dispose(); ho_CaltabCandidates.Dispose(); ho_ObjectSelected.Dispose(); ho_ConnectedMarks.Dispose(); ho_ObjectSelectedCaltab.Dispose(); ho_RegionFillUpCand.Dispose(); ho_MarksCand.Dispose(); ho_RegionDilation1.Dispose(); ho_ImageReduced.Dispose(); ho_DefaultEdges.Dispose(); ho_UnionContours.Dispose(); ho_SelectedXLD.Dispose(); ho_SelectedXLD1.Dispose(); 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); }
// Procedures public void EnhanceEdgeArea(HObject ho_InputImage, out HObject ho_EnhancedImage, HTuple hv_LineStartPoint_Row, HTuple hv_LineStartPoint_Column, HTuple hv_LineEndPoint_Row, HTuple hv_LineEndPoint_Column, HTuple hv_RoiWidthLen, HTuple hv_EmpMaskWidth, HTuple hv_EmpMaskHeight, HTuple hv_EmpMaskFactor, HTuple hv_MeanMaskWidth, HTuple hv_MeanMaskHeight, HTuple hv_IterationCount, HTuple hv_MinGray, HTuple hv_MaxGray, HTuple hv_Grayval, HTuple hv_ClosingWidth, HTuple hv_ClosingHeight, HTuple hv_DilationRadius) { // Stack for temporary objects HObject[] OTemp = new HObject[20]; // Local iconic variables HObject ho_Rectangle, ho_RegionDilation, ho_MeanImage = null; HObject ho_ImageEmphasize = null, ho_Seg, ho_ConnectedRegions; HObject ho_SelectedRegions, ho_RegionClosing, ho_RegionBorder; HObject ho_RegionBorder2; // Local control variables HTuple hv_TmpCtrl_Row = null, hv_TmpCtrl_Column = null; HTuple hv_TmpCtrl_Dr = null, hv_TmpCtrl_Dc = null, hv_TmpCtrl_Phi = null; HTuple hv_TmpCtrl_Len1 = null, hv_TmpCtrl_Len2 = null; HTuple hv_Index = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_EnhancedImage); HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_RegionDilation); HOperatorSet.GenEmptyObj(out ho_MeanImage); HOperatorSet.GenEmptyObj(out ho_ImageEmphasize); HOperatorSet.GenEmptyObj(out ho_Seg); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_RegionClosing); HOperatorSet.GenEmptyObj(out ho_RegionBorder); HOperatorSet.GenEmptyObj(out ho_RegionBorder2); // //init //FilterAlpha := 6 //Measure 01: Convert coordinates to rectangle2 type hv_TmpCtrl_Row = 0.5 * (hv_LineStartPoint_Row + hv_LineEndPoint_Row); hv_TmpCtrl_Column = 0.5 * (hv_LineStartPoint_Column + hv_LineEndPoint_Column); hv_TmpCtrl_Dr = hv_LineStartPoint_Row - hv_LineEndPoint_Row; hv_TmpCtrl_Dc = hv_LineEndPoint_Column - hv_LineStartPoint_Column; hv_TmpCtrl_Phi = hv_TmpCtrl_Dr.TupleAtan2(hv_TmpCtrl_Dc); hv_TmpCtrl_Len1 = 0.5 * ((((hv_TmpCtrl_Dr * hv_TmpCtrl_Dr) + (hv_TmpCtrl_Dc * hv_TmpCtrl_Dc))).TupleSqrt() ); hv_TmpCtrl_Len2 = hv_RoiWidthLen.Clone(); ho_Rectangle.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle, hv_TmpCtrl_Row, hv_TmpCtrl_Column, hv_TmpCtrl_Phi, hv_TmpCtrl_Len1, hv_TmpCtrl_Len2); ho_RegionDilation.Dispose(); HOperatorSet.DilationRectangle1(ho_Rectangle, out ho_RegionDilation, 100, 100); ho_EnhancedImage.Dispose(); HOperatorSet.ReduceDomain(ho_InputImage, ho_RegionDilation, out ho_EnhancedImage ); HTuple end_val17 = hv_IterationCount; HTuple step_val17 = 1; for (hv_Index = 1; hv_Index.Continue(end_val17, step_val17); hv_Index = hv_Index.TupleAdd(step_val17)) { ho_MeanImage.Dispose(); HOperatorSet.MeanImage(ho_EnhancedImage, out ho_MeanImage, hv_MeanMaskWidth, hv_MeanMaskHeight); ho_ImageEmphasize.Dispose(); HOperatorSet.Emphasize(ho_MeanImage, out ho_ImageEmphasize, hv_EmpMaskWidth, hv_EmpMaskHeight, hv_EmpMaskFactor); ho_EnhancedImage.Dispose(); HOperatorSet.MeanImage(ho_ImageEmphasize, out ho_EnhancedImage, 2, 2); //median_separate (ImageEmphasize, EnhancedImage, MeanMaskWidth, MeanMaskHeight, 'continued') } ho_Seg.Dispose(); HOperatorSet.FastThreshold(ho_EnhancedImage, out ho_Seg, hv_MinGray, hv_MaxGray, 150); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_Seg, out ho_ConnectedRegions); ho_SelectedRegions.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area", "and", 1000, 9999999); //opening_rectange1(SelectedRegions, RegionOpening, ClosingWidth, ClosingHeight) ho_RegionClosing.Dispose(); HOperatorSet.ClosingRectangle1(ho_SelectedRegions, out ho_RegionClosing, hv_ClosingWidth, hv_ClosingHeight); ho_RegionBorder.Dispose(); HOperatorSet.Boundary(ho_RegionClosing, out ho_RegionBorder, "inner"); ho_RegionDilation.Dispose(); HOperatorSet.DilationCircle(ho_RegionClosing, out ho_RegionDilation, hv_DilationRadius); ho_RegionBorder2.Dispose(); HOperatorSet.Boundary(ho_RegionDilation, out ho_RegionBorder2, "inner"); HOperatorSet.PaintRegion(ho_RegionDilation, ho_EnhancedImage, out OTemp[0], hv_Grayval, "fill"); ho_EnhancedImage.Dispose(); ho_EnhancedImage = OTemp[0]; //region_to_bin (RegionDilation, EnhancedImage, 255, 0, 512, 512) ho_Rectangle.Dispose(); ho_RegionDilation.Dispose(); ho_MeanImage.Dispose(); ho_ImageEmphasize.Dispose(); ho_Seg.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_RegionClosing.Dispose(); ho_RegionBorder.Dispose(); ho_RegionBorder2.Dispose(); return; }
public void big_small_screwteeth(HObject ho_RegionToDetect, out HObject ho_Arrow, HTuple hv_blackOrwhite, HTuple hv_vOrh, out HTuple hv_big_diameter, out HTuple hv_small_diameter) { // Local iconic variables HObject ho_RegionTrans, ho_RegionBorder, ho_Rectangle = null; HObject ho_RegionIntersection = null, ho_ConnectedRegions = null; HObject ho_SortedRegions = null, ho_ObjectSelected1 = null; HObject ho_ObjectSelected2 = null, ho_Arrow1 = null, ho_Arrow2 = null; HObject ho_Arrow3 = null, ho_Arrow4 = null, ho_Arrow5, ho_Arrow6; // Local control variables HTuple hv_Row1, hv_Column1, hv_Row2, hv_Column2; HTuple hv_Rows = new HTuple(), hv_Columns = new HTuple(), hv_maxr1 = new HTuple(); HTuple hv_minr1 = new HTuple(), hv_Indices1 = new HTuple(); HTuple hv_Indices2 = new HTuple(), hv_maxr2 = new HTuple(); HTuple hv_minr2 = new HTuple(), hv_maxc1 = new HTuple(), hv_minc1 = new HTuple(); HTuple hv_maxc2 = new HTuple(), hv_minc2 = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Arrow); HOperatorSet.GenEmptyObj(out ho_RegionTrans); HOperatorSet.GenEmptyObj(out ho_RegionBorder); HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_RegionIntersection); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SortedRegions); HOperatorSet.GenEmptyObj(out ho_ObjectSelected1); HOperatorSet.GenEmptyObj(out ho_ObjectSelected2); HOperatorSet.GenEmptyObj(out ho_Arrow1); HOperatorSet.GenEmptyObj(out ho_Arrow2); HOperatorSet.GenEmptyObj(out ho_Arrow3); HOperatorSet.GenEmptyObj(out ho_Arrow4); HOperatorSet.GenEmptyObj(out ho_Arrow5); HOperatorSet.GenEmptyObj(out ho_Arrow6); hv_big_diameter = new HTuple(); hv_small_diameter = new HTuple(); ho_RegionTrans.Dispose(); HOperatorSet.ShapeTrans(ho_RegionToDetect, out ho_RegionTrans, "rectangle1"); HOperatorSet.SmallestRectangle1(ho_RegionTrans, out hv_Row1, out hv_Column1, out hv_Row2, out hv_Column2); ho_RegionBorder.Dispose(); HOperatorSet.Boundary(ho_RegionToDetect, out ho_RegionBorder, "inner_filled"); if ((int)(new HTuple(hv_vOrh.TupleEqual("h"))) != 0) { ho_Rectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle, hv_Row1, hv_Column1 + 20, hv_Row2, hv_Column2 - 20); ho_RegionIntersection.Dispose(); HOperatorSet.Intersection(ho_Rectangle, ho_RegionBorder, out ho_RegionIntersection ); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_RegionIntersection, out ho_ConnectedRegions); ho_SortedRegions.Dispose(); HOperatorSet.SortRegion(ho_ConnectedRegions, out ho_SortedRegions, "first_point", "true", "row"); ho_ObjectSelected1.Dispose(); HOperatorSet.SelectObj(ho_SortedRegions, out ho_ObjectSelected1, 1); ho_ObjectSelected2.Dispose(); HOperatorSet.SelectObj(ho_SortedRegions, out ho_ObjectSelected2, 2); HOperatorSet.GetRegionPoints(ho_ObjectSelected1, out hv_Rows, out hv_Columns); hv_maxr1 = hv_Rows.TupleMax(); hv_minr1 = hv_Rows.TupleMin(); HOperatorSet.TupleFind(hv_Rows, hv_maxr1, out hv_Indices1); HOperatorSet.TupleFind(hv_Rows, hv_minr1, out hv_Indices2); ho_Arrow1.Dispose(); gen_arrow_contour_xld(out ho_Arrow1, (hv_Rows.TupleSelect(hv_Indices1.TupleSelect( 0))) - 50, hv_Columns.TupleSelect(hv_Indices1.TupleSelect(0)), hv_Rows.TupleSelect( hv_Indices1.TupleSelect(0)), hv_Columns.TupleSelect(hv_Indices1.TupleSelect( 0)), 15, 15); ho_Arrow2.Dispose(); gen_arrow_contour_xld(out ho_Arrow2, (hv_Rows.TupleSelect(hv_Indices2.TupleSelect( 0))) - 50, hv_Columns.TupleSelect(hv_Indices2.TupleSelect(0)), hv_Rows.TupleSelect( hv_Indices2.TupleSelect(0)), hv_Columns.TupleSelect(hv_Indices2.TupleSelect( 0)), 15, 15); HOperatorSet.GetRegionPoints(ho_ObjectSelected2, out hv_Rows, out hv_Columns); hv_maxr2 = hv_Rows.TupleMax(); hv_minr2 = hv_Rows.TupleMin(); //maxc := max(Columns) //minc := min(Columns) //big_diameter := maxr-minr HOperatorSet.TupleFind(hv_Rows, hv_maxr2, out hv_Indices1); HOperatorSet.TupleFind(hv_Rows, hv_minr2, out hv_Indices2); ho_Arrow3.Dispose(); gen_arrow_contour_xld(out ho_Arrow3, (hv_Rows.TupleSelect(hv_Indices1.TupleSelect( 0))) + 50, hv_Columns.TupleSelect(hv_Indices1.TupleSelect(0)), hv_Rows.TupleSelect( hv_Indices1.TupleSelect(0)), hv_Columns.TupleSelect(hv_Indices1.TupleSelect( 0)), 15, 15); ho_Arrow4.Dispose(); gen_arrow_contour_xld(out ho_Arrow4, (hv_Rows.TupleSelect(hv_Indices2.TupleSelect( 0))) + 50, hv_Columns.TupleSelect(hv_Indices2.TupleSelect(0)), hv_Rows.TupleSelect( hv_Indices2.TupleSelect(0)), hv_Columns.TupleSelect(hv_Indices2.TupleSelect( 0)), 15, 15); hv_big_diameter = hv_maxr2 - hv_minr1; hv_small_diameter = hv_minr2 - hv_maxr1; } else if ((int)(new HTuple(hv_vOrh.TupleEqual("v"))) != 0) { ho_Rectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle, hv_Row1 + 20, hv_Column1, hv_Row2 - 20, hv_Column2); ho_RegionIntersection.Dispose(); HOperatorSet.Intersection(ho_Rectangle, ho_RegionBorder, out ho_RegionIntersection ); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_RegionIntersection, out ho_ConnectedRegions); ho_SortedRegions.Dispose(); HOperatorSet.SortRegion(ho_ConnectedRegions, out ho_SortedRegions, "first_point", "true", "row"); ho_ObjectSelected1.Dispose(); HOperatorSet.SelectObj(ho_SortedRegions, out ho_ObjectSelected1, 1); ho_ObjectSelected2.Dispose(); HOperatorSet.SelectObj(ho_SortedRegions, out ho_ObjectSelected2, 2); HOperatorSet.GetRegionPoints(ho_ObjectSelected1, out hv_Rows, out hv_Columns); hv_maxc1 = hv_Columns.TupleMax(); hv_minc1 = hv_Columns.TupleMin(); HOperatorSet.TupleFind(hv_Columns, hv_maxc1, out hv_Indices1); HOperatorSet.TupleFind(hv_Columns, hv_minc1, out hv_Indices2); ho_Arrow1.Dispose(); gen_arrow_contour_xld(out ho_Arrow1, hv_Rows.TupleSelect(hv_Indices1.TupleSelect( 0)), (hv_Columns.TupleSelect(hv_Indices1.TupleSelect(0))) - 50, hv_Rows.TupleSelect( hv_Indices1.TupleSelect(0)), hv_Columns.TupleSelect(hv_Indices1.TupleSelect( 0)), 15, 15); ho_Arrow2.Dispose(); gen_arrow_contour_xld(out ho_Arrow2, hv_Rows.TupleSelect(hv_Indices2.TupleSelect( 0)), (hv_Columns.TupleSelect(hv_Indices2.TupleSelect(0))) - 50, hv_Rows.TupleSelect( hv_Indices2.TupleSelect(0)), hv_Columns.TupleSelect(hv_Indices2.TupleSelect( 0)), 15, 15); HOperatorSet.GetRegionPoints(ho_ObjectSelected2, out hv_Rows, out hv_Columns); hv_maxc2 = hv_Columns.TupleMax(); hv_minc2 = hv_Columns.TupleMin(); HOperatorSet.TupleFind(hv_Columns, hv_maxc2, out hv_Indices1); HOperatorSet.TupleFind(hv_Columns, hv_minc2, out hv_Indices2); ho_Arrow3.Dispose(); gen_arrow_contour_xld(out ho_Arrow3, hv_Rows.TupleSelect(hv_Indices1.TupleSelect( 0)), (hv_Columns.TupleSelect(hv_Indices1.TupleSelect(0))) + 50, hv_Rows.TupleSelect( hv_Indices1.TupleSelect(0)), hv_Columns.TupleSelect(hv_Indices1.TupleSelect( 0)), 15, 15); ho_Arrow4.Dispose(); gen_arrow_contour_xld(out ho_Arrow4, hv_Rows.TupleSelect(hv_Indices2.TupleSelect( 0)), (hv_Columns.TupleSelect(hv_Indices2.TupleSelect(0))) + 50, hv_Rows.TupleSelect( hv_Indices2.TupleSelect(0)), hv_Columns.TupleSelect(hv_Indices2.TupleSelect( 0)), 15, 15); hv_big_diameter = hv_maxc2 - hv_minc1; hv_small_diameter = hv_minc2 - hv_maxc1; } ho_Arrow5.Dispose(); HOperatorSet.ConcatObj(ho_Arrow1, ho_Arrow2, out ho_Arrow5); ho_Arrow6.Dispose(); HOperatorSet.ConcatObj(ho_Arrow3, ho_Arrow4, out ho_Arrow6); ho_Arrow.Dispose(); HOperatorSet.ConcatObj(ho_Arrow5, ho_Arrow6, out ho_Arrow); ho_RegionTrans.Dispose(); ho_RegionBorder.Dispose(); ho_Rectangle.Dispose(); ho_RegionIntersection.Dispose(); ho_ConnectedRegions.Dispose(); ho_SortedRegions.Dispose(); ho_ObjectSelected1.Dispose(); ho_ObjectSelected2.Dispose(); ho_Arrow1.Dispose(); ho_Arrow2.Dispose(); ho_Arrow3.Dispose(); ho_Arrow4.Dispose(); ho_Arrow5.Dispose(); ho_Arrow6.Dispose(); return; }