How to change a 3x3 median filter to 5x5?

5 visualizaciones (últimos 30 días)
Matthew Worker
Matthew Worker el 4 de Oct. de 2021
Comentada: DGM el 27 de Abr. de 2023
My code works, but i need a 5x5 filter not 3x3. I tried to alter the code to get the 5x5 but keep getting an error
%% 2. Median Filter
%READ IMAGE
sad1=imread('sadimg.bmp');
figure,imshow(sad1)
title('IMAGE WITH NOISE');
%PAD THE MATRIX WITH ZEROS ON ALL SIDES
median_filt=zeros(size(sad1)+2);
re_sad1=zeros(size(sad1));
%COPY THE ORIGINAL IMAGE MATRIX TO THE PADDED MATRIX
for x=1:size(sad1,1)
for y=1:size(sad1,2)
median_filt(x+1,y+1)=sad1(x,y);
end
end
%LET THE WINDOW BE AN ARRAY
%STORE THE 3-by-3 NEIGHBOUR VALUES IN THE ARRAY
%SORT AND FIND THE MIDDLE ELEMENT
for i= 1:size(median_filt,1)-2
for j=1:size(median_filt,2)-2
window=zeros(9,1);
inc=1;
for x=1:3
for y=1:3
window(inc)=median_filt(i+x-1,j+y-1);
inc=inc+1;
end
end
med=sort(window);
%PLACE THE MEDIAN ELEMENT IN THE OUTPUT MATRIX
re_sad1(i,j)=med(5);
end
end
%CONVERT THE OUTPUT MATRIX TO 0-255 RANGE IMAGE TYPE
re_sad1=uint8(re_sad1);
title('IMAGE AFTER MEDIAN FILTERING');
figure,imshow(re_sad1)
this was my attempt but i get the error : "ndex exceeds the number of array elements (25)."
%% 2. Median Filter
%READ IMAGE
sad1=imread('sadimg.bmp');
%PAD THE MATRIX WITH ZEROS ON ALL SIDES
median_filt=zeros(size(sad1)+2);
re_sad1=zeros(size(sad1));
%COPY THE ORIGINAL IMAGE MATRIX TO THE PADDED MATRIX
for x=1:size(sad1,1)
for y=1:size(sad1,2)
median_filt(x+1,y+1)=sad1(x,y);
end
end
%LET THE WINDOW BE AN ARRAY
%STORE THE 5-by-5 NEIGHBOUR VALUES IN THE ARRAY
%SORT AND FIND THE MIDDLE ELEMENT
for i= 1:size(median_filt,1)-3
for j=1:size(median_filt,2)-3
window=zeros(25,1);
inc=1;
for x=1:5
for y=1:5
window(inc)=median_filt(i+x-1,j+y-1);
inc=inc+1;
end
end
med=sort(window);
%PLACE THE MEDIAN ELEMENT IN THE OUTPUT MATRIX
re_sad1(i,j)=med(175);
end
end
%CONVERT THE OUTPUT MATRIX TO 0-255 RANGE IMAGE TYPE
re_sad1=uint8(re_sad1);
figure
subplot(2,1,1);
imshow(sad1)
title('IMAGE WITH NOISE');
subplot(2,1,2);
imshow(re_sad1)
title('IMAGE AFTER MEDIAN FILTERING');
  3 comentarios
Rahmi
Rahmi el 26 de Abr. de 2023
3x3 that's mean 3 layer or different?
DGM
DGM el 27 de Abr. de 2023
It's a local median filter that works in a 3x3 window on a 2D array.

Iniciar sesión para comentar.

Respuestas (1)

DGM
DGM el 4 de Oct. de 2021
Editada: DGM el 4 de Oct. de 2021
Start by simplifying:
% 2. Median Filter
%READ IMAGE
inpict = imread('sadimg.bmp');
%imshow(inpict)
%title('IMAGE WITH NOISE');
% make things parametric instead of hard-coding everything
filtersize = [5 5]; % or whatever size
% PAD THE MATRIX WITH ZEROS ON ALL SIDES
padsize = floor(filtersize/2);
paddedimage = padarray(inpict,padsize,0,'both');
s0 = size(inpict);
outpict = zeros(s0,class(inpict));
os = filtersize-1;
for m = 1:s0(1)
for n = 1:s0(2)
%PLACE THE MEDIAN ELEMENT IN THE OUTPUT MATRIX
sample = paddedimage(m:(m+os(1)),n:(n+os(2)));
outpict(m,n) = median(sample(:)); % what's wrong with median()?
end
end
% test the output against existing tools
referenceimage = medfilt2(inpict,filtersize);
immse(outpict,referenceimage) % show that this filter behaves the same as medfilt2()
title('IMAGE AFTER MEDIAN FILTERING');
imshow(outpict)

Community Treasure Hunt

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

Start Hunting!

Translated by