MATLAB Answers

Sliding window minimum and maximum filter

151 views (last 30 days)
Lab Rat
Lab Rat on 28 Sep 2012
Answered: Dan on 19 Jun 2017
I'm trying to apply a sliding window minimum and maximum filter to an image of a certain window size. Actually, I'm trying to find the optimum window size for it. But I really haven't gotten the hang of it. I presume that I should be using blockproc to implement the sliding window, but not really sure how to find the maximum and minimum filter. As to the implementation itself, should I use loops to slide the window across the entire area of the image ?
doudou on 1 Dec 2012
@image Analyst thanks for your answer

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 29 Sep 2012
If you have the Image Processing Toolbox, you're in luck!. The sliding max filter is called imdilate() and the sliding min filter is imerode(). These are called "morphological operations." No loops needed:
localMinImage = imerode(grayImage, true(3));
localMaxImage = imdilate(grayImage, true(3));
Image Analyst
Image Analyst on 28 Apr 2014
What do you mean by shift? As long as the window size is odd, e.g. 3 or 5, there will be no shift. If the window size is even, e.g. 2 or 4, then there will be a half pixel shift.

Sign in to comment.

More Answers (4)

Matt J
Matt J on 29 Sep 2012
Edited: per isakson on 25 Oct 2014
I would just use a for-loop to do 2 separable passes. BLOCKPROC can't take advantage of the separable nature of the max/min filter:
for ii=1:m+1-window
for ii=1:n+1-window
  1 Comment
Royi Avital
Royi Avital on 23 Apr 2014
This is nice and fast. Yet it shift the matrix to the right.
How would replicate the results of `imerode` or `imdilate` with Image Processing Toolbox (no `padaaray`) most efficiently?

Sign in to comment.

Royi Avital
Royi Avital on 23 Apr 2014
I would use:
localMaxImage = colfilt(inputImage, [winLength winLength], 'sliding', @max);
localMinImage = colfilt(inputImage, [winLength winLength], 'sliding', @min);
Though it is still requires patience. I wonder what would be the fastest way to do so without Image Processing Toolbox.

tilak tenneti
tilak tenneti on 24 Oct 2014
Edited: per isakson on 25 Oct 2014
i want to apply a sliding window minimum filter on input image I and obtain Imin and also apply sliding window maximum filter on Imin to obtain Imax : the following is code
Imin=ordfilt2(I, 1, true(N));
Imax = ordfilt2(Imin, N*N, true(N));
here i assume N as window size... but i am confused as how should i take N for minimum filter and again N for maximum filter?

Dan on 19 Jun 2017
function [minVals,maxVals] = minmaxfilt1(vector,nhoodSz)
vector = vector(:);
if nhoodSz < 3 || ~floor(mod(nhoodSz,2))
error('nhoodSz must be odd scalar');
minVals = min(conv2(vector,eye(nhoodSz)),[],2);
maxVals = max(conv2(vector,eye(nhoodSz)),[],2);
minVals = minVals(((nhoodSz-1)/2)+1: end- ((nhoodSz-1)/2));
maxVals = maxVals(((nhoodSz-1)/2)+1: end - ((nhoodSz-1)/2));


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!