MATLAB: I want to use 5*5 filter mask on the image in order to detect edges but the image is 3*3 how can i make the image 5*5? Please help me

edge detectionsobel operator

I want to use 5*5 filter mask on my image in order to detect edges but my image is 3*3 how can i make my image 5*5? Please help me, my code is
%Input Image
A=imread('cameraman.jpg');
imshow(A);
A=rgb2gray(A);
%Preallocate the matrices with zeros
I=zeros(size(A));
%Filter Masks
F1=[5 8 10 8 5;4 10 20 10 4;0 0 0 0 0;-4 -10 -20 -10 -4;-5 -8 -10 -8 -5];
F2=[-5 -4 0 4 5;-8 -10 0 10 8;-10 -20 0 20 10;-8 -10 0 10 8;-5 -4 0 4 5];
A=double(A);
for i=1:size(A,1)
for j=1:size(A,2)
%Gradient operations
Gx=sum(sum(F1.*A(i:i+2,j:j+2)));
Gy=sum(sum(F2.*A(i:i+2,j:j+2)));
%Magnitude of vector
I(i+1,j+1)=sqrt(Gx.^2+Gy.^2);
end
end
I=uint8(I);
figure,imshow(I);title('Filtered Image');
%Define a threshold value
Thresh=100;
B=max(I,Thresh);
B(B==round(Thresh))=0;
B=uint8(B);
%B=im2bw(B);
figure,imshow(~B);title('Edge detected Image');
|edgePixels = nnz(B)
error shows- error using .* "matrix dimension must agree"|

Best Answer

Why are you extracting a 3x3 subset of your image in the first place if you have a 5x5 filter?
I would assume you need
Gx=sum(sum(F1.*A(i-2:i+2,j-2:j+2)));
Gy=sum(sum(F2.*A(i-2:i+2,j-2:j+2)));
and your loops will have to run from 3 up to size-2 instead unless you want to do special handling for the edge pixels where you don't have a full neighbourhood.
you should be able to use
doc nlfilter
or similar functions for this though too, although last time I used it it did annoyingly force a progress bar on you.