public ReflectionedLine Approximate(List <IntPoint> sourcePoints, int sourcePointCount, out double rSquares, out double relativeEstimation) { _sourcePoints = sourcePoints; _sourcePointsCount = sourcePointCount; LinearLeastSquares approximationOfRansac = new LinearLeastSquares(GetRansacPoints()); rSquares = approximationOfRansac.RSquares; relativeEstimation = approximationOfRansac.RelativeEstimation; return(approximationOfRansac.Line); }
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); }