138 views (last 30 days)

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));

Royi Avital
on 23 Apr 2014

Yes, I know. But Matt's method creates a "Shift" in the image which requires padding which is again part of the IP toolbox.

I wonder if there's a code which can be faster than imdilate and require no toolbox.

Matt J
on 28 Apr 2014

Padding doesn't require the IP toolbox, e.g.,

p=2; %padding

Apad=zeros(size(A)+2*p,class(A));

Apad(p+1:end-p,p+1:end-p)=A;

Image Analyst
on 28 Apr 2014

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:

A=rand(100);

window=3;

[m,n]=size(A);

B=A;

for ii=1:m+1-window

B(ii,:)=max(A(ii:ii+window-1,:),[],1);

end

for ii=1:n+1-window

B(:,ii)=max(B(:,ii:ii+window-1),[],2);

end

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?

Thaks.

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
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

N=1;

Imin=ordfilt2(I, 1, true(N));

N=10;

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');

end

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));

end

Opportunities for recent engineering grads.

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

Start Hunting!
## 5 Comments

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/49368-sliding-window-minimum-and-maximum-filter#comment_102056

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/49368-sliding-window-minimum-and-maximum-filter#comment_102056

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/49368-sliding-window-minimum-and-maximum-filter#comment_102059

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/49368-sliding-window-minimum-and-maximum-filter#comment_102059

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/49368-sliding-window-minimum-and-maximum-filter#comment_114677

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/49368-sliding-window-minimum-and-maximum-filter#comment_114677

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/49368-sliding-window-minimum-and-maximum-filter#comment_114682

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/49368-sliding-window-minimum-and-maximum-filter#comment_114682

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/49368-sliding-window-minimum-and-maximum-filter#comment_114698

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/49368-sliding-window-minimum-and-maximum-filter#comment_114698

Sign in to comment.