function O=myconv2(I,mask);
% Perform a convolution of image I with odd sized kernel mask
% O=myconv(I,mask)
% theorectically mask should contain the vertically and horizontally
% flipped filter coefficients

[sy,sx]=size(mask);

if rem(sy,2)==0 | rem(sx,2)==0,
    display('Error: the mask has an even size');
else
    I=double(I);
    [height,width]=size(I);
    O=zeros(height,width); % output image
    
    for j=round(sy/2):height-round(sy/2)+1,
        for i=round(sx/2):width-round(sx/2)+1,
            O(j,i)=sum(sum(I(j-round(sy/2)+1:j+round(sy/2)-1,i-round(sx/2)+1:i+round(sx/2)-1)...
                .*mask(:,:)));  % convolution operation
        end
    end
    
end            
