I am trying to filter an image with out using imfilter. I should get the same results as imfilter but I keep getting diffrent results. Can someone tell me where I went wrong?
orignal=imread('obj6__17.png');
filter=1/9*[-1 -1 -1 ; -1 17 -1 ; -1 -1 -1];
s=size(orignal);
r=zeros(s(1));
temp = zeros(3);
for i= 2: s(1)-1
for j = 2: s(2)-1
for n= 1: 3
for m= 1:3
temp(n,m)=orignal(i+2-n,j+2-m)*filter(n,m);
end
end
r(i,j)=sum(single(sum(temp)));
end
end
The size of
rshould be the same as the original I think. And I don’t understand why you convert to single precision usingsingle. Anyway, I think you want to do the following:The result is as follows:
And with
imfilter, it is:As you see, the results are the same except the ones on the borders. There are a few strategies to compute the ones on the borders as mirroring the image to the out of the borders, keeping them the same, etc. Please read the documentation of
imfilterand choose one strategy.Note that I didn’t flipped
filterhere since the filter is symmetric in both directions. And I recommend you to avoid loops! There are nested loops of depth four in your code!Lastly, you can use 2-D convolution to do the same as
imfilter: