function [img] = generate_im(im, imsize, nsite, template)

img = im;

for i=1:nsite,
  row = floor(rand*(imsize-size(template,1)))+1;
  col = floor(rand*(imsize-size(template,2)))+1;
  
  patch = img(row:(row+ size(template,1)-1), col:(col+size(template,2)-1));
  if sum(sum(patch < 120)) < 0.5,
    img(row:(row+ size(template,1)-1), col:(col+size(template,2)-1)) = template;
  end
end
image(img);
msg = sprintf('Size (%d,%d) with %d templates.', size(img,1), size(img,2), nsite);
title(msg);
axis('off');
axis('image');
return;

