rgb2gray by 5*5 vicinity marix
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
hello world
I apologize in advance for the weakness of my speech
I am writing a code for the university but I got stuck and I ask you dear ones for help.
In this code we want to blur the image with a 5 * 5 matrix using rgb2gray.
A series of codes have been written that are wrong with my figure.
Please correct it for me.
Thankful
[filename, pathname]=uigetfile('*.*','Select the image file');
Address=strcat(pathname,filename);
a=imread(Addres);
a=double(rgb2gray(a));
a2=zeos(size(a));
w=[1,1,1,1,1;1,1,1,1,1;1,1,1,1,1];
for x=3:size(a,1)-2
for y=3:size(a,2)-2
a2(x,y)=(w(1,1)*a(x-2,y-2)+w(1,2)*a(x-2,y-1)+w(1,3)*a(x-1,y+1)
end
end
0 commentaires
Réponses (1)
DGM
le 26 Oct 2021
Modifié(e) : DGM
le 26 Oct 2021
Errors aside, this doesn't even look complete.
[filename, pathname]=uigetfile('*.*','Select the image file');
Address=strcat(pathname,filename);
a=imread(Addres);
a=double(rgb2gray(a))
a2=zeros(size(a)); % typo
%w=[1,1,1,1,1;1,1,1,1,1;1,1,1,1,1]; % this is not 5x5
w = ones(5)/25; % this is a 5x5 flat filter
for x = 3:size(a,1)-2
for y = 3:size(a,2)-2
% 1: mismatched parentheses
% 2: this only addresses 3 of the 25 pixels in the window
% 3: this will leave a 3px black border around the image
a2(x,y) = w(1,1)*a(x-2,y-2) + w(1,2)*a(x-2,y-1) + w(1,3)*a(x-1,y+1)
end
end
In order to avoid #3, you need to pad the edges or conditionally crop the filter. Consider the example:
% example image
inpict = im2double(imread('cameraman.tif'));
filtersize = [5 5];
% pad the image
padsize = floor(filtersize/2);
paddedimage = padarray(inpict,padsize,'replicate','both');
% make flat filter
fk = ones(filtersize)/prod(filtersize);
s0 = size(inpict);
outpict = zeros(s0,class(inpict));
os = filtersize-1;
for m = 1:s0(1)
for n = 1:s0(2)
sample = paddedimage(m:(m+os(1)),n:(n+os(2)));
outpict(m,n) = sum(sample.*fk,'all');
end
end
% show that result is the same as using imfilter()
referenceimage = imfilter(inpict,fk);
immse(outpict,referenceimage)
imshow(outpict)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!