Max filter for 5 x 5 and 9 x 9 pixel size

%READ AN IMAGE
A = imread('sample2.jpg');
A = rgb2gray(A(1:300,1:300,:));
figure,imshow(A),title('ORIGINAL IMAGE');
%PREALLOCATE THE OUTPUT MATRIX
B=zeros(size(A));
%PAD THE MATRIX A WITH ZEROS
modifyA=padarray(A,[1 1]);
x=[1:3]';
y=[1:3]';
for i= 1:size(modifyA,1)-2
for j=1:size(modifyA,2)-2
%VECTORIZED METHOD
window=reshape(modifyA(i+x-1,j+y-1),[],1);
%FIND THE MAXIMUM VALUE IN THE SELECTED WINDOW
B(i,j)=max(window);
end
end
%CONVERT THE OUTPUT MATRIX TO 0-255 RANGE IMAGE TYPE
B=uint8(B);
figure,imshow(B),title('IMAGE AFTER MAX FILTERING');
I want to do a 5x5 and 9x9 size pixel fro this coding. Can someone help me ?
Thank you

 Accepted Answer

For the N x N, use
x = (1:N).';
y = x;

3 Comments

Jensen
Jensen on 17 Mar 2014
Edited: Jensen on 17 Mar 2014
Is it that i only need to change
x=[1:3]'; y=[1:3]';
into x = (1:N).'; y = x; ??
When i change it. The coding got error :
|??? Index exceeds matrix dimensions.
Error in ==> Untitled at 20
window=reshape(modifyA(i+x-1,j+y-1),[],1);|
The code has size(modifyA,1)-2 in the for loop. Change that -2 to -N+1 where N is a variable holding the desired size. Make the same kind of change on the following line.

Sign in to comment.

More Answers (1)

In your case use function imdilate from Image Processing Toolbox,
for 5x5:
B = imdilate(A,ones(5));
for 9x9:
B = imdilate(A,ones(9));

3 Comments

Jensen
Jensen on 17 Mar 2014
Edited: Jensen on 17 Mar 2014
I am kinda new in this Matlab coding. Can u teach how to use imdilate from Image Prcessing Toolbox ? Is it i just at it onto the code ?
You must be install Image Processing Toolbox (if it is not installed) and run following:
A = imread('sample2.jpg');
A = rgb2gray(A(1:300,1:300,:));
figure,imshow(A),title('ORIGINAL IMAGE');
B = imdilate(A,ones(5));
B=uint8(B);
figure,imshow(B),title('IMAGE AFTER MAX FILTERING');
It looks like it may be a homework assignment since I just answered this yesterday for someone else: http://www.mathworks.com/matlabcentral/answers/121748#answer_128654

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!