Ejemplo n.º 1
0
        /// <summary>
        /// Compute the angle between two lines
        /// </summary>
        /// <param name="line"></param>
        /// <returns>angle in radian uint</returns>
        public double AngleWithLine(Line line)
        {
            HTuple angle;

            HOperatorSet.AngleLl(YStart, XStart, YEnd, XEnd, line.YStart, line.XStart, line.YEnd, line.XEnd, out angle);
            return(angle);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 计算直线1的夹角
 /// </summary>
 /// <param name="Row1"></param>
 /// <param name="Col1"></param>
 /// <param name="Row2"></param>
 /// <param name="Col2"></param>
 /// <param name="CurHandEyeMat"></param>
 /// <param name="Th"></param>
 /// <returns></returns>
 public static bool CalculateLineTh(double Row1, double Col1, double Row2, double Col2, MyHomMat2D CurHandEyeMat, out double Th)
 {
     Th = 0;
     try {
         //调整原点位置
         Row1 = 2448 - Row1;
         Row2 = 2448 - Row2;
         Point2Db Pt1 = new Point2Db(true), Pt2 = new Point2Db(true);
         Pt1.Row = Row1;
         Pt1.Col = Col1;
         Pt2.Row = Row2;
         Pt2.Col = Col2;
         Point2Db WorldPt1 = new Point2Db(true), WorldPt2 = new Point2Db(true);
         MyVisionBase.AffineTransPoint2D(Pt1, CurHandEyeMat, out WorldPt1);
         MyVisionBase.AffineTransPoint2D(Pt2, CurHandEyeMat, out WorldPt2);
         HTuple AngleX = 0;
         if (WorldPt1.Col < WorldPt2.Col)
         {
             HOperatorSet.AngleLl(0, 0, 1, 0, WorldPt1.Col, WorldPt1.Row, WorldPt2.Col, WorldPt2.Row, out AngleX);
             Th = AngleX.D;
         }
         else
         {
             HOperatorSet.AngleLl(0, 0, 1, 0, WorldPt2.Col, WorldPt2.Row, WorldPt1.Col, WorldPt1.Row, out AngleX);
             Th = AngleX.D;
         }
         return(true);
     }
     catch (Exception e0) {
         Logger.PopError(e0.Message + e0.Source + e0.StackTrace);
         return(false);
     }
 }
Ejemplo n.º 3
0
        public override int Measure(HObject ho_Image) //测量
        {
            base.Measure(ho_Image);                   //调用父类的测量方法
            Line line1 = (unit1 as Line);
            Line line2 = (unit2 as Line);

            HOperatorSet.AngleLl(line1.hv_Row1, line1.hv_Column1, line1.hv_Row2, line1.hv_Column2,
                                 line2.hv_Row1, line2.hv_Column1, line2.hv_Row2, line2.hv_Column2, out hv_RealDistance);
            hv_RealDistance = hv_RealDistance.TupleDeg().TupleAbs();
            if (AlwaysMinAngel)
            {
                if (hv_RealDistance.D > 90)
                {
                    hv_RealDistance = 180 - hv_RealDistance;
                }
            }
            ComparingResults();
            Point cLine1 = new Point((line1.hv_Column1 + line1.hv_Column2) / 2, (line1.hv_Row1 + line1.hv_Row2) / 2);
            Point cLine2 = new Point((line2.hv_Column1 + line2.hv_Column2) / 2, (line2.hv_Row1 + line2.hv_Row2) / 2);

            DP.hv_Column = (cLine1.hv_Column + cLine2.hv_Column) / 2;
            DP.hv_Row    = (cLine1.hv_Row + cLine1.hv_Row) / 2;
            MeasureDone  = true;
            return(Convert.ToInt32(measureResult));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 計算線與線的角度
        /// </summary>
        /// <param name="line1">IMeasureGeoModel 線段1</param>
        /// <param name="line2">IMeasureGeoModel 線段2</param>
        /// <returns></returns>
        public static AngleResult AngleLineToLine(IMeasureGeoModel line1, IMeasureGeoModel line2)
        {
            AngleResult result = null;

            //check
            if (isMeasureModelValid(line1) && isMeasureModelValid(line2))
            {
                //act
                HTuple angle, firstPhi, secondPhi, interRow, interCol, isParallel;
                HOperatorSet.AngleLx(line1.Row1, line1.Col1, line1.Row2, line1.Col2, out firstPhi);

                HOperatorSet.AngleLx(line2.Row1, line2.Col1, line2.Row2, line2.Col2, out secondPhi);

                HOperatorSet.IntersectionLl(line1.Row1, line1.Col1, line1.Row2, line1.Col2, line2.Row1, line2.Col1, line2.Row2, line2.Col2,
                                            out interRow, out interCol, out isParallel);
                HOperatorSet.AngleLl(line1.Row1, line1.Col1, line1.Row2, line1.Col2, line2.Row1, line2.Col1, line2.Row2, line2.Col2, out angle);
                result = new AngleResult()
                {
                    Angle    = angle,
                    StartPhi = firstPhi,
                    EndPhi   = secondPhi,
                    Row      = interRow,
                    Col      = interCol,
                };
            }
            return(result);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 检测
        /// </summary>
        /// <param name="ho_Image"></param>
        /// <param name="hwin"></param>
        /// <param name="Key"></param>
        /// <param name="_dictionary_resulte"></param>
        /// <returns></returns>
        public override bool analyze_show(HWindow hwin, string Key, ref Dictionary <string, object> _dictionary_resulte)
        {
            bool ok = false;

            if ((this._diErTiaoZhiXian != null) && (this._diYiTiaoZhiXian != null))
            {
                if ((this._diErTiaoZhiXian.IrectShuJuPianYiOne != null) && (this._diYiTiaoZhiXian.IrectShuJuPianYiTwo != null) && (this._diErTiaoZhiXian.IrectShuJuPianYiOne.Column != null) && (this._diYiTiaoZhiXian.IrectShuJuPianYiTwo.Column != null))
                {
                    HTuple IsOverlapping;
                    HOperatorSet.IntersectionLines(this._diYiTiaoZhiXian.IrectShuJuPianYiOne.Row, this._diYiTiaoZhiXian.IrectShuJuPianYiOne.Column, this._diYiTiaoZhiXian.IrectShuJuPianYiTwo.Row, this._diYiTiaoZhiXian.IrectShuJuPianYiTwo.Column, this._diErTiaoZhiXian.IrectShuJuPianYiOne.Row, this._diErTiaoZhiXian.IrectShuJuPianYiOne.Column, this._diErTiaoZhiXian.IrectShuJuPianYiTwo.Row, this._diErTiaoZhiXian.IrectShuJuPianYiTwo.Column, out this.row, out this.column, out IsOverlapping);
                    HOperatorSet.AngleLl(this._diYiTiaoZhiXian.IrectShuJuPianYiOne.Row, this._diYiTiaoZhiXian.IrectShuJuPianYiOne.Column, this._diYiTiaoZhiXian.IrectShuJuPianYiTwo.Row, this._diYiTiaoZhiXian.IrectShuJuPianYiTwo.Column, this._diErTiaoZhiXian.IrectShuJuPianYiOne.Row, this._diErTiaoZhiXian.IrectShuJuPianYiOne.Column, this._diErTiaoZhiXian.IrectShuJuPianYiTwo.Row, this._diErTiaoZhiXian.IrectShuJuPianYiTwo.Column, out angle);

                    this.JiaoDianXLD.Dispose();
                    HOperatorSet.GenCrossContourXld(out this._jiaoDianXLD, this.Row, this.Column, 6, this.Angle);

                    /*************数据分析********************/
                    Key = "DistanceTwoLine_" + Key;
                    _result._tolatName   = Key;
                    _result._tolatResult = true;
                    _result.Angle        = this.Angle;
                    _result.Column       = this.Column;
                    _result.Row          = this.Row;

                    if (this._ICalibration != null)
                    {
                        this.Cal(this._ICalibration.HomMat2D, ref _result.Row, ref _result.Column);
                    }
                    _dictionary_resulte.Add(Key, this._result);

                    show(hwin);
                    ok = true;
                }
            }
            return(ok);
        }
Ejemplo n.º 6
0
        public static bool CalculateLineAng(HTuple LineRow1, HTuple LineCol1, HTuple LineRow2, HTuple LineCol2, out HTuple OutAngle, out HObject LineArrow)
        {
            OutAngle  = new HTuple();
            LineArrow = new HObject();
            HTuple NewLineRow1 = new HTuple(), NewLineRow2 = new HTuple(), NewLineCol1 = new HTuple(), NewLineCol2 = new HTuple();
            bool   IsOk   = false;
            HTuple Length = 10;

            try {
                #region
                if (LineRow1.Length == 4 && LineRow2.Length == 4 && LineCol1.Length == 4 && LineCol2.Length == 4)
                {
                    NewLineRow1 = (LineRow1[0].D + LineRow1[1].D + LineRow2[0].D + LineRow2[1].D) / 4;
                    NewLineCol1 = (LineCol1[0].D + LineCol1[1].D + LineCol2[0].D + LineCol2[1].D) / 4;
                    NewLineRow2 = (LineRow1[2].D + LineRow1[3].D + LineRow2[2].D + LineRow2[3].D) / 4;
                    NewLineCol2 = (LineCol1[2].D + LineCol1[3].D + LineCol2[2].D + LineCol2[3].D) / 4;
                    HOperatorSet.DistancePp(NewLineCol1, NewLineRow1, NewLineCol2, NewLineRow2, out Length);
                    MyVisionBase.GenArrowContourXld(out LineArrow, NewLineRow1, NewLineCol1, NewLineRow2, NewLineCol2, Length / 20, Length / 20);
                    NewLineRow1 = 2048 - NewLineRow1[0].D;
                    NewLineRow2 = 2048 - NewLineRow2[0].D;
                    HOperatorSet.AngleLl(0, 0, 1, 0, NewLineCol1, NewLineRow1, NewLineCol2, NewLineRow2, out OutAngle);
                    IsOk = true;
                }
                else if (LineRow1.Length == 2 && LineRow2.Length == 2 && LineCol1.Length == 2 && LineCol2.Length == 2)
                {
                    NewLineRow1 = (LineRow1[0].D + LineRow2[0].D) / 2;
                    NewLineCol1 = (LineCol1[0].D + LineCol2[0].D) / 2;
                    NewLineRow2 = (LineRow1[1].D + LineRow2[1].D) / 2;
                    NewLineCol2 = (LineCol1[1].D + LineCol2[1].D) / 2;
                    HOperatorSet.DistancePp(NewLineCol1, NewLineRow1, NewLineCol2, NewLineRow2, out Length);
                    MyVisionBase.GenArrowContourXld(out LineArrow, NewLineRow1, NewLineCol1, NewLineRow2, NewLineCol2, Length / 20, Length / 20);
                    NewLineRow1 = 2048 - NewLineRow1[0].D;
                    NewLineRow2 = 2048 - NewLineRow2[0].D;
                    HOperatorSet.AngleLl(0, 0, 1, 0, NewLineCol1, NewLineRow1, NewLineCol2, NewLineRow2, out OutAngle);
                    IsOk = true;
                }
                else if (LineRow1.Length == 1 && LineRow2.Length == 1 && LineCol1.Length == 1 && LineCol2.Length == 1)
                {
                    NewLineRow1 = LineRow1[0].D;
                    NewLineCol1 = LineCol1[0].D;
                    NewLineRow2 = LineRow2[0].D;
                    NewLineCol2 = LineCol2[0].D;
                    HOperatorSet.DistancePp(NewLineCol1, NewLineRow1, NewLineCol2, NewLineRow2, out Length);
                    MyVisionBase.GenArrowContourXld(out LineArrow, NewLineRow1, NewLineCol1, NewLineRow2, NewLineCol2, Length / 20, Length / 20);
                    NewLineRow1 = 2048 - NewLineRow1[0].D;
                    NewLineRow2 = 2048 - NewLineRow2[0].D;
                    HOperatorSet.AngleLl(0, 0, 1, 0, NewLineCol1, NewLineRow1, NewLineCol2, NewLineRow2, out OutAngle);
                    IsOk = true;
                }
                #endregion
            }
            catch (Exception e0) {
                Logger.PopError1(e0);
            }
            return(IsOk);
        }
Ejemplo n.º 7
0
        private void action()
        {
            HObject ho_Rectangle1, ho_ImageReduced1, ho_Border, ho_ContoursSplit, ho_RegionLines;

            HOperatorSet.GenEmptyObj(out ho_Rectangle1);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced1);
            HOperatorSet.GenEmptyObj(out ho_Border);
            HOperatorSet.GenEmptyObj(out ho_ContoursSplit);
            HOperatorSet.GenEmptyObj(out ho_RegionLines);
            HTuple hv_RowBeginOut = null, hv_ColBeginOut = null, hv_RowEndOut = null, hv_ColEndOut = null;

            if (hv_ModelID == null)
            {
                HOperatorSet.ReadNccModel(PathHelper.currentProductPath + @"\jdmx.ncm", out hv_ModelID);
            }
            HTuple hv_ColBegin = null, hv_RowEnd = null, hv_ColEnd = null, hv_Angle1 = null, hv_jiaodu = null;
            HTuple hv_Nr = null, hv_Nc = null, hv_Dist = null, hv_RowBegin = null;
            HTuple hv_Angle = null, hv_Score = null, hv_Row1 = null, hv_Column1 = null;

            HOperatorSet.FindNccModel(Image, hv_ModelID, -0.2, 0.39, 0.8, 1, 0.5, "true",
                                      0, out hv_Row1, out hv_Column1, out hv_Angle, out hv_Score);
            ho_Rectangle1.Dispose();
            HOperatorSet.GenRectangle2(out ho_Rectangle1, hv_Row1, hv_Column1, hv_Phi + hv_Angle,
                                       hv_Length1, hv_Length2);
            ho_ImageReduced1.Dispose();
            HOperatorSet.ReduceDomain(Image, ho_Rectangle1, out ho_ImageReduced1);
            ho_Border.Dispose();
            HOperatorSet.ThresholdSubPix(ho_ImageReduced1, out ho_Border, thv);
            ho_ContoursSplit.Dispose();
            HOperatorSet.SegmentContoursXld(ho_Border, out ho_ContoursSplit, "lines_circles",
                                            5, 10, 10);
            HOperatorSet.FitLineContourXld(ho_ContoursSplit, "tukey", -1, 0, 5, 2, out hv_RowBegin,
                                           out hv_ColBegin, out hv_RowEnd, out hv_ColEnd, out hv_Nr, out hv_Nc, out hv_Dist);
            HOperatorSet.SelectLinesLongest(hv_RowBegin, hv_ColBegin, hv_RowEnd, hv_ColEnd,
                                            2, out hv_RowBeginOut, out hv_ColBeginOut, out hv_RowEndOut, out hv_ColEndOut);
            HOperatorSet.AngleLl(hv_RowBeginOut.TupleSelect(0), hv_ColBeginOut.TupleSelect(0),
                                 hv_RowEndOut.TupleSelect(0), hv_ColEndOut.TupleSelect(0), hv_RowBeginOut.TupleSelect(
                                     1), hv_ColBeginOut.TupleSelect(1), hv_RowEndOut.TupleSelect(1), hv_ColEndOut.TupleSelect(
                                     1), out hv_Angle1);
            ho_RegionLines.Dispose();
            HOperatorSet.GenRegionLine(out ho_RegionLines, hv_RowBeginOut, hv_ColBeginOut,
                                       hv_RowEndOut, hv_ColEndOut);
            HOperatorSet.Union2(ho_RegionLines, ho_RegionLines, out RegionToDisp);
            hv_jiaodu = ((hv_Angle1.TupleDeg())).TupleAbs();
            HTuple hv_result = GetHv_result();

            hv_result = hv_result.TupleConcat("夹角");
            hv_result = hv_result.TupleConcat(hv_jiaodu.D);
            result    = hv_result.Clone();
            ho_Rectangle1.Dispose();
            ho_ImageReduced1.Dispose();
            ho_Border.Dispose();
            ho_ContoursSplit.Dispose();
            ho_RegionLines.Dispose();
        }
        public static double GetAngle(PointF line1S, PointF line1E, PointF line2S, PointF line2E)
        {
            HTuple rad = new HTuple();
            HTuple deg = new HTuple();

            HOperatorSet.AngleLl(line1S.Y, line1S.X, line1E.Y, line1E.X
                                 , line2S.Y, line2S.X, line2E.Y, line2E.X, out rad);
            HOperatorSet.TupleDeg(rad, out deg);
            double angle = deg.D;

            return(angle);
        }
        public static double GetAngle(double px1, double py1, double px2, double py2)
        {
            //两点的x、y值
            HTuple rad = new HTuple();
            HTuple deg = new HTuple();

            HOperatorSet.AngleLl(py1, px1, py2, px2, 0, 0, 0, 1, out rad);
            HOperatorSet.TupleDeg(rad, out deg);
            double angle = deg.D;

            if (py1 == py2)// 0 和 180
            {
                angle = 0;
            }
            return(angle);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 线线夹角
        /// </summary>
        /// <param name="hv_Line1Row1"></param>
        /// <param name="hv_Line1Column1"></param>
        /// <param name="hv_Line1Row2"></param>
        /// <param name="hv_Line1Column2"></param>
        /// <param name="hv_Line2Row1"></param>
        /// <param name="hv_Line2Column1"></param>
        /// <param name="hv_Line2Row2"></param>
        /// <param name="hv_Line2Column2"></param>
        /// <returns></returns>
        public static double L2LAngle(HTuple hv_Line1Row1, HTuple hv_Line1Column1, HTuple hv_Line1Row2, HTuple hv_Line1Column2,
                                      HTuple hv_Line2Row1, HTuple hv_Line2Column1, HTuple hv_Line2Row2, HTuple hv_Line2Column2)
        {
            try
            {
                double Angle_Result;
                HOperatorSet.AngleLl(hv_Line1Row1, hv_Line1Column1, hv_Line1Row2, hv_Line1Column2, hv_Line2Row1, hv_Line2Column1, hv_Line2Row2, hv_Line2Column2, out HTuple angle);
                Angle_Result = angle;
                Angle_Result = Math.Abs(Angle_Result) / Math.PI * 180;

                if (Angle_Result > 90)
                {
                    Angle_Result = 180 - Angle_Result;
                }
                return(Angle_Result);
            }
            catch (Exception)
            { return(100); }
        }
    // 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();
    }
Ejemplo n.º 12
0
        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();
            }
        }
Ejemplo n.º 13
0
        public static bool CircleLocation(HObject ImgIn, St_CirclesParam TeachCirclePara0, out St_VectorAngle VectorAngleOut, out HObject circleCont, out HObject CirContour, out HObject centerCont)
        {
            St_CirclesParam TeachCirclePara;

            TeachCirclePara = TeachCirclePara0;
            HObject ObjContour = new HObject();

            CirContour = new HObject();
            HOperatorSet.GenEmptyObj(out CirContour);
            HObject CircleObj = new HObject();

            centerCont     = new HObject();
            circleCont     = new HObject();
            VectorAngleOut = new St_VectorAngle(true);

            HTuple  CircleRows = new HTuple(), CircleCols = new HTuple();
            HObject RoiContour = new HObject();
            HTuple  ResultRows = new HTuple(), ResultCols = new HTuple(), ArcType0 = new HTuple();
            HTuple  CircleCenterRow = new HTuple(), CircleCenterCol = new HTuple(), CircleRadius = new HTuple();
            HTuple  StartPhi = new HTuple(), EndPhi = new HTuple(), PointOrder = new HTuple(), IsTrueFlag = new HTuple();
            int     cirCount;

            try
            {
                #region
                for (int i = 0; i < TeachCirclePara.Count; i++)
                {
                    //2.0利用存下来的圆的信息,创建圆的边缘点坐标
                    MyVisionBase.GenCirclePts2(TeachCirclePara.CenterRows[i], TeachCirclePara.CenterCols[i], TeachCirclePara.CircleRs[i], TeachCirclePara.StartPhis[i], TeachCirclePara.EndPhis[i]
                                               , TeachCirclePara.PointOrders[i], out CircleRows, out CircleCols);
                    //3.0找出边缘点用来拟合圆
                    MyVisionBase.spoke2(ImgIn, TeachCirclePara.Elements[i], TeachCirclePara.DetectHeights[i], 2, 2, TeachCirclePara.Thresholds[i], "all", "first", CircleRows, CircleCols,
                                        TeachCirclePara.Directs[i], out ResultRows, out ResultCols, out ArcType0);
                    HOperatorSet.GenCrossContourXld(out CircleObj, ResultRows, ResultCols, TeachCirclePara.CircleRs[i] / 10, 0.6);
                    HOperatorSet.ConcatObj(CircleObj, CirContour, out CirContour);
                    cirCount = CirContour.CountObj();

                    HTuple ElectRows = ResultRows, ElectCols = ResultCols;
                    int    lengthResultRows = ResultRows.Length;
                    int    StartNum         = (int)(lengthResultRows * 0.3);
                    int    RemoveNum        = (int)(lengthResultRows * 0.2);
                    for (int j = StartNum; j < StartNum + RemoveNum; j++)
                    {
                        if (j < lengthResultRows && StartNum + RemoveNum < lengthResultRows)
                        {
                            ElectRows = ElectRows.TupleRemove(StartNum);
                            ElectCols = ElectCols.TupleRemove(StartNum);
                        }
                    }
                    //4.拟合圆找到圆的中心
                    HObject CircleContour = new HObject();
                    HTuple  FitRow = new HTuple(), FitCol = new HTuple(), FitR = new HTuple(), SartP = new HTuple(), EndP = new HTuple(), IsFlag = new HTuple();
                    MyVisionBase.PtsToBestCircle1(out CircleObj, ElectRows, ElectCols, 4, "arc", out FitRow, out FitCol, out FitR, out SartP, out EndP, out IsFlag);
                    if (Math.Abs(TeachCirclePara.CircleRs[i] - FitR.D) > TeachCirclePara.CircleRs[i] * 0.2)
                    {
                        HOperatorSet.ConcatObj(CircleObj, CirContour, out CirContour);
                        Logger.PopError("找出圆的半径大于理论值。");
                        return(false);
                    }
                    cirCount = CircleObj.CountObj();
                    HOperatorSet.ConcatObj(CircleObj, CirContour, out CirContour);
                    CircleCenterRow[i] = FitRow.D;
                    CircleCenterCol[i] = FitCol.D;
                    CircleRadius[i]    = FitR.D;
                    StartPhi[i]        = SartP.D;
                    EndPhi[i]          = EndP.D;
                    IsTrueFlag[i]      = IsFlag[0];
                }
                HTuple AngleLxPixel = new HTuple(), AngleLx = new HTuple();
                if (CircleCenterRow.TupleLength() != TeachCirclePara0.Count)
                {
                    Logger.PopError("CircleTypePos.FindCenter" + "没有找到圆");
                    return(false);
                }

                #region  //生成圆心十字轮廓,非功能性代码
                HTuple cRow = new HTuple(), cColumn = new HTuple();
                int    count = CircleCenterRow.TupleLength();
                cRow    = 0;
                cColumn = 0;
                if (count < 4)
                {
                    for (int i = 0; i < count; i++)
                    {
                        cRow    += CircleCenterRow[i];
                        cColumn += CircleCenterCol[i];
                    }
                    HOperatorSet.GenCrossContourXld(out centerCont, cRow / count, cColumn / count, 150, 0);
                }
                else if (count == 4)
                {
                    for (int i = 0; i < 2; i++)
                    {
                        cRow    += CircleCenterRow[i];
                        cColumn += CircleCenterCol[i];
                    }
                    HOperatorSet.GenCrossContourXld(out centerCont, cRow / 2, cColumn / 2, 150, 0);
                }
                #endregion
                //5.0X轴到圆的中心连线的角度
                HTuple CircleRowAdj = new HTuple();
                CircleRowAdj = CircleCenterRow.Clone();
                //5.1调整行坐标
                MyVisionBase.AdjImgRow(ImgIn, ref CircleRowAdj);
                if (CircleRowAdj.Length >= 2)
                {
                    if (CircleCenterCol[0].D < CircleCenterCol[1].D)
                    {
                        HOperatorSet.AngleLl(0, 0, 1, 0, CircleCenterCol[0].D, CircleRowAdj[0].D, CircleCenterCol[1].D, CircleRowAdj[1].D, out AngleLxPixel); //圆中心连线与相机坐标X轴之间的夹角
                    }
                    else
                    {
                        HOperatorSet.AngleLl(0, 0, 1, 0, CircleCenterCol[1].D, CircleRowAdj[1].D, CircleCenterCol[0].D, CircleRowAdj[0].D, out AngleLxPixel);
                    }
                }
                else if (CircleRowAdj.Length == 1)
                {
                    AngleLxPixel = 0;
                }
                VectorAngleOut = new St_VectorAngle(cRow / count, cColumn / count, AngleLxPixel);
                if (double.IsNaN(cColumn / count) || double.IsNaN(cRow / count))
                {
                    Logger.PopError("计算出错,非数字");
                    return(false);
                }
                return(true);

                #endregion
            }
            catch (Exception e0)
            {
                Logger.PopError(e0.Message + e0.Source);
                return(false);
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// 计算胶带或者金手指中心在平台中的坐标
        /// </summary>
        /// <param name="ImgIn"></param>
        /// <param name="CurHandEyeHomMat">手眼标定矩阵</param>
        /// <param name="IsDn">是否是下相机</param>
        /// <param name="CurMotionPos">当前平台的坐标</param>
        /// <param name="TeachCirclePara0">圆的示教参数</param>
        /// <param name="Row1">模板示教时的行坐标</param>
        /// <param name="Col1">模板示教时的列坐标</param>
        /// <param name="Theta1">模板示教时的角度</param>
        /// <param name="Row2"></param>
        /// <param name="Col2"></param>
        /// <param name="Theta2"></param>
        /// <param name="CenterRow">胶带或者金手指的中心在平台中的行坐标</param>
        /// <param name="CenterCol">胶带或者金手指的中心在平台中的列坐标</param>
        /// <param name="RotAngle"></param>
        /// <returns></returns>
        public static En_ImageProcessResult FindCenter(HObject ImgIn, MyHomMat2D CurHandEyeHomMat, bool IsUp, Point2Db CurMotionPos, St_CirclesParam TeachCirclePara0, HTuple Row1, HTuple Col1, HTuple Theta1,
                                                       HTuple Row2, HTuple Col2, HTuple Theta2, out double CenterRow, out double CenterCol, out double RotAngle, out HObject circleCont, out HObject CirContour, out HObject centerCont)
        {
            CenterRow = 0;
            CenterCol = 0;
            RotAngle  = 0;
            St_CirclesParam TeachCirclePara;

            TeachCirclePara = TeachCirclePara0;
            HObject ObjContour = new HObject();

            CirContour = new HObject();
            HOperatorSet.GenEmptyObj(out CirContour);
            HObject CircleObj = new HObject();

            centerCont = new HObject();
            circleCont = new HObject();
            // string strLog = "";

            //1.0调整示教的坐标
            try
            {
                MyVisionBase.VectorAngleToTransPt(Row1, Col1, Theta1, Row2, Col2, Theta2, TeachCirclePara0.CenterRows, TeachCirclePara0.CenterCols, out TeachCirclePara.CenterRows, out TeachCirclePara.CenterCols);
                //if (TeachCirclePara.IsThreeFeeder)
                //{
                //    TeachCirclePara.CenterRows[2] = TeachCirclePara0.CenterRows[2];
                //    TeachCirclePara.CenterRows[3] = TeachCirclePara0.CenterRows[3];
                //    TeachCirclePara0.CenterCols[2] = TeachCirclePara0.CenterCols[2];
                //    TeachCirclePara0.CenterCols[3] = TeachCirclePara0.CenterCols[3];
                //}
                HTuple  CircleRows = new HTuple(), CircleCols = new HTuple();
                HObject RoiContour = new HObject();
                HTuple  ResultRows = new HTuple(), ResultCols = new HTuple(), ArcType0 = new HTuple();
                HTuple  CircleCenterRow = new HTuple(), CircleCenterCol = new HTuple(), CircleRadius = new HTuple();
                HTuple  StartPhi = new HTuple(), EndPhi = new HTuple(), PointOrder = new HTuple(), IsTrueFlag = new HTuple();
                int     cirCount;
                #region  //用示教的参数找出圆
                for (int i = 0; i < TeachCirclePara.CenterRows.Count; i++)
                {
                    //2.0利用存下来的圆的信息,创建圆的边缘点坐标
                    MyVisionBase.GenCirclePts2(TeachCirclePara.CenterRows[i], TeachCirclePara.CenterCols[i], TeachCirclePara.CircleRs[i], TeachCirclePara.StartPhis[i], TeachCirclePara.EndPhis[i]
                                               , TeachCirclePara.PointOrders[i], out CircleRows, out CircleCols);

                    //3.0找出边缘点用来拟合圆
                    MyVisionBase.spoke2(ImgIn, TeachCirclePara.Elements[i], TeachCirclePara.DetectHeights[i], 2, 2, TeachCirclePara.Thresholds[i], "all", "first", CircleRows, CircleCols,
                                        TeachCirclePara.Directs[i], out ResultRows, out ResultCols, out ArcType0);
                    HOperatorSet.GenCrossContourXld(out CircleObj, ResultRows, ResultCols, TeachCirclePara.CircleRs[i] / 10, 0.6);
                    HOperatorSet.ConcatObj(CircleObj, CirContour, out CirContour);
                    cirCount = CirContour.CountObj();
                    //4.拟合圆找到圆的中心
                    HObject CircleContour = new HObject();
                    HTuple  FitRow = new HTuple(), FitCol = new HTuple(), FitR = new HTuple(), SartP = new HTuple(), EndP = new HTuple(), IsFlag = new HTuple();
                    MyVisionBase.PtsToBestCircle1(out CircleObj, ResultRows, ResultCols, 4, "arc", out FitRow, out FitCol, out FitR, out SartP, out EndP, out IsFlag);
                    if (Math.Abs(TeachCirclePara.CircleRs[i] - FitR.D) > TeachCirclePara.CircleRs[i] * 0.5)
                    {
                        if (ObjContour != null)
                        {
                            ObjContour.Dispose();
                        }
                        if (CircleObj != null)
                        {
                            CircleObj.Dispose();
                        }
                        Logger.PopError("拟合的圆的半径和理论值偏差过大。");
                        return(En_ImageProcessResult.圆半径和理论值偏差过大);
                    }
                    cirCount = CircleObj.CountObj();
                    HOperatorSet.ConcatObj(CircleObj, CirContour, out CirContour);
                    CircleCenterRow[i] = FitRow.D;
                    CircleCenterCol[i] = FitCol.D;
                    CircleRadius[i]    = FitR.D;
                    StartPhi[i]        = SartP.D;
                    EndPhi[i]          = EndP.D;
                    IsTrueFlag[i]      = IsFlag[0];
                }
                #endregion
                HTuple AngleLxPixel = new HTuple(), AngleLx = new HTuple();
                if (CircleCenterRow.TupleLength() != TeachCirclePara0.CenterRows.Count)
                {
                    if (ObjContour != null)
                    {
                        ObjContour.Dispose();
                    }
                    if (CircleObj != null)
                    {
                        CircleObj.Dispose();
                    }
                    Logger.PopError("CircleTypePos.FindCenter" + "没有找到圆");
                    return(En_ImageProcessResult.圆的数量少于示教的数量);
                }
                #region  //生成圆心十字轮廓,非功能性代码
                HTuple cRow = new HTuple(), cColumn = new HTuple();
                int    count = CircleCenterRow.TupleLength();
                cRow    = 0;
                cColumn = 0;
                if (count < 4)
                {
                    for (int i = 0; i < count; i++)
                    {
                        cRow    += CircleCenterRow[i];
                        cColumn += CircleCenterCol[i];
                    }
                    HOperatorSet.GenCrossContourXld(out centerCont, cRow / count, cColumn / count, 150, 0);
                }
                else if (count == 4)
                {
                    for (int i = 0; i < 2; i++)
                    {
                        cRow    += CircleCenterRow[i];
                        cColumn += CircleCenterCol[i];
                    }
                    HOperatorSet.GenCrossContourXld(out centerCont, cRow / 2, cColumn / 2, 150, 0);
                }
                #endregion
                //5.0X轴到圆的中心连线的角度
                HTuple hv_HandEyeHomMat = new HTuple();
                MyVisionBase.MyHomMatToHalcon(CurHandEyeHomMat, out hv_HandEyeHomMat);      //手眼标定矩阵格式的转换
                HTuple FitHomMat = new HTuple();
                HTuple CircleRowW = new HTuple(), CircleColW = new HTuple();
                HTuple CircleRowAdj = new HTuple();
                //5.1调整行坐标
                //CircleRowAdj =CircleCenterRow;
                CircleRowAdj = CircleCenterRow.Clone();
                MyVisionBase.AdjImgRow(ImgIn, ref CircleRowAdj);
                //5.2像素坐标调整到世界坐标,消除相机坐标系与世界坐标系之间的夹角
                HOperatorSet.AffineTransPoint2d(hv_HandEyeHomMat, CircleCenterCol, CircleRowAdj, out CircleColW, out CircleRowW);
                if (CircleColW.Length >= 2)
                {
                    if (CircleColW[0].D < CircleColW[1].D)
                    {
                        HOperatorSet.AngleLl(0, 0, 1, 0, CircleColW[0].D, CircleRowW[0].D, CircleColW[1].D, CircleRowW[1].D, out AngleLx); //圆中心连线与世界坐标X轴之间的夹角
                        HOperatorSet.AngleLl(0, 0, 1, 0, CircleCenterCol[0].D, CircleRowAdj[0].D, CircleCenterCol[1].D, CircleRowAdj[1].D, out AngleLxPixel);
                    }
                    else
                    {
                        HOperatorSet.AngleLl(0, 0, 1, 0, CircleColW[1].D, CircleRowW[1].D, CircleColW[0].D, CircleRowW[0].D, out AngleLx);
                        HOperatorSet.AngleLl(0, 0, 1, 0, CircleCenterCol[1].D, CircleRowAdj[1].D, CircleCenterCol[0].D, CircleRowAdj[0].D, out AngleLxPixel);
                    }
                    if ((Math.PI / 4) < AngleLxPixel && AngleLxPixel < (Math.PI * 0.75))
                    {
                        AngleLxPixel = AngleLxPixel - Math.PI / 2;
                    }
                    else if (AngleLxPixel < (-45.0 / 180.0 * Math.PI))
                    {
                        AngleLxPixel = AngleLxPixel + Math.PI / 2;
                    }
                }
                else if (CircleColW.Length == 1)
                {
                    AngleLxPixel = 0;
                    AngleLx      = 0;
                }
                //6.0生成图像坐标原点到示教中心的平移放射矩阵
                if (CircleColW.Length >= 2)
                {
                    HOperatorSet.VectorAngleToRigid(0, 0, 0, (CircleCenterRow[1].D + CircleCenterRow[0].D) / 2, (CircleCenterCol[1].D + CircleCenterCol[0].D) / 2, AngleLxPixel, out FitHomMat);
                }
                else if (CircleColW.Length == 1)
                {
                    HOperatorSet.VectorAngleToRigid(0, 0, 0, CircleCenterRow[0].D, CircleCenterCol[0].D, AngleLxPixel, out FitHomMat);
                }
                HTuple TapeCenterRow = new HTuple(), TapeCenterCol = new HTuple();
                HOperatorSet.AffineTransPoint2d(FitHomMat, TeachCirclePara.OffsetPixelY, TeachCirclePara.OffsetPixelX, out TapeCenterRow, out TapeCenterCol);  //计算出贴标中心在图像坐标系中的位置
                HObject LineContour = new HObject(), OffsetArrowContour = new HObject();
                HOperatorSet.GenContourPolygonXld(out LineContour, CircleCenterRow, CircleCenterCol);
                double Dist = new double();
                Dist = Math.Sqrt(TeachCirclePara.OffsetPixelX * TeachCirclePara.OffsetPixelX + TeachCirclePara.OffsetPixelY * TeachCirclePara.OffsetPixelY);
                //7.0定位中心指向实际贴胶的箭头
                if (CircleColW.Length >= 2)
                {
                    MyVisionBase.GenArrowContourXld(out OffsetArrowContour, (CircleCenterRow[1].D + CircleCenterRow[0].D) / 2, (CircleCenterCol[1].D + CircleCenterCol[0].D) / 2, TapeCenterRow, TapeCenterCol, Dist / 20, Dist / 20);
                }
                else if (CircleColW.Length == 1)
                {
                    MyVisionBase.GenArrowContourXld(out OffsetArrowContour, CircleCenterRow[0].D, CircleCenterCol[0].D, TapeCenterRow, TapeCenterCol, Dist / 20, Dist / 20);
                }

                HOperatorSet.ConcatObj(LineContour, OffsetArrowContour, out OffsetArrowContour);
                HOperatorSet.ConcatObj(OffsetArrowContour, CirContour, out CirContour);
                //8.0需要转换到平台坐标
                HTuple TransRow = new HTuple(), TransCol = new HTuple();
                //8.0图片原点调整到右下角
                MyVisionBase.AdjImgRow(ImgIn, ref TapeCenterRow);
                HOperatorSet.AffineTransPoint2d(hv_HandEyeHomMat, TapeCenterCol, TapeCenterRow, out TransCol, out TransRow);
                //9.0减去平台的坐标,得到产品相对于平台旋转中心的坐标,下相机加上平台坐标,得到胶带在平台中的绝对位置

                if (ObjContour != null)
                {
                    ObjContour.Dispose();
                }
                if (CircleObj != null)
                {
                    CircleObj.Dispose();
                }

                if (IsUp)
                {
                    CenterRow = TransRow.D - CurMotionPos.Row;
                    CenterCol = TransCol.D - CurMotionPos.Col;
                }

                else
                {
                    CenterRow = TransRow.D + CurMotionPos.Row;
                    CenterCol = TransCol.D + CurMotionPos.Col;
                }
                RotAngle = AngleLx.D;
                St_Position NowPos = new St_Position();
                // St_Position NowPos = MotionController.GetCurrentPos(true);
                //if (TeachCirclePara.IsThreeFeeder)
                //{
                //    HTuple DistMarkToFinger = new HTuple();
                //    HOperatorSet.DistancePl((CircleCenterCol[0].D + CircleCenterCol[1].D) / 2.0, (CircleCenterRow[0].D + CircleCenterRow[1].D) / 2.0,
                //                             CircleCenterCol[2].D, CircleCenterRow[2].D, CircleCenterCol[3].D, CircleCenterRow[3].D, out DistMarkToFinger);
                //    HTuple Sx = new HTuple(), Sy = new HTuple(), Phi = new HTuple(), Theta = new HTuple(), Tx = new HTuple(), Ty = new HTuple();
                //    HOperatorSet.HomMat2dToAffinePar(hv_HandEyeHomMat, out Sx, out Sy, out Phi, out Theta, out Tx, out Ty);
                //    DistMarkToFinger = DistMarkToFinger.D * Sx;
                //    double OffSetX0 = 0,OffSetX1 =0;
                //    OffSetX0 = (DistMarkToFinger.D - 3.5) / 2;
                //    //CenterCol = CenterCol - OffSetX0 + 0.3 * OffSetX0;//laobanyaojiade
                //    CenterCol = CenterCol - OffSetX0;
                //    OffSetX1 = DistMarkToFinger.D - 3.5;
                //    //if (DistMarkToFinger.D < TeachCirclePara.DistMarkToFinger)
                //    //    return En_ImageProcessResult.产品三号位置弯曲;
                //   // Logger.Pop1(string.Format("Y轴位置={0},变形量={1}", CurMotionPos.Row, OffSetX0), "产品变形量");
                //    if (Math.Abs(OffSetX1) > 0.3)
                //        return En_ImageProcessResult.产品三号位置弯曲;
                //}
                if (double.IsNaN(CenterCol) || double.IsNaN(CenterRow))
                {
                    Logger.PopError("找圆定位出错坐标非数字");
                    return(En_ImageProcessResult.找圆定位出错坐标非数字);
                }

                return(En_ImageProcessResult.OK);
            }
            catch (Exception e)
            {
                Logger.PopError("CircleTypePos.FindCenter:" + e.Message + e.Source);
                return(En_ImageProcessResult.找圆失败);
            }
        }
Ejemplo n.º 15
0
        public static En_ImageProcessResult FindCenter1(HObject ImgIn, St_CirclesParam TeachCirclePara0, HTuple Row1, HTuple Col1, HTuple Theta1,
                                                        HTuple Row2, HTuple Col2, HTuple Theta2, out HTuple CircleCenterRow, out HTuple CircleCenterCol, out HTuple CircleR, out HObject circleCont, out HObject CirContour, out HObject centerCont)
        {
            CircleCenterRow = new HTuple();
            CircleCenterCol = new HTuple();
            CircleR         = new HTuple();
            St_CirclesParam TeachCirclePara;

            TeachCirclePara = TeachCirclePara0;
            HObject ObjContour = new HObject();

            CirContour = new HObject();
            HOperatorSet.GenEmptyObj(out CirContour);
            HObject CircleObj = new HObject();

            centerCont = new HObject();
            circleCont = new HObject();
            try
            {
                //1.0调整示教的坐标
                MyVisionBase.VectorAngleToTransPt(Row1, Col1, Theta1, Row2, Col2, Theta2, TeachCirclePara0.CenterRows, TeachCirclePara0.CenterCols, out TeachCirclePara.CenterRows,
                                                  out TeachCirclePara.CenterCols);
                HTuple  CircleRows = new HTuple(), CircleCols = new HTuple();
                HObject RoiContour = new HObject();
                HTuple  ResultRows = new HTuple(), ResultCols = new HTuple(), ArcType0 = new HTuple();
                HTuple  CircleRadius = new HTuple();
                HTuple  StartPhi = new HTuple(), EndPhi = new HTuple(), PointOrder = new HTuple(), IsTrueFlag = new HTuple();
                int     cirCount;
                #region  //用示教的参数找出圆
                for (int i = 0; i < TeachCirclePara.CenterRows.Count; i++)
                {
                    //2.0利用存下来的圆的信息,创建圆的边缘点坐标
                    MyVisionBase.GenCirclePts2(TeachCirclePara.CenterRows[i], TeachCirclePara.CenterCols[i], TeachCirclePara.CircleRs[i], TeachCirclePara.StartPhis[i],
                                               TeachCirclePara.EndPhis[i], TeachCirclePara.PointOrders[i], out CircleRows, out CircleCols);
                    //3.0找出边缘点用来拟合圆
                    MyVisionBase.spoke2(ImgIn, TeachCirclePara.Elements[i], TeachCirclePara.DetectHeights[i], 2, 2, TeachCirclePara.Thresholds[i], "all", "first", CircleRows, CircleCols,
                                        TeachCirclePara.Directs[i], out ResultRows, out ResultCols, out ArcType0);
                    HOperatorSet.GenCrossContourXld(out CircleObj, ResultRows, ResultCols, TeachCirclePara.CircleRs[i] / 10, 0.6);
                    HOperatorSet.ConcatObj(CircleObj, CirContour, out CirContour);
                    cirCount = CirContour.CountObj();
                    //4.拟合圆找到圆的中心
                    HObject CircleContour = new HObject();
                    HTuple  FitRow = new HTuple(), FitCol = new HTuple(), FitR = new HTuple(), SartP = new HTuple(), EndP = new HTuple(), IsFlag = new HTuple();
                    MyVisionBase.PtsToBestCircle1(out CircleObj, ResultRows, ResultCols, 4, "arc", out FitRow, out FitCol, out FitR, out SartP, out EndP, out IsFlag);
                    if (Math.Abs(TeachCirclePara.CircleRs[i] - FitR.D) > TeachCirclePara.CircleRs[i] * 0.5)
                    {
                        if (ObjContour != null)
                        {
                            ObjContour.Dispose();
                        }
                        if (CircleObj != null)
                        {
                            CircleObj.Dispose();
                        }
                        Logger.PopError("拟合的圆的半径和理论值偏差过大。");
                        return(En_ImageProcessResult.圆半径和理论值偏差过大);
                    }
                    cirCount = CircleObj.CountObj();
                    HOperatorSet.ConcatObj(CircleObj, CirContour, out CirContour);
                    CircleCenterRow[i] = FitRow.D;
                    CircleCenterCol[i] = FitCol.D;
                    CircleRadius[i]    = FitR.D;
                    StartPhi[i]        = SartP.D;
                    EndPhi[i]          = EndP.D;
                    IsTrueFlag[i]      = IsFlag[0];
                }
                CircleR = CircleRadius;
                #endregion
                HTuple AngleLxPixel = new HTuple();
                if (CircleCenterRow.TupleLength() != TeachCirclePara0.CenterRows.Count)
                {
                    if (ObjContour != null)
                    {
                        ObjContour.Dispose();
                    }
                    if (CircleObj != null)
                    {
                        CircleObj.Dispose();
                    }
                    Logger.PopError("CircleTypePos.FindCenter" + "没有找到圆");
                    return(En_ImageProcessResult.圆的数量少于示教的数量);
                }
                #region  //生成圆心十字轮廓,非功能性代码
                HTuple cRow = new HTuple(), cColumn = new HTuple();
                int    count = CircleCenterRow.TupleLength();
                cRow    = 0;
                cColumn = 0;
                if (count < 4)
                {
                    for (int i = 0; i < count; i++)
                    {
                        cRow    += CircleCenterRow[i];
                        cColumn += CircleCenterCol[i];
                    }
                    HOperatorSet.GenCrossContourXld(out centerCont, cRow / count, cColumn / count, 150, 0);
                }
                else if (count == 4)
                {
                    for (int i = 0; i < 2; i++)
                    {
                        cRow    += CircleCenterRow[i];
                        cColumn += CircleCenterCol[i];
                    }
                    HOperatorSet.GenCrossContourXld(out centerCont, cRow / 2, cColumn / 2, 150, 0);
                }
                #endregion
                //5.0X轴到圆的中心连线的角度
                HTuple FitHomMat    = new HTuple();
                HTuple CircleRowAdj = new HTuple();
                //5.1调整行坐标
                //CircleRowAdj =CircleCenterRow;
                CircleRowAdj = CircleCenterRow.Clone();
                MyVisionBase.AdjImgRow(ImgIn, ref CircleRowAdj);
                //5.2像素坐标调整到世界坐标,消除相机坐标系与世界坐标系之间的夹角
                if (CircleCenterCol.Length >= 2)
                {
                    if (CircleCenterCol[0].D < CircleCenterCol[0].D)
                    {
                        HOperatorSet.AngleLl(0, 0, 1, 0, CircleCenterCol[0].D, CircleRowAdj[0].D, CircleCenterCol[1].D, CircleRowAdj[1].D, out AngleLxPixel); //圆中心连线与相机坐标X轴之间的夹角
                    }
                    else
                    {
                        HOperatorSet.AngleLl(0, 0, 1, 0, CircleCenterCol[1].D, CircleRowAdj[1].D, CircleCenterCol[0].D, CircleRowAdj[0].D, out AngleLxPixel);
                    }
                    if ((Math.PI / 4) < AngleLxPixel && AngleLxPixel < (Math.PI * 0.75))
                    {
                        AngleLxPixel = AngleLxPixel - Math.PI / 2;
                    }
                    else if (AngleLxPixel < (-45.0 / 180.0 * Math.PI))
                    {
                        AngleLxPixel = AngleLxPixel + Math.PI / 2;
                    }
                }
                else if (CircleCenterCol.Length == 1)
                {
                    AngleLxPixel = 0;
                }
                //6.0生成图像坐标原点到示教中心的平移放射矩阵
                if (CircleCenterCol.Length >= 2)
                {
                    HOperatorSet.VectorAngleToRigid(0, 0, 0, (CircleCenterRow[1].D + CircleCenterRow[0].D) / 2, (CircleCenterCol[1].D + CircleCenterCol[0].D) / 2, AngleLxPixel, out FitHomMat);
                }
                else if (CircleCenterCol.Length == 1)
                {
                    HOperatorSet.VectorAngleToRigid(0, 0, 0, CircleCenterRow[0].D, CircleCenterCol[0].D, AngleLxPixel, out FitHomMat);
                }
                HTuple TapeCenterRow = new HTuple(), TapeCenterCol = new HTuple();
                HOperatorSet.AffineTransPoint2d(FitHomMat, TeachCirclePara.OffsetPixelY, TeachCirclePara.OffsetPixelX, out TapeCenterRow, out TapeCenterCol);  //计算出贴标中心在图像坐标系中的位置
                HObject LineContour = new HObject(), OffsetArrowContour = new HObject();
                HOperatorSet.GenContourPolygonXld(out LineContour, CircleCenterRow, CircleCenterCol);
                double Dist = new double();
                Dist = Math.Sqrt(TeachCirclePara.OffsetPixelX * TeachCirclePara.OffsetPixelX + TeachCirclePara.OffsetPixelY * TeachCirclePara.OffsetPixelY);
                ////7.0定位中心指向实际贴胶的箭头
                //if (CircleCenterCol.Length >= 2)
                //    MyVisionBase.GenArrowContourXld(out OffsetArrowContour, (CircleCenterRow[1].D + CircleCenterRow[0].D) / 2, (CircleCenterCol[1].D + CircleCenterCol[0].D) / 2, TapeCenterRow, TapeCenterCol, Dist / 20, Dist / 20);
                //else if (CircleCenterCol.Length == 1)
                //    MyVisionBase.GenArrowContourXld(out OffsetArrowContour, CircleCenterRow[0].D, CircleCenterCol[0].D, TapeCenterRow, TapeCenterCol, Dist / 20, Dist / 20);

                //HOperatorSet.ConcatObj(LineContour, OffsetArrowContour, out OffsetArrowContour);
                //HOperatorSet.ConcatObj(OffsetArrowContour, CirContour, out CirContour);
                ////8.0需要转换到平台坐标
                //HTuple TransRow = new HTuple(), TransCol = new HTuple();
                ////8.0图片原点调整到右下角
                //MyVisionBase.AdjImgRow(ImgIn, ref TapeCenterRow);
                //HOperatorSet.AffineTransPoint2d(hv_HandEyeHomMat, TapeCenterCol, TapeCenterRow, out TransCol, out  TransRow);
                ////9.0减去平台的坐标,得到产品相对于平台旋转中心的坐标,下相机加上平台坐标,得到胶带在平台中的绝对位置
                if (ObjContour != null)
                {
                    ObjContour.Dispose();
                }
                if (CircleObj != null)
                {
                    CircleObj.Dispose();
                }
                return(En_ImageProcessResult.OK);
            }
            catch (Exception e)
            {
                Logger.PopError("CircleTypePos.FindCenter:" + e.Message + e.Source);
                return(En_ImageProcessResult.找圆失败);
            }
        }
    // 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;
    }
Ejemplo n.º 17
0
        /// <summary>
        /// 拟合参数的示教函数
        /// </summary>
        public void TeachProcess()
        {
            ShowRoiFlag = true;
            HObject RoiContour = new HObject();
            //1.0获取图像,调节曝光时间
            HTuple ResultRows = new HTuple(), ResultCols = new HTuple();

            //2.0示教参数
            #region  //直线参数示教
            //2.1画出直线的检测区域
            bool CurIsOk = true;
            view1.AddViewImage(GrabedImg);
            while (ShowRoiFlag)
            {
                //2.2显示直线的检测ROI,调整ROI的参数
                Thread.Sleep(50);
                view1.Refresh();
                view1.AddImage(GrabedImg);
                CurIsOk = MyVisionBase.gen_rake_ROI(GrabedImg, out RoiContour, Element, DetectHeight, 2,
                                                    LineRow1, LineCol1, LineRow2, LineCol2);
                if (!CurIsOk)
                {
                    break;
                }
                view1.SetDraw("blue", "margin");
                view1.AddViewObject(RoiContour);
                //2.3利用卡尺工具找出边界点
                CurIsOk = MyVisionBase.Rake(GrabedImg, out RoiContour, Element, DetectHeight, 2, 2, Threshold,
                                            "all", "first", LineRow1, LineCol1,
                                            LineRow2, LineCol2, out ResultRows, out ResultCols);
                //减去中间不要的点
                int Count       = ResultRows.Length;
                int ClipLength  = (int )(Count * CenterClip);
                int StartClipNo = (int)((Count - ClipLength) * 0.5);
                if ((Count - ClipLength) > 4 && ClipLength > 0)
                {
                    for (int i = 0; i < ClipLength; i++)
                    {
                        ResultRows = ResultRows.TupleRemove(StartClipNo);
                        ResultCols = ResultCols.TupleRemove(StartClipNo);
                    }
                }
                if (!CurIsOk)
                {
                    break;
                }
                //2.4
                HObject LinePtCross = new HObject();
                //2.5显示拟合的点
                HOperatorSet.GenCrossContourXld(out LinePtCross, ResultRows, ResultCols, 12, 0.7);
                view1.SetDraw("green", "margin");
                view1.AddViewObject(LinePtCross);
                //2.6直线拟合
                HObject FitLineCotour = new HObject();
                HTuple  FitLineRow1 = new HTuple(), FitLineCol1 = new HTuple(), FitLineRow2 = new HTuple(),
                        FitLineCol2 = new HTuple(), PtToLineDist = new HTuple();
                CurIsOk = MyVisionBase.PtsToBestLine(out FitLineCotour, ResultRows, ResultCols, 3,
                                                     out FitLineRow1, out FitLineCol1, out FitLineRow2, out FitLineCol2, out PtToLineDist);
                if (CurIsOk)
                {
                    HTuple AngleLx = new HTuple();
                    HOperatorSet.AngleLl(0, 0, 1, 0, FitLineCol1, FitLineRow1, FitLineCol2, FitLineRow2, out AngleLx);  //直线1与X轴之间的夹角
                    HTuple DistP1L = new HTuple(), DistP2L = new HTuple();
                    HOperatorSet.DistancePl(LineRow10, LineCol10, FitLineRow1, FitLineCol1, FitLineRow2, FitLineCol2, out DistP1L);
                    HOperatorSet.DistancePl(LineRow20, LineCol20, FitLineRow1, FitLineCol1, FitLineRow2, FitLineCol2, out DistP2L);
                    AngleLx = AngleLx.D / Math.PI * 180.0;
                    if (Math.Abs(AngleLx.D) % 90 < 10 && DistP1L.D < 10 && DistP2L.D < 10)
                    {
                        HOperatorSet.ProjectionPl(LineRow1, LineCol1, FitLineRow1, FitLineCol1, FitLineRow2, FitLineCol2,
                                                  out LineRow1, out LineCol1);
                        HOperatorSet.ProjectionPl(LineRow2, LineCol2, FitLineRow1, FitLineCol1, FitLineRow2, FitLineCol2,
                                                  out LineRow2, out LineCol2);
                    }
                }
                if (!CurIsOk)
                {
                    break;
                }
                //2.7显示拟合直线
                view1.SetDraw("red", "margin");
                view1.AddViewObject(FitLineCotour);
                view1.Repaint();
                Thread.Sleep(1500);
            }
            #endregion
        }