private bool IsGoodTilt(ReflectionedLine line)
 {
     if (Math.Atan(line.A) > 0)
     {
         return(true);
     }
     return(false);
 }
 private bool IsWeakAngleClose(ReflectionedLine firstLine, ReflectionedLine secondLine)
 {
     if ((180 / Math.PI) * Math.Abs(Math.Atan(firstLine.A) - Math.Atan(secondLine.A)) < WeakAngleDif)
     {
         return(true);
     }
     return(false);
 }
        private void Approximation()
        {
            LinearLeastSquares approx = new LinearLeastSquares(GetGraphicPoints());

            _rSquare            = approx.RSquares;
            _approxLine         = approx.Line;
            _relativeEstimation = approx.RelativeEstimation;
        }
 private bool IsGoodApproximation(ReflectionedLine approxLine, double rSquare, double relativeEstimation)
 {
     if ((180 / Math.PI) * Math.Abs(Math.Atan(approxLine.A)) < AngleLimit)
     {
         //if (relativeEstimation > aMax)
         return(true);
     }
     //else
     //  return false;
     else if (rSquare > RMax)
     {
         return(true);
     }
     return(false);
 }
        public LinearLeastSquares(List <IntPoint> sourcePoints)
        {
            _sourcePoints = sourcePoints;
            _n            = _sourcePoints.Count;
            sourcePoints.ForEach(point =>
            {
                _sumX  += point.X;
                _sumY  += point.Y;
                _sumXy += point.X * point.Y;
                _sumYy += (long)Math.Pow(point.Y, 2);
            });
            double a = (_n * _sumXy - _sumX * _sumY) / (_n * _sumYy - Math.Pow(_sumY, 2));
            double b = (_sumX - a * _sumY) / _n;

            Line = new ReflectionedLine(a, b);
        }
        private bool IsGoodApproximation(ReflectionedLine approxLine, double rSquare, double relativeEstimation)
        {
            double angle = Math.Abs(approxLine.Angle);

            if ((angle < AngleLimit) && (relativeEstimation > AMax))
            {
                return(true);
            }
            if ((angle < AngleLimit) && (relativeEstimation <= AMax))
            {
                return(false);
            }
            if (rSquare > RMax)
            {
                return(true);
            }
            return(false);
        }
示例#7
0
        private List <IntPoint> GetInliers(ReflectionedLine line, int inlierNum)
        {
            List <double> estimations = new List <double>();

            _sourcePoints.ForEach(
                sourcePoint => estimations.Add(Math.Pow(sourcePoint.X - line.GetX(sourcePoint.Y), 2)));

            List <double>   orderedEstimations  = estimations.OrderBy(x => x).ToList();
            double          estimationThreshold = orderedEstimations[inlierNum];
            List <IntPoint> inliers             = new List <IntPoint>();

            for (int i = 0; i < _sourcePointsCount; i++)
            {
                if (estimations[i] < estimationThreshold)
                {
                    inliers.Add(_sourcePoints[i]);
                }
            }
            return(inliers);
        }
示例#8
0
        private List <IntPoint> GetRansacPoints()
        {
            List <IntPoint> ransacPoints = new List <IntPoint>();
            double          bestRSquare  = -1 * Double.MaxValue;

            var syncTask = new Task(() =>
            {
                System.Threading.Tasks.Parallel.For(0, _iterations, i =>
                {
                    Random rnd = new Random();
                    List <IntPoint> samplePoints = GetSamplePoints(rnd, (int)(_sampleShare * _sourcePointsCount));

                    LinearLeastSquares ols      = new LinearLeastSquares(samplePoints);
                    ReflectionedLine approxLine = ols.Line;
                    List <IntPoint> inliers     = GetInliers(approxLine, (int)((1 - _outlierShare) * _sourcePointsCount));

                    //calculate rSquare of inliers with sampleApproxLine
                    int xMean = 0;
                    inliers.ForEach(point => xMean += point.X);
                    xMean = xMean / inliers.Count;

                    double rSquareNumerator   = 0;
                    double rSquareDenominator = 0;
                    inliers.ForEach(point =>
                    {
                        rSquareNumerator   += Math.Pow(point.X - approxLine.GetX(point.Y), 2);
                        rSquareDenominator += Math.Pow(point.X - xMean, 2);
                    });
                    double rSquare = 1 - rSquareNumerator / rSquareDenominator;
                    if (rSquare > bestRSquare)
                    {
                        bestRSquare  = rSquare;
                        ransacPoints = inliers;
                    }
                });
            });

            syncTask.RunSynchronously();

            return(ransacPoints);
        }
        public Image Step14DrawUltraSoundApproximation(ref VerificationStatus result, int relativeEstimationLimit)
        {
            if (!_debugMode)
            {
                throw new Exception("Can`t use this method in production mode");
            }

            ReflectionedLine drawingLine = _workingUltrasoundModA.ApproxLine;
            SimpleGrayImage  approxImage = new SimpleGrayImage(_workingUltrasoundModA.Image.Data);

            IntPoint startPoint = new IntPoint(drawingLine.GetX(ModATopIndention), ModATopIndention);
            IntPoint endPoint   = new IntPoint(drawingLine.GetX(_workingUltrasoundModA.Image.Rows - ModABottomIndention),
                                               _workingUltrasoundModA.Image.Rows - ModABottomIndention);

            approxImage.DrawGrayLine(startPoint, endPoint, 128);

            _ultrasoundModeAStatus = _workingUltrasoundModA.RelativeEstimation > relativeEstimationLimit ? VerificationStatus.Correct : VerificationStatus.Incorrect;
            result = _ultrasoundModeAStatus;

            return(approxImage.Bitmap);
        }
        public void Approximate(int topIndention, double sampleShare, double outlierShare, int iterations)
        {
            Ransac          linear     = new Ransac(sampleShare, outlierShare, iterations);
            List <IntPoint> leftPoints = new List <IntPoint>();

            LeftContour.Points.ForEach(point =>
            {
                if (point.Y >= topIndention)
                {
                    leftPoints.Add(point);
                }
            });
            List <IntPoint> rightPoints = new List <IntPoint>();

            RightContour.Points.ForEach(point =>
            {
                if (point.Y >= topIndention)
                {
                    rightPoints.Add(point);
                }
            });
            _leftApproximation  = linear.Approximate(leftPoints, leftPoints.Count, out _rSquareLeft, out _relativeEstimationLeft);
            _rightApproximation = linear.Approximate(rightPoints, rightPoints.Count, out _rSquareRight, out _relativeEstimationRight);
        }
        public VerificationStatus Classiffy(ElastoBlob targetObject, Segment fibroLine)
        {
            if (CheckForNull(targetObject, fibroLine))
            {
                return(VerificationStatus.NotCalculated);
            }
            int area = targetObject.Blob.Area;
            ReflectionedLine leftLine     = targetObject.LeftApproximation;
            ReflectionedLine rightLine    = targetObject.RightApproximation;
            double           rSquareLeft  = targetObject.RSquareLeft;
            double           rSquareRight = targetObject.RSquareRight;
            double           aLeft        = targetObject.RelativeEstimationLeft;
            double           aRight       = targetObject.RelativeEstimationRight;


            if (area < 4000)
            {
                return(VerificationStatus.Uncertain);
            }
            if (area < 6000)
            {
                if ((angleDifference(leftLine, fibroLine.Equation) < VeryStrongAngleDif) &&
                    (angleDifference(leftLine, rightLine) < VeryStrongAngleDif) &&
                    (IsGoodTilt(leftLine)) &&
                    IsGoodApproximation(leftLine, rSquareLeft, aLeft) &&
                    IsGoodApproximation(rightLine, rSquareRight, aRight))
                {
                    return(VerificationStatus.Correct);
                }
                return(VerificationStatus.Uncertain);
            }
            if (area < 8000)
            {
                if (!IsGoodApproximation(leftLine, rSquareLeft, aLeft) ||
                    !IsGoodApproximation(rightLine, rSquareRight, aRight))
                {
                    return(VerificationStatus.Uncertain);
                }
                if ((angleDifference(leftLine, fibroLine.Equation) < WeakAngleDif) &&
                    (angleDifference(leftLine, rightLine) < WeakAngleDif) &&
                    (IsGoodTilt(leftLine)))
                {
                    return(VerificationStatus.Correct);
                }
                return(VerificationStatus.Incorrect);
            }
            if (area < 10000)
            {
                if (!IsGoodApproximation(leftLine, rSquareLeft, aLeft) ||
                    !IsGoodApproximation(rightLine, rSquareRight, aRight))
                {
                    return(VerificationStatus.Uncertain);
                }
                if ((angleDifference(leftLine, fibroLine.Equation) < MeanAngleDif) &&
                    (angleDifference(leftLine, rightLine) < MeanAngleDif) &&
                    (IsGoodTilt(leftLine)))
                {
                    return(VerificationStatus.Correct);
                }
                return(VerificationStatus.Incorrect);
            }
            if (area < 13500)
            {
                if (!IsGoodApproximation(leftLine, rSquareLeft, aLeft) ||
                    !IsGoodApproximation(rightLine, rSquareRight, aRight))
                {
                    return(VerificationStatus.Uncertain);
                }
                if ((angleDifference(leftLine, fibroLine.Equation) < StrongAngleDif) &&
                    (angleDifference(leftLine, rightLine) < StrongAngleDif) &&
                    (IsGoodTilt(leftLine)))
                {
                    return(VerificationStatus.Correct);
                }
                return(VerificationStatus.Incorrect);
            }

            if (area < 21000)
            {
                if (IsGoodApproximation(leftLine, rSquareLeft, aLeft) &&
                    IsGoodApproximation(rightLine, rSquareRight, aRight))
                {
                    return(VerificationStatus.Incorrect);
                }
                return(VerificationStatus.Uncertain);
            }
            return(VerificationStatus.Uncertain);
        }
 private double angleDifference(ReflectionedLine firstLine, ReflectionedLine secondLine)
 {
     return(Math.Abs(firstLine.Angle - secondLine.Angle));
 }
        public VerificationStatus Classiffy(ElastoBlob targetObject, Segment fibroLine)
        {
            TargetObject = targetObject;
            FibroLine    = fibroLine;
            if (CheckForNull())
            {
                return(VerificationStatus.NotCalculated);
            }
            int area = TargetObject.Blob.Area;
            ReflectionedLine leftLine     = TargetObject.LeftApproximation;
            ReflectionedLine rightLine    = TargetObject.RightApproximation;
            double           rSquareLeft  = TargetObject.RSquareLeft;
            double           rSquareRight = TargetObject.RSquareRight;
            double           aLeft        = TargetObject.RelativeEstimationLeft;
            double           aRight       = TargetObject.RelativeEstimationRight;

            if (area < 4000)
            {
                return(VerificationStatus.Uncertain);
            }

            if (area < 6000)
            {
                if ((IsStrongAngleClose(leftLine, fibroLine.Equation)) &&
                    IsStrongAngleClose(leftLine, rightLine) &&
                    (IsGoodTilt(leftLine)) &&
                    IsGoodApproximation(leftLine, rSquareLeft, aLeft) &&
                    IsGoodApproximation(rightLine, rSquareRight, aRight))
                {
                    return(VerificationStatus.Correct);
                }
                return(VerificationStatus.Uncertain);
            }

            if (area < 12000)
            {
                if (!IsGoodApproximation(leftLine, rSquareLeft, aLeft) ||
                    !IsGoodApproximation(rightLine, rSquareRight, aRight))
                {
                    return(VerificationStatus.Uncertain);
                }
                if ((IsWeakAngleClose(leftLine, fibroLine.Equation)) &&
                    IsWeakAngleClose(leftLine, rightLine) &&
                    (IsGoodTilt(leftLine)))
                {
                    return(VerificationStatus.Correct);
                }
                return(VerificationStatus.Incorrect);
            }

            if (area < 21000)
            {
                if (IsGoodApproximation(leftLine, rSquareLeft, aLeft) &&
                    IsGoodApproximation(rightLine, rSquareRight, aRight))
                {
                    return(VerificationStatus.Incorrect);
                }
                return(VerificationStatus.Uncertain);
            }

            return(VerificationStatus.Uncertain);

            //Reserved code for classify
            if ((area < 8000) || (area > 6000))
            {
                if (IsGoodApproximation(leftLine, rSquareLeft, aLeft) &&
                    IsGoodApproximation(rightLine, rSquareRight, aRight))
                {
                    return(VerificationStatus.Correct);
                }

                return(VerificationStatus.Incorrect);
            }

            if ((area < 17500) || (area > 12000))
            {
                if (IsGoodApproximation(leftLine, rSquareLeft, aLeft) &&
                    IsGoodApproximation(rightLine, rSquareRight, aRight))
                {
                    return(VerificationStatus.Uncertain);
                }
                return(VerificationStatus.Incorrect);
            }
        }