Sliding window minimum and maximum filter in MATLAB
ordfilt2 will do this for you: 

N=5;
filtered_img = ordfilt2(img, 1, true(N));


for minimum and

filtered_img = ordfilt2(img, N*N, true(N));


for the maximum.  You can also use imdilate and imerode to perform maximum and minimum filters.