function D = removerareobjects(D, countthreshold)
%
% Remove all objects that appear in less that 'countthreshold' images

disp('Number of objects before removing objects:')
sum(LMcountobject(D))

[objectnames, instancecounts] = LMobjectstats(D);

n = find(full(sum(instancecounts>0,2))<countthreshold);

%all = sprintf('-%s,', objectnames{n(:)});
%all = all(1:end-1);
%D = LMvalidobjects(D, all,  'exact');

for i = 1:length(n)
    j = find(instancecounts(n(i), :)>0);
    disp([i length(n)])
    D(j) = LMvalidobjects(D(j), ['-' objectnames{n(i)}],  'exact');
end
disp('Number of objects after removing objects:')
sum(LMcountobject(D))

%293539

%sum(LMcountobject(D))

