I have an image matrix I want a way to detect the noise type, and then find a filter to remove that noise, using MATLAB.
My problem: I draw a histogram of the image, which looked the same as a histogram for salt and paper noise (my image has only three pixels of noise). I tried the median filter to remove noise, but it changed the image more than just removing noise.
1 1 1 1 1 1 1 1 1
1 100 100 100 100 100 1 1 1
1 100 100 100 100 100 100 1 1
1 100 100 1 1 100 100 1 100
1 100 100 1 1 100 100 1 1
1 100 100 100 100 100 100 1 1
1 100 100 100 100 100 100 1 1
1 100 100 1 1 1 1 1 1
1 100 100 1 80 1 1 1 90
A median filter will cut off the corners of your image like that. Given that your image is more or less binary, a simple way of removing the noise in this case is to remove isolated pixels: pixels surrounded by values much lower than itself.
This can be easily accomplished in MATLAB with the Image Processing Toolbox using the morphological opening (imopen
):
img = [1 1 1 1 1 1 1 1 1
1 100 100 100 100 100 1 1 1
1 100 100 100 100 100 100 1 1
1 100 100 1 1 100 100 1 100
1 100 100 1 1 100 100 1 1
1 100 100 100 100 100 100 1 1
1 100 100 100 100 100 100 1 1
1 100 100 1 1 1 1 1 1
1 100 100 1 80 1 1 1 90];
img = padarray(img,[1,1]); % proper boundary conditions needed
img = max(imopen(img,[1,1]),imopen(img,[1;1]));
img = img(2:end-1,2:end-1); % remove padding again
We're using two openings: one with a SE [1,1]
, and one with an SE [1;1]
. Either one of them might remove 1-pixel thick lines, but no lines will be removed by both. Thus, we take the maximum of the two results: if both filters remove the pixel, it will stay removed, but if only one removes it, we want to keep this pixel (it belongs to a line).
There are other ways of identifying isolated pixels, but this method is quite simple to implement based on an existing function in the toolbox.