rgb2gray by 5*5 vicinity marix

조회 수: 3 (최근 30일)
Ali Azizi
Ali Azizi 2021년 10월 26일
댓글: Ali Azizi 2021년 10월 26일
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

답변 (1개)

DGM
DGM 2021년 10월 26일
편집: DGM 2021년 10월 26일
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)
ans = 9.7586e-04
imshow(outpict)
  댓글 수: 1
Ali Azizi
Ali Azizi 2021년 10월 26일
How kind you are to
thanks you
it`s work
How kind you are to help me. help me.

댓글을 달려면 로그인하십시오.

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by