function D = addcroplabel(D, words)
%
% Adds the field object.crop (it also considers an occluded object as a
% cropped object).
% 
% Removes the words 'crop', 'occluded', etc. from the object name.

if nargin < 2
    words = {'crop', 'occluded', 'part'};
end

Nwords = length(words);

for i = 1:length(D)
    if isfield(D(i).annotation, 'object')
        Nobjects = length(D(i).annotation.object);
        for j = 1:Nobjects
            % set to 'not crop' by default
            D(i).annotation.object(j).crop = '0';

            % Check occluded field
            if isfield(D(i).annotation.object(j), 'occluded')
                if strcmp(D(i).annotation.object(j).occluded,'yes')
                    D(i).annotation.object(j).crop = '1';
                end
            end
            
            % Check if crop is inside the attributes list.
            if isfield(D(i).annotation.object, 'attributes')
                atr = lower(D(i).annotation.object(j).attributes);
                if ~isempty(strfind(atr, 'crop'))
                    c = strtrim(textscan(atr, '%s','Delimiter', ','));
                    c = sort(c{1});
                    c = setdiff(c,'crop');
                    atr = sprintf('%s,', c{:});
                    atr = atr(1:end-1);
                    D(i).annotation.object(j).attributes = lower(atr);
                    D(i).annotation.object(j).crop = '1';
                end
            end
            
            % Check if one of the words 'crop, part, occluded' is inside
            % the object name.
            w = lower(getwords(D(i).annotation.object(j).name));
            k = ismember(w, words);

            if sum(k)>0
                D(i).annotation.object(j).crop = '1';
                % remove words
                for n = 1:Nwords
                    D(i).annotation.object(j).name = strrep(D(i).annotation.object(j).name, words{n}, '');
                end
            end
        end
    end
end

% Make sure there are no new empty object names
D = LMvalidobjects(D);


