示例#1
0
        public bool WaitForClickPoints(double x, double y)
        {
            if (_clickedPointsPositionList.Count < _clickPoints)
            {
                var isNotTheSamePoint = _clickedPointsPositionList.Count == 0;
                var prevIdx           = _clickedPointsPositionList.Count - 1;
                var prevPoint         = prevIdx > -1 ? _clickedPointsPositionList[prevIdx] : null;
                if (prevPoint != null)
                {
                    isNotTheSamePoint = Math.Abs(prevPoint.ColBegin - x) > 0 || Math.Abs(prevPoint.RowBegin - y) > 0;
                }
                if (isNotTheSamePoint)
                {
                    _clickedPointsPositionList.Add(new PositionModel()
                    {
                        ColBegin = x, RowBegin = y
                    });
                }
            }

            _initPointsDone = _clickedPointsPositionList.Count == _clickPoints;
            if (_initPointsDone)
            {
                //產生 Rectangle 資訊
                midR = _clickedPointsPositionList.Average(p => p.RowBegin);
                midC = _clickedPointsPositionList.Average(p => p.ColBegin);

                //長度
                var beginRow = _clickedPointsPositionList[0].RowBegin;
                var beginCol = _clickedPointsPositionList[0].ColBegin;
                var endRow   = _clickedPointsPositionList[1].RowBegin;
                var endCol   = _clickedPointsPositionList[1].ColBegin;

                var distance = HMisc.DistancePp(beginRow, beginCol, endRow, endCol);
                length1 = distance / 2.0;
                length2 = 10;

                //決定角度
                creakPointPhi = phi = HMisc.AngleLx(beginRow, beginCol, endRow, endCol) * -1;

                rowsInit = new HTuple(new double[] { -1.0, -1.0, 1.0,
                                                     1.0, 0.0, 0.0 });
                colsInit = new HTuple(new double[] { -1.0, 1.0, 1.0,
                                                     -1.0, 0.0, 0.6 });
                //order        ul ,  ur,   lr,  ll,   mp, arrowMidpoint
                hom2D = new HHomMat2D();
                tmp   = new HHomMat2D();

                updateHandlePos();
            }
            return(_initPointsDone);
        }
示例#2
0
        public void FindLine(HImage image, double Row, double Column, double Phi, double Length1, double Length2, ushort pointNum, ushort EdgeWidth, ushort EdgeThresold, EdgePolarEnum edgePolar, EdgeTypeEnum edgeType, out PointF firstPoint, out PointF endPoint, out double angle, out HXLDCont[] cross, out HXLDCont line)
        {
            double[] row          = new double[pointNum];
            double[] column       = new double[pointNum];
            HTuple   _rowEdges    = new HTuple();
            HTuple   _columnEdges = new HTuple();

            image.GetImageSize(out int width, out int height);
            for (int i = 0; i <= (pointNum - 1) / 2; i++)//遍历搜索点数的一半
            {
                row[i]                   = Row - (Length2 - 2.0 * Length2 * (i + 1) / (pointNum + 1)) * Math.Cos(Phi);
                column[i]                = Column - (Length2 - 2.0 * Length2 * (i + 1) / (pointNum + 1)) * Math.Sin(Phi);
                row[pointNum - 1 - i]    = 2 * Row - row[i];
                column[pointNum - 1 - i] = 2 * Column - column[i];
            }
            for (int i = 0; i < pointNum - 1; i++)
            {
                HMeasure measure = new HMeasure(row[i], column[i], Phi, Length1, EdgeWidth, width, height, "nearest_neighbor");                                                              //获得测量矩形
                measure.MeasurePos(image, 1, EdgeThresold, edgePolar.ToString(), edgeType.ToString(), out HTuple rowEdge, out HTuple columnEdge, out HTuple Amplitude, out HTuple distance); //提取垂直于矩形或环形弧的直边
                if (rowEdge.Length > 0 && columnEdge.Length > 0)
                {
                    _rowEdges    = _rowEdges.TupleConcat(rowEdge);
                    _columnEdges = _columnEdges.TupleConcat(columnEdge);
                }
            }
            if (_rowEdges.Length > 5)
            {
                HXLDCont Contour = new HXLDCont(_rowEdges, _columnEdges);
                Contour.SmoothContoursXld(5);
                Contour.FitLineContourXld("tukey", -1, 0, 5, 2, out double rowBegin, out double columnBegin, out double rowEnd, out double columnEnd, out double nr, out double nc, out double dist);
                Contour.GenContourPolygonXld(new HTuple(rowBegin, rowEnd), new HTuple(columnBegin, columnEnd));
                cross = new HXLDCont[_rowEdges.Length];
                for (int i = 0; i < _rowEdges.Length; i++)
                {
                    HXLDCont _cross = new HXLDCont();
                    _cross.GenCrossContourXld(_rowEdges[i], _columnEdges[i], 30d, 0);
                    cross[i] = _cross;
                }
                firstPoint = new PointF((float)columnBegin, (float)rowBegin);
                endPoint   = new PointF((float)columnEnd, (float)rowEnd);
                angle      = HMisc.AngleLx(rowBegin, columnBegin, rowEnd, columnEnd) * 180 / Math.PI;
                line       = Contour;
            }
            else
            {
                firstPoint = new PointF(0, 0);
                endPoint   = new PointF(0, 0);
                cross      = null;
                line       = null;
                angle      = 0;
            }
        }
示例#3
0
        /// <summary>
        /// Defines the measuring field of a linear ROI.
        /// </summary>
        /// <param name="line">
        /// Model data for a linear interactive ROI
        /// </param>
        /// <param name="width">Half width of (rectangular) measure ROI</param>
        /// <returns>Model data describing a linear measuring field</returns>
        private HTuple GenSurRect2(HTuple line, double width)
        {
            double row1 = line[0];
            double col1 = line[1];
            double row2 = line[2];
            double col2 = line[3];

            double phi     = HMisc.AngleLx(row1, col1, row2, col2);
            double length1 = (HMisc.DistancePp(row1, col1, row2, col2)) / 2.0;
            double length2 = width;
            double rowM    = (row1 + row2) / 2;
            double colM    = (col1 + col2) / 2;

            return(new HTuple(new double[] { rowM, colM, phi, length1, length2 }));
        }
        /// <summary>
        /// Defines the measuring field of a linear ROI.
        /// </summary>
        /// <param name="line">
        /// Model data for a linear interactive ROI
        /// </param>
        /// <param name="width">Half width of (rectangular) measure ROI</param>
        /// <returns>Model data describing a linear measuring field</returns>
        private HTuple GenSurRect2(HTuple line, double width)
        {
            double row1 = line[0];
            double col1 = line[1];
            double row2 = line[2];
            double col2 = line[3];

            //half 長
            double length1 = (HMisc.DistancePp(row1, col1, row2, col2)) / 2.0;
            //half 寬
            double length2 = width;

            if (mRoi is ROIRectangle2 || mRoi is ViewROI.SmartROIs.SmartPoint)
            {
                var midR      = line[0];
                var midC      = line[1];
                var mPhi      = line[2];
                var mlength1  = line[3];
                var mlength2  = line[4];
                var yDistance = (Math.Sin(mPhi) * mlength1);
                var xDistance = (Math.Cos(mPhi) * mlength1);

                row1 = midR - yDistance;            //起點R
                col1 = midC - xDistance;            //起點C
                row2 = midR + yDistance;            //終點R
                col2 = midC + xDistance;            //終點C

                length1 = mlength1;                 //half 長
                length2 = mlength2;                 // half 寬
            }


            double phi = HMisc.AngleLx(row1, col1, row2, col2);

            double rowM = (row1 + row2) / 2;
            double colM = (col1 + col2) / 2;

            return(new HTuple(new double[] { rowM, colM, phi, length1, length2 }));
        }
示例#5
0
        /// <summary>
        /// If calibration data is available and valid, then transform the
        /// distance between measure result edges into world coordinates,
        /// else leave them the same.
        /// </summary>
        public HTuple Distance(HTuple row1, HTuple col1, HTuple row2, HTuple col2, int shift)
        {
            HTuple   rows, cols, rowRect, colRect;
            HTuple   distance = new HTuple();
            HXLDCont contour;


            if (shift == 0)
            {
                if (mROIType == ROI.ROI_TYPE_CIRCLEARC)
                {
                    double cRow, cCol, radius, extent, phi1, phi2, phi, res, length, tmp;

                    cRow   = mROICoord[0].D;
                    cCol   = mROICoord[1].D;
                    radius = mROICoord[2].D;
                    extent = mROICoord[4].D;

                    HOperatorSet.TupleGenConst(new HTuple(row1.Length), 0.0, out distance);

                    for (int i = 0; i < distance.Length; i++)
                    {
                        phi1 = HMisc.AngleLx(cRow, cCol, row1[i].D, col1[i].D);
                        phi2 = HMisc.AngleLx(cRow, cCol, row2[i].D, col2[i].D);

                        if (extent < 0)
                        {
                            tmp  = phi1;
                            phi1 = phi2;
                            phi2 = tmp;
                        }

                        phi = phi2 - phi1;

                        if (phi < 0)
                        {
                            phi += 2 * Math.PI;
                        }

                        res = 0.05 * 24.0 / (radius * phi);

                        contour = new HXLDCont();
                        contour.GenEllipseContourXld(cRow, cCol, 0, radius, radius, phi1, phi2, "positive", res);
                        contour.GetContourXld(out rows, out cols);
                        Rectify(rows, cols, out rowRect, out colRect);
                        contour.Dispose();
                        contour.GenContourPolygonXld(rowRect, colRect);
                        length        = contour.LengthXld();
                        distance[i].D = length;
                        contour.Dispose();
                    }
                }
                else if (mROIType == ROI.ROI_TYPE_LINE)
                {
                    HTuple rRect1, cRect1, rRect2, cRect2;
                    Rectify(row1, col1, out rRect1, out cRect1);
                    Rectify(row2, col2, out rRect2, out cRect2);
                    distance = HMisc.DistancePp(rRect1, cRect1, rRect2, cRect2);
                }
                return(distance);
            }
            else
            {
                HTuple rClip1, cClip1, rShift2, cShift2;

                rClip1  = row1.TupleSelectRange(new HTuple(0), new HTuple(row1.Length - shift - 1));
                cClip1  = col1.TupleSelectRange(new HTuple(0), new HTuple(col1.Length - shift - 1));
                rShift2 = row2.TupleSelectRange(new HTuple(shift), new HTuple(row2.Length - 1));
                cShift2 = col2.TupleSelectRange(new HTuple(shift), new HTuple(col2.Length - 1));

                return(this.Distance(rClip1, cClip1, rShift2, cShift2, 0));
            }
        }
示例#6
0
        private void init()
        {
            this.NumHandles  = 2;           //移動用的 Handle, 1 middle point, 整條線
            _dependROIModels = new List <PositionModel>();
            if (_model != null)
            {
                //原始值
                _rawCenterRow = (_model.RowBegin + _model.RowEnd) / 2.0;
                _rawCenterCol = (_model.ColBegin + _model.ColEnd) / 2.0;
                _halfDistance = _model.Distance / 2.0;

                var radStart = HMisc.AngleLx(_rawCenterRow, _rawCenterCol, _model.RowBegin, _model.ColBegin);
                _phiStart = halconRadToNormalRad(radStart);

                var radEnd = HMisc.AngleLx(_rawCenterRow, _rawCenterCol, _model.RowEnd, _model.ColEnd);
                _phiEnd = halconRadToNormalRad(radEnd);

                this.NewCenterRow = (!_model.UserDefineCenterRow.HasValue) ? _rawCenterRow : _model.UserDefineCenterRow.Value;
                this.NewCenterCol = (!_model.UserDefineCenterCol.HasValue) ? _rawCenterCol : _model.UserDefineCenterCol.Value;

                this.ID = _model.ID;
                Name    = _model.Name;

                //_modelPoints = new List<PositionModel>() {
                //	new PositionModel(){ RowBegin =_model.RowBegin, ColBegin = _model.ColBegin },
                //	new PositionModel(){ RowBegin =_model.RowEnd, ColBegin = _model.ColEnd },
                //};

                if (_model.ROIs != null)
                {
                    var basicGeoType = new List <MeasureType>()
                    {
                        { MeasureType.FitLine },
                        { MeasureType.Line },
                        { MeasureType.Point },
                        { MeasureType.Circle }
                    };
                    for (var i = 0; i < _model.ROIs.Length; i++)
                    {
                        var roiModel = _model.ROIs[i];

                        PositionModel dto;
                        if (roiModel.RowBegin > 0 &&
                            roiModel.ColBegin > 0 &&
                            roiModel.RowEnd > 0 &&
                            roiModel.ColEnd > 0)
                        {
                            dto = new PositionModel()
                            {
                                RowBegin = roiModel.RowBegin,
                                ColBegin = roiModel.ColBegin,
                                RowEnd   = roiModel.RowEnd,
                                ColEnd   = roiModel.ColEnd,
                            };
                            _ROIModelPoints.Add(new PositionModel()
                            {
                                RowBegin = (roiModel.RowBegin + roiModel.RowEnd) / 2.0,
                                ColBegin = (roiModel.ColBegin + roiModel.ColEnd) / 2.0
                            });
                        }
                        else
                        {
                            dto = new PositionModel()
                            {
                                RowBegin = roiModel.RowBegin,
                                ColBegin = roiModel.ColBegin,
                            };
                            _ROIModelPoints.Add(new PositionModel()
                            {
                                RowBegin = (roiModel.RowBegin),
                                ColBegin = (roiModel.ColBegin)
                            });
                        }
                        if (basicGeoType.Contains(roiModel.GeoType))
                        {
                            _dependROIModels.Add(dto);
                        }
                    }
                    _ROIModelPoints = _ROIModelPoints.OrderBy(x => x.ColBegin).OrderBy(y => y.RowBegin).ToList();
                }
            }
        }
示例#7
0
        public bool WaitForClickPoints(double x, double y)
        {
            if (_clickedPointsPositionList.Count < _clickPoints)
            {
                _clickedPointsPositionList.Add(new PositionModel()
                {
                    ColBegin = x, RowBegin = y
                });
            }
            _initPointsDone = _clickedPointsPositionList.Count == _clickPoints;
            if (_initPointsDone)
            {
                //3 點求圓
                var rows = _clickedPointsPositionList.Select(p => p.RowBegin).ToArray();
                var cols = _clickedPointsPositionList.Select(p => p.ColBegin).ToArray();
                GeoOperatorSet.CalculateCircleCenter(rows, cols, out midR, out midC, out radius);

                //檢查 and notify
                _success = (midR > 0 && midC > 0 && radius > 0);
                if (_success)
                {
                    _FirstPoint = new GeoPoint()
                    {
                        Row = rows[0], Col = cols[0]
                    };
                    _SecondPoint = new GeoPoint()
                    {
                        Row = rows[1], Col = cols[1]
                    };
                    _EndPoint = new GeoPoint()
                    {
                        Row = rows[2], Col = cols[2]
                    };

                    //
                    sizeR = midR;
                    sizeC = midC - radius;

                    //toDo, 計算startPhi, extentPhi
                    // 角度為正,表示在圓心的上方,反之在下方
                    // 角度越大,表示越接近左上角,視為起始點
                    // 角度越小,視為終點
                    var agOne   = HMisc.AngleLx(midR, midC, _FirstPoint.Row, _FirstPoint.Col);
                    var agTwo   = HMisc.AngleLx(midR, midC, _SecondPoint.Row, _SecondPoint.Col);
                    var agThree = HMisc.AngleLx(midR, midC, _EndPoint.Row, _EndPoint.Col);
                    Dictionary <string, double> agDict = new Dictionary <string, double>()
                    {
                    };
                    agDict.Add("1", agOne);
                    agDict.Add("2", agTwo);
                    agDict.Add("3", agThree);

                    //逆排序,再取奇數, 即為 StartPhi, EndPhi
                    var angles = agDict.OrderByDescending(p => p.Value)
                                 .Where((p, idx) => idx % 2 == 0)
                                 .Select(p => p.Value).ToArray();
                    _startPhi = angles[0];
                    var endPhi = angles[1];

                    //計算延伸長度

                    /*
                     * 起始點在第一,二象限,則
                     */
                    _extentPhi = (_startPhi < 0) ? Math.Abs(_startPhi) - Math.Abs(endPhi)
                                                                                                : endPhi - _startPhi;
                    _pointOrder = "negative";                    //clockwise 畫弧

                    determineArcHandles();
                    updateArrowHandle();
                }
            }
            return(_initPointsDone);
        }