function img = myprewittfilter(f)
% MYFILTER performs spatial correlation
% I = MYFILTER(f,w) produces an image that has undergone correlation
% f is the original image
% w is the filter (assumed to be 3x3)
% the original image is padded with 0's
% check that the filter,w is 3x3
w=[-1,0,1;-1,0,1;-1,0,1];
w1=imrotate(w,180);
w2=imrotate(w,270);
%[m,n] = size(w);
%if m~=3 || n~=3
% error ('Filter must be 3x3')
%end
% get size of the original image, f
[x,y] = size(f);
%x-direction filtering
% create padded f (namely gx)
% filled with zeros
gx = zeros (x+2, y+2);
% store f withing gx
for i = 1:x
for j = 1:y
gx(i+1, j+1) = f(i,j);
end
end
% traverse the array and apply the filter
for i = 1:x
for j = 1:y
img1(i,j) = gx(i,j)*w1(1,1) + gx(i+1,j)*w1(2,1) + gx(i+2,j)*w1(3,1)...
+ gx(i,j+1)*w1(1,2) + gx(i+1,j+1)*w1(2,2) + gx(i+2,j+1)*w1(3,2)...
+ gx(i,j+2)*w1(1,3) + gx(i+1,j+2)*w1(2,3) + gx(i+2,j+2)*w1(3,3);
end
end
%y-direction filtering
%create padded f (namely gy)
gy = zeros (x+2, y+2);
% store f withing gy
for i = 1:x
for j = 1:y
gy(i+1, j+1) = f(i,j);
end
end
% traverse the array and apply the filter
for i = 1:x
for j = 1:y
img2(i,j) = gy(i,j)*w2(1,1) + gy(i+1,j)*w2(2,1) + gy(i+2,j)*w2(3,1)...
+ gy(i,j+1)*w2(1,2) + gy(i+1,j+1)*w2(2,2) + gy(i+2,j+1)*w2(3,2)...
+ gy(i,j+2)*w2(1,3) + gy(i+1,j+2)*w2(2,3) + gy(i+2,j+2)*w2(3,3);
end
end
%combining the x- and y- direction convolution results
img=(abs(img1)+abs(img2));
%img=abs(img);
%img=abs(img);
% convert to uint -- expected range is [0,1] when the image is displayed
img = uint8(img);
end
评论0