public static MatrixData CalculateDistanceField(this MatrixData input, int searchDistance) { MatrixData output = new MatrixData(input.NumberOfRows, input.NumberOfColumns); for (int r = 0; r < input.NumberOfRows; r++) { for (int c = 0; c < input.NumberOfColumns; c++) { //skip black pixels if (input[r, c] == 0) { continue; } int steps = 0; int fxMin = Math.Max(r - searchDistance, 0); int fxMax = Math.Min(r + searchDistance, input.NumberOfRows); int fyMin = Math.Max(c - searchDistance, 0); int fyMax = Math.Min(c + searchDistance, input.NumberOfColumns); for (int fx = fxMin; fx < fxMax - 1; ++fx) { for (int fy = fyMin; fy < fyMax - 1; ++fy) { if (input[fx, fy] == 255) { steps += ((fx - fxMin) + (fy - fyMin)); //int tempStep = ((fx - fxMin) + (fy - fyMin)); //if(tempStep > steps) steps = tempStep; } } } output[r, c] = steps; } } return(output.HistEQ()); }