function im = displayImages(images,ncol,bx,by)
% display a set of images of the same size (bx,by)
n = length(images);
nrow = ceil(n/ncol);
widthMargin = 5;
width = by*ncol + (ncol-1)*widthMargin;
heightMargin = 5;
height = bx*nrow + (nrow-1)*heightMargin;
im = 255-zeros(height,width,3,'single');
for i=1:n
    if isempty(images{i})
        continue
    end
    row = ceil(i/ncol);
    col = i-(row-1)*ncol;
    startx = (row-1)*(bx+heightMargin);
    starty = (col-1)*(by+widthMargin);
    towrite = single(imresize(images{i},[bx,by]));
    towrite = uint8(255 * (towrite-min(towrite(:)))/(max(towrite(:))-min(towrite(:))));
    if size(towrite,3) == 3
        im((startx+1):(startx+bx),(starty+1):(starty+by),:) = towrite(:,:,:);
    else
        im((startx+1):(startx+bx),(starty+1):(starty+by),:) = repmat(towrite,[1 1 3]);
    end
end
im = uint8(im);
