public static PNM ApplyDistanceTransform(this PNM image) { PNM binaryImage = image.ApplyCannyDetector(); int pixelCount = 0; double[][] distanceMap = ToInitialDistanceMap(binaryImage, ref pixelCount); CalculateDistances(distanceMap, pixelCount); PNM distancedImage = new PNM(image.Width, image.Height); for (int i = 0; i < image.Width * image.Height; i++) { byte distance = Filter.Coerce(distanceMap[i % image.Width][i / image.Width]); distancedImage.SetPixel(i, distance, distance, distance); } return(distancedImage); }