close all;
clear clc;

I = imread("AlfredoBorba_TuscanLandscape.jpg");
I = imresize(I, 0.2);
I = rgb2gray(I);
I = im2double(I);

%creating the kernel
K = fspecial('prewitt');
K = K';

%padding the image
k1 = (size(K,1)-1) / 2;
k2 = (size(K,2)-1) / 2;
P = padarray(I, [k1, k2], 'both');

%rotating the kernel
K = rot90(K, 2);

%convolution
conv_I = zeros(size(I));
for i1 = 1+k1:size(P,1)-k1
    for i2 = 1+k2:size(P,2)-k2
       IP = P(i1-k1:i1+k1, i2-k2:i2+k2);
       PR = IP .* K;
       S = sum(PR(:));
       conv_I(i1-k1,i2-k2) = S;
    end
end

%creating the new kernel
new_K = fspecial('prewitt');
new_K = new_K';

%using the built-in convolution
built_in = conv2(I, new_K, 'same');

figure();

subplot(1, 3, 1);
imshow(I);
title('original image');

subplot(1, 3, 2);
imagesc(conv_I);
title('with our convolution');
colormap gray;
axis equal;
axis off;
colorbar;

subplot(1, 3, 3);
imagesc(built_in);
title('with built-in conv2');
colormap gray;
axis equal;
axis off;
colorbar;





