private int[,] ComputeDistanceTransform(PointsSequence curve) { // create points transformed to the [0 .. MAX_RESOLUTION] range. var points = curve.Points.ToArray(); foreach (var i in Enumerable.Range(0, points.Length)) { points[i].X = 0.5 * Constants.DISTANCE_TRANSFORM_RESOLUTION * (points[i].X + 1); points[i].Y = 0.5 * Constants.DISTANCE_TRANSFORM_RESOLUTION * (points[i].Y + 1); } // close the curve, if needed if (curve is Polygon) { points = points.Concat(Enumerable.Repeat(points.First(), 1)).ToArray(); } // compute distance transform and return it double[,] transform = new double[Constants.DISTANCE_TRANSFORM_RESOLUTION, Constants.DISTANCE_TRANSFORM_RESOLUTION]; ChamferDistanceTransform.Compute(points, transform); int[,] result = new int[Constants.DISTANCE_TRANSFORM_RESOLUTION, Constants.DISTANCE_TRANSFORM_RESOLUTION]; for (int x = 0; x < Constants.DISTANCE_TRANSFORM_RESOLUTION; ++x) { for (int y = 0; y < Constants.DISTANCE_TRANSFORM_RESOLUTION; ++y) { result[x, y] = (int)Math.Round(16 * transform[x, y]); Debug.Assert(result[x, y] >= 0); } } return(result); }
private static double[,] GetDistanceTransform(Point[] l1, Point[] l2) { var transform1 = new double[IMAGE_SPACE_WIDTH, IMAGE_SPACE_HEIGHT]; var transform2 = new double[IMAGE_SPACE_WIDTH, IMAGE_SPACE_HEIGHT]; ChamferDistanceTransform.Compute(l1, transform1); ChamferDistanceTransform.Compute(l2, transform2); var totalTransform = Min(transform1, transform2); return(totalTransform); }