function [ out_image ] = myConv( image_matrix, kernel_matrix )
% This funciton
% realizes the general convolution, 
% the parameters of this function should be as follows: 
%   input1: image matrix 
%   input2: convolution kernel matrix (h), 
%   output: convolved image; 
% it should work with grayscale images (check if the input image is grayscale), 
% the boundary condition can be any of the mentioned (easiest: zero-padded, 
%       you can also use the built-in padarray command).

if (size(image_matrix,3) ~= 1)
    error 'This picture is not grayscale!'
end

kernel_size = size(kernel_matrix,1);
padding = (kernel_size-1)/2;
im_matrix = padarray(image_matrix, padding);
kernel_matrix = rot90(rot90(kernel_matrix));
out_image = zeros(size(image_matrix,1), size(image_matrix,2));

for i = 1+padding:size(image_matrix,1)-2*padding
    for j = 1+padding:size(image_matrix,2)-2*padding
        matrix1 = im_matrix(i:i+kernel_size-1, j:j+kernel_size-1);
        out_image(i-padding, j-padding) = sum(sum(matrix1 .* kernel_matrix));
    end
end

end

