MinMax Neighborhood Filters

A minmax neighborhood filter finds the minimum (maximum) pixel in 3 x 3 neighborhoods of each pixel. Repeated calls to the filter extend the size of the neighborhood.

Matlab code for this study maybe downloaded from minmax.zip.

Simple Example

As a simple example, consider the case of isolated pixels on a zero background:

» a = zeros(9);
» idx = [1 5 9];
» a(idx,idx) = 1

a =

     1     0     0     0     1     0     0     0     1
     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0
     1     0     0     0     1     0     0     0     1
     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0
     1     0     0     0     1     0     0     0     1

» maxfilt(a)


ans =

     1     1     0     1     1     1     0     1     1
     1     1     0     1     1     1     0     1     1
     0     0     0     0     0     0     0     0     0
     1     1     0     1     1     1     0     1     1
     1     1     0     1     1     1     0     1     1
     1     1     0     1     1     1     0     1     1
     0     0     0     0     0     0     0     0     0
     1     1     0     1     1     1     0     1     1
     1     1     0     1     1     1     0     1     1

» -minfilt(-a)

ans =

     1     1     0     1     1     1     0     1     1
     1     1     0     1     1     1     0     1     1
     0     0     0     0     0     0     0     0     0
     1     1     0     1     1     1     0     1     1
     1     1     0     1     1     1     0     1     1
     1     1     0     1     1     1     0     1     1
     0     0     0     0     0     0     0     0     0
     1     1     0     1     1     1     0     1     1
     1     1     0     1     1     1     0     1     1

Grayscale Image Example

» gray = imread('cameraman.tif');
» gray = im2double(gray);
» imwrite(gray,'cameraman.jpg');


Original image

» out1 = (maxfilt(gray)+minfilt(gray))/2;
» imwrite(out1,'cameraman_out1.jpg');


filtered image (rep=1)

» diff = imdiff(out1,gray);
maximum difference: 0.433333
» imwrite(diff,'cameraman_diff1.jpg');


difference image (rep=1)

» out2 = (maxfilt(gray,2)+minfilt(gray,2))/2;
» imwrite(out2,'cameraman_out2.jpg');


filtered image (rep=2)

» diff = imdiff(out2,gray);
maximum difference: 0.462745
» imwrite(diff,'cameraman_diff2.jpg');


difference image (rep=2)

The effect here is very similar to the Laplacian filter:

» lap = fspecial('laplacian');
» out = filter2(lap,gray);
» diff = imdiff(out);
maximum difference: 1.79346
» imwrite(diff,'cameraman_laplacian.jpg');


laplacian image

Color Image Example

» rgb = imread('flowers.tif');
» rgb = im2double(rgb);
» imwrite(rgb,'flowers.jpg');


original image

» out2 = (maxfilt(rgb,2)+minfilt(rgb,2))/2;
» imwrite(out2,'flowers_out2.jpg');


filtered image (rep=2)

» diff = imdiff(out2,rgb);
maximum difference: 0.447059
» imwrite(diff,'flowers_diff2.jpg');


difference image (rep=2)


Maintained by John Loomis, last updated 17 March 2000