How to change a 3x3 median filter to 5x5?
7 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
DGM
2021-10-4
编辑:DGM
2021-10-4
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)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Multirate Signal Processing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!