function saMat = SSDB_LocalSegmentCount(X,options);
%counts the number of unique values in a local region

d.yxPool = [15 45];
d.OrigSize = [128 128];
if(nargin < 2),options = [];,end
options = ResolveMissingOptions(options,d);
nIm = size(X,2);
saMat.SegFeature = zeros(size(X));
for i = 1:nIm;
  fprintf('SSDB_LocalSegmentCount image %d of %d\r',i,nIm);
  stim = SplitBrokenSegs(reshape(X(:,i),[128 128]));
  nUniqueIm = sum(UniqueWindowed(stim,options.yxPool),3);
  [saMat.SegFeature(:,i)] = nUniqueIm(:);
end
  fprintf('SSDB_LocalSegmentCount image %d of %d\n',i,nIm);

function MLI = UniqueWindowed(IdxImg,yx_wind);
%function MLI = UniqueWindowed(IdxImg,yx_wind);

IdxImg = padimage(IdxImg,[floor(yx_wind),ceil(yx_wind)],'symmetric');
u = unique(IdxImg);
MLI = zeros(size(IdxImg,1), size(IdxImg,2), length(u));

nWindsY = size(IdxImg,1) - yx_wind(1) + 1;
nWindsX = size(IdxImg,2) - yx_wind(2) + 1;
T = zeros(size(IdxImg,1), size(IdxImg,2), length(u));
for i = u'
  T(:,:,i) = (IdxImg == u(i));
  MLI(:,:,i) = maxfilter(T(:,:,i),[floor(yx_wind/2),ceil(yx_wind/2)]);
  %  MLI(:,:,i) = imdilate(T(:,:,i),ones(floor(yx_wind(1)),ceil(yx_wind(2))));
end
MLI = unpadimage(MLI,[floor(yx_wind),ceil(yx_wind)]);

