function [caS2Centroid,SourceLocations] = NaturalS2Centroids(yxSizes, AvailableIndicies,savename);
%function [caS2Centroid,SourceLocations] = NaturalS2Centroids(yxSizes, AvailableIndicies,savename);
%
%intent:  record a list of S2 centroids from random locations in the StreetScenesDatabase.
%         This constitutes a "natural" s2 representation
%
%pseudocode:  for each of the yxsizes, choose a random AvailableIndex and a random location
%  within the c1 image associated with that index.  Extract a template (centroid) and record

HelpTextInfo = GetHelpTextInfo;
nS2 = size(yxSizes,1);
seedrand;

tic
for i =1:nS2
   fprintf('%d of %d.  %d seconds elapsed\r',i,nS2,round(toc));
   p = randperm(length(AvailableIndicies));
   SSIdx = AvailableIndicies(p(1));
   SourceLocations(i).SSIdx = SSIdx;
   C1Img = GetC1Img(SSIdx,'all');
   s = yxSizes(i,:);
   SourceLocations(i).size = s;
   AvailPos = [(size(C1Img,1) - s(1) + 1),(size(C1Img,2) - s(2) + 1)];
   yxTopLeft = GetRandTopLeft(AvailPos);
   SourceLocations(i).yxTopLeft = yxTopLeft;
   caS2Centroid{i} = CropC1(C1Img, SourceLocations(i));
end
fprintf('\n');
if(nargin > 2);
  save(savename, 'caS2Centroid','SourceLocations','HelpTextInfo');
end

function yxTopLeft = GetRandTopLeft(AvailPos);
seedrand, r = rand(1,2);
yxTopLeft = floor(r .* AvailPos) + 1;

function S2c = CropC1(C1Img, sSource);
a = (1:sSource.size(1)) + sSource.yxTopLeft(1) - 1;
b = (1:sSource.size(2)) + sSource.yxTopLeft(2) - 1;
S2c = C1Img(a,b,:);
