private double DistanceToClosestHandle2(double row, double col)
        {
            HTuple data = base.GetModeData();

            double[] val = new double[_numHandles];
            val[0] = HalconDotNet.HMisc.DistancePp(row, col, data[0], data[1]);

            for (int i = 1; i < _numHandles; i++)
            {
                val[i] = 99999;
            }

            HHomMat2D mat2D          = new HHomMat2D();
            HTuple    init_Arrow_Row = data[0];
            HTuple    init_Arrow_Col = data[1] + data[3] * 0.8;

            mat2D = mat2D.HomMat2dRotate(data[2].D, data[0], data[1]);
            double arrow_Row;
            double arrow_Col;

            arrow_Row            = mat2D.AffineTransPoint2d(init_Arrow_Row, init_Arrow_Col, out arrow_Col);
            val[_numHandles - 1] = HalconDotNet.HMisc.DistancePp(row, col, arrow_Row, arrow_Col);

            double minvalue = 0.0;
            int    idx      = 0;

            if (this.MinValueAndIndex(val, out minvalue, out idx))
            {
                this._activeHandleIdx = idx;
            }
            return(minvalue);
        }
        public void RotateTrans(double x, double y, double r, out double tx, out double ty)
        {
            HHomMat2D mat2D = new HHomMat2D();

            mat2D = mat2D.HomMat2dRotate((HTuple)r, RotateCircle.Center.X, RotateCircle.Center.Y);
            HTuple qx, qy;

            HOperatorSet.AffineTransPoint2d(mat2D, x, y, out qx, out qy);
            tx = qx[0].D;
            ty = qy[0].D;
        }
示例#3
0
        public ToolResult GetResult()
        {
            try
            {
                int             imageWidt, imageHeight;
                HMetrologyModel metrologyModel = new HMetrologyModel();
                HTuple          startRow, startCol, endRow, endCol;
                HHomMat2D       matd  = new HHomMat2D();
                HHomMat2D       matd3 = matd.HomMat2dRotate(info.Rectangle2Angle, 0.0, 0.0);
                HHomMat2D       matd4 = matd3.HomMat2dTranslate(info.Rectangle2Row, info.Rectangle2Col);
                HOperatorSet.AffineTransPoint2d(matd4, new HTuple(0.0), new HTuple(-info.Rectangle2Length1), out startRow, out startCol);
                HOperatorSet.AffineTransPoint2d(matd4, new HTuple(0.0), new HTuple(info.Rectangle2Length1), out endRow, out endCol);
                //创建找线工具
                Image.GetImageSize(out imageWidt, out imageHeight);
                int[]  value  = new int[] { Convert.ToInt32(startRow.D), Convert.ToInt32(startCol.D), Convert.ToInt32(endRow.D), Convert.ToInt32(endCol.D) };
                HTuple values = new HTuple(value);
                metrologyModel.CreateMetrologyModel();
                metrologyModel.SetMetrologyModelImageSize(imageWidt, imageHeight);
                int index = metrologyModel.AddMetrologyObjectGeneric("line", values, 20, 5, 1, 30, "min_score", 0.6);
                #region 设置参数
                metrologyModel.SetMetrologyObjectParam(new HTuple(index), new HTuple("measure_length1"), new HTuple(Convert.ToInt32(info.measure_length1)));
                metrologyModel.SetMetrologyObjectParam(new HTuple(index), new HTuple("measure_length2"), new HTuple(Convert.ToInt32(info.measure_length2)));
                metrologyModel.SetMetrologyObjectParam(new HTuple(index), new HTuple("measure_distance"), new HTuple(info.measure_distance));
                metrologyModel.SetMetrologyObjectParam(new HTuple(index), new HTuple("num_measures"), new HTuple(info.num_measures));
                metrologyModel.SetMetrologyObjectParam(new HTuple(index), new HTuple("num_instances"), new HTuple(info.num_instances));
                metrologyModel.SetMetrologyObjectParam(new HTuple(index), new HTuple("measure_sigma"), new HTuple(info.measure_sigma));
                metrologyModel.SetMetrologyObjectParam(new HTuple(index), new HTuple("measure_threshold"), new HTuple(info.measure_threshold));
                metrologyModel.SetMetrologyObjectParam(new HTuple(index), new HTuple("measure_transition"), new HTuple(info.measure_transition.ToString()));
                #endregion

                HTuple   Row;
                HTuple   Col;
                HXLDCont modelXLD = metrologyModel.GetMetrologyObjectMeasures(index, "all", out Row, out Col);
                if (info.IsShowLineMeasureDetail)
                {
                    Window.SetColor("red");
                    modelXLD.DispXld(Window.Window);
                }

                return(ApplyMetrology(metrologyModel, index, info.ToolName));
            }
            catch (Exception e)
            {
                return(new ToolResult()
                {
                    ResultName = info.ToolName, Errormessage = e.ToString(), GetResultTime = DateTime.Now, IsSuccess = false
                });
            }
        }
示例#4
0
 private void GenVirtualBtn_Click(object sender, EventArgs e)
 {
     TryDebugBtn_Click(null, new EventArgs());
     if (FindCircleRow.Length >= 2 && FindCircleCol.Length >= 2)
     {
         HTuple OutLineRow1 = new HTuple(), OutLineCol1 = new HTuple(), OutLineRow2 = new HTuple(), OutLineCol2 = new HTuple();
         view1.ResetView();
         view1.AddImage(GrabedImg);
         //1.0找线
         TryDebugBtn_Click(null, new EventArgs());
         HTuple  LineAngle   = new HTuple();
         HObject LineArrow   = new HObject();
         HObject LitCirCross = new HObject();
         //2.0计算圆心连线与相机X轴的夹角
         OutLineRow1 = FindCircleRow[0].D;
         OutLineCol1 = FindCircleCol[0].D;
         OutLineRow2 = FindCircleRow[1].D;
         OutLineCol2 = FindCircleCol[1].D;
         LineTypePos.CalculateLineAng(OutLineRow1, OutLineCol1, OutLineRow2, OutLineCol2, out LineAngle, out LineArrow);
         LineAngle = LineAngle.D / Math.PI * 180.0;
         HTuple LitCirRow = new HTuple(), LitCircol = new HTuple();
         HTuple DistRr = (7.78 / 2.0) / 2.8 * FindCircleR[0].D;
         //3.0生成虚拟圆的圆心
         CircleTypePos.GenCirCenter(FindCircleRow[0].D, FindCircleCol[0].D, DistRr, out LitCirRow, out LitCircol);
         HOperatorSet.GenCrossContourXld(out LitCirCross, LitCirRow, LitCircol, 60, 0);
         view1.SetDraw("blue", "margin");
         view1.AddViewObject(LineArrow);
         view1.AddViewObject(LitCirCross);
         HHomMat2D MyHomMat = new HHomMat2D();
         MyHomMat = MyHomMat.HomMat2dRotate((TeachCirclePara.OffSetPixelTh + LineAngle) / 180.0 * Math.PI, FindCircleRow[0], FindCircleCol[0]);
         //4.0旋转生成圆的圆心
         MyHomMat.AffineTransPixel(LitCirRow, LitCircol, out LitCirRow, out LitCircol);
         HOperatorSet.GenCrossContourXld(out LitCirCross, LitCirRow, LitCircol, 60, 0);
         HObject FirstCirCont = new HObject(), SecondCirCont = new HObject();
         HOperatorSet.GenCircleContourXld(out FirstCirCont, LitCirRow[0].D, LitCircol[0].D, 60, 0, 2 * Math.PI, "positive", 0.1);
         HOperatorSet.GenCircleContourXld(out SecondCirCont, LitCirRow[1].D, LitCircol[1].D, 40, 0, 2 * Math.PI, "positive", 0.1);
         HOperatorSet.ConcatObj(LitCirCross, FirstCirCont, out LitCirCross);
         HOperatorSet.ConcatObj(LitCirCross, SecondCirCont, out LitCirCross);
         view1.SetDraw("red", "margin");
         view1.AddViewObject(LitCirCross);
     }
 }
示例#5
0
        public HXLDCont GetMatchModelCont(int index)
        {
            HXLDCont hXLD = GetModelCont();

            if (hXLD == null)
            {
                return(null);
            }

            HXLDCont cont = null;

            if (index < OutputResult.Count)
            {
                HHomMat2D mat2d = new HHomMat2D();
                mat2d = mat2d.HomMat2dScale(OutputResult.Scale[index], OutputResult.Scale[index], 0d, 0d);
                mat2d = mat2d.HomMat2dRotate(OutputResult.Angle[index], 0d, 0d);
                mat2d = mat2d.HomMat2dTranslate(OutputResult.Row[index].D, OutputResult.Col[index].D);

                cont = hXLD.AffineTransContourXld(mat2d);
            }

            return(cont);
        }
示例#6
0
        public bool FindCircle(HObject img)
        {
            HTuple  Row         = new HTuple();
            HTuple  Col         = new HTuple();
            HTuple  Distance    = new HTuple();
            HTuple  outRow      = new HTuple();
            HTuple  outCol      = new HTuple();
            HTuple  outAmp      = new HTuple();
            HTuple  outDistance = new HTuple();
            HRegion regionCount = new HRegion();

            regionCount.GenEmptyRegion();
            HXLDCont contour = new HXLDCont();

            if (img == null)
            {
                return(false);
            }
            HTuple imgWidth, imgHeight;
            HTuple hms = null;

            HOperatorSet.GetImageSize(img, out imgWidth, out imgHeight);

            double xCenter, yCenter, Center_radius, angle, angleSplit;

            angle = 0;
            //分割角度
            angleSplit    = 2 * 3.1415926 / (Hnum);
            xCenter       = in_col[0].D + data_col;
            yCenter       = in_row[0].D + data_row;
            Center_radius = in_radius[0].D;

            HHomMat2D hom = new HHomMat2D();

            hom.HomMat2dIdentity();
            hom = hom.HomMat2dRotate(angleSplit, yCenter, xCenter);
            double rect_row, rect_col, angleStart, L1, L2;

            //分割矩形的中心坐标
            rect_row = yCenter;
            rect_col = xCenter + Center_radius;
            L1       = HLengh[0].D / 2;
            L2       = Hwid[0].D / 2;
            if (Hdirection != "in2out")
            {
                //由外向内
                angleStart = angle + (new HTuple(180)).TupleRad();
            }
            else
            {
                angleStart = angle;
            }

            for (int i = 0; i < Hnum[0].I; ++i)
            {
                if (m_bShowRectang2)
                {
                    contour.Dispose();
                    contour.GenRectangle2ContourXld(rect_row, rect_col, angleStart + angleSplit * i, L1, L2);
                    HRegion region;
                    region      = contour.GenRegionContourXld("margin");
                    regionCount = regionCount.Union2(region);
                    region.Dispose();
                }
                try
                {//out_row, out_col, out_radius;
                    HOperatorSet.GenMeasureRectangle2(rect_row, rect_col, angleStart + angleSplit * i, L1,
                                                      L2, imgWidth, imgHeight, "nearest_neighbor", out hms);
                    HOperatorSet.MeasurePos(img, hms, Hsomth, Hamp, Htransition, Hselect,
                                            out outRow, out outCol, out outAmp, out outDistance);
                }
                catch { continue; }
                if (Hselect == "all" && outRow.Length > 1)
                {
                    HTuple hIndex = outAmp.TupleSortIndex();
                    int    nMax   = hIndex[hIndex.Length - 1];
                    Row.Append(outRow[nMax]);
                    Col.Append(outCol[nMax]);
                }
                else
                {
                    Row.Append(outRow);
                    Col.Append(outCol);
                }
                hom.AffineTransPixel(rect_row, rect_col, out rect_row, out rect_col);
            }
            HOperatorSet.CloseMeasure(hms);
            if (m_bShowRectang2)
            {
                m_hRectang2.Dispose();
                m_hRectang2 = regionCount.GenContoursSkeletonXld(1, "filter");
            }
            if (Row.Length > Hnum[0].D / 3)
            {
                HXLDCont counter = new HXLDCont();
                counter.GenContourPolygonXld(Row, Col);

                HTuple startphi, endphi;
                HTuple pointorder;
                counter.FitCircleContourXld("atukey", -1, 0, 0, 3, 2,
                                            out out_row, out out_col, out out_radius, out startphi, out endphi, out pointorder);

                HTuple rowDrop = new HTuple();
                HTuple colDrop = new HTuple();
                if (Row.Length - 5 > m_nNumDropPoints && m_nNumDropPoints > 0)
                {
                    HTuple distance = new HTuple();
                    for (int i = 0; i < Row.Length; ++i)
                    {
                        double dis = HMisc.DistancePp(Row[i], Col[i], xCenter, yCenter);
                        distance.Append(Math.Abs(dis - Center_radius));
                    }
                    HTuple index = distance.TupleSortIndex();
                    index = index.TupleInverse();
                    for (int i = 0; i < m_nNumDropPoints; ++i)
                    {
                        int n = index[i];
                        rowDrop.Append(Row[n]);
                        colDrop.Append(Col[n]);
                    }
                    index = index.TupleFirstN(m_nNumDropPoints - 1);
                    Row   = Row.TupleRemove(index);
                    Col   = Col.TupleRemove(index);

                    counter.GenContourPolygonXld(Row, Col);
                    counter.FitCircleContourXld("atukey", -1, 0, 0, 3, 2,
                                                out out_row, out out_col, out out_radius, out startphi, out endphi, out pointorder);
                }
                m_hCircle.Dispose();
                m_centerCross.Dispose();
                if (m_bShowCircle)
                {
                    HOperatorSet.GenCrossContourXld(out m_centerCross, out_row, out_col, (out_radius / 2) + 1, (new HTuple(0)).TupleRad());
                    HOperatorSet.GenCircleContourXld(out m_hCircle, out_row, out_col, out_radius,
                                                     0, 3.1415926 * 2, "positive", 1);
                }
                m_hCross.Dispose();
                if (m_bShowCross)
                {
                    HOperatorSet.GenCrossContourXld(out m_hCross, Row, Col, 17, (new HTuple(45)).TupleRad());
                }

                Result_Array[0] = out_row;
                Result_Array[1] = out_col;
                Result_Array[2] = out_radius;
                return(true);
            }
            return(false);
        }