function O=myconv(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);
    
    for j=1:height,
        for i=1:width,
            try
                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
            catch
                
                O(j,i)=0; % zero padding at borders
            end
        end
    end
    
end            
