function check = image_checkerboard(I, J, n_patches)
    % Create a checkerboard merge of two images
    %   I, J        input images (required), must be of same size
    %   n_patches   number of patches along smaller of the dimensions of I
    %               (default=16)
    %
    
    % Author: Jan Hering, jan.hering@fel.cvut.cz

    if nargin < 2
        error('Two images must be set');
    end
    
    assert(isequal(size(I), size(J)), 'Checkerboard creator', 'Images differ in size');

    if nargin < 3
        n_patches = 16;
    end
    
    patch_size = round(size(I)/n_patches);
    patch_size = min(patch_size(1:2));
    check = zeros(size(I), 'uint8');
    
    for i=1:size(I, 1)
        for j=1:size(I, 2)
            if rem(floor(i / patch_size), 2) == rem(floor(j / patch_size), 2)
                check(i, j, :) = J(i, j, :);
            else
                check(i, j, :) = I(i, j, :);
            end
        end
    end

end