I made a minimum filter function that should work for PGM P2 images. The problem is that the output is wrong. Everything is described below.
Algorithm: http://www.roborealm.com/help/Min.php and https://www.youtube.com/watch?v=Y_QF0Xq8zGM
Debugging sample:
The beginning part of my image:
matrixSize = 3
offset = 1
First loop iteration:
j = 1, i = 1
neighboursNumbers = Count = 9
neighboursNumbers
values: (note that this is before sorting)
Second loop iteration:
j = 1, i = 2
neighboursNumbers = Count = 9
neighboursNumbers
values: (again before sorting)
Code:
// Properties
public string Format { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public int MaxGrayLevel { get; set; }
public int[] Pixels { get; set; }
// Minimum Filter Code
int matrixSize = 3;
int offset = (matrixSize - 1) / 2;
for (int j = offset; j < image.Height - offset; j++)
{
for (int i = offset; i < image.Width - offset; i++)
{
List<int> neighboursNumbers = (from x in Enumerable.Range(i - offset, matrixSize)
from y in Enumerable.Range(j - offset, matrixSize)
where (x >= 0) && (x < image.Width) && (y >= 0) && (y < image.Height)
select image.Pixels[y * Width + x]).ToList();
neighboursNumbers.Sort();
int minIndex = neighboursNumbers[0];
image.Pixels[j * image.Width + i] = minIndex;
}
}
Result:
Expected (this result is using radius 7.0 in ImageJ):