function model = cropneg(model)

% generate background images and depths
grid = model.samgrid;

disp('Cropping background ... ...');
% create save path
bgdir = model.datadir.bgdir;

% crop background images and depths
subbg = bodir(bgdir);
num = 0;
aa = 0;
negsam = 0;
for i = 1:length(subbg)
    subbgdir{i} = [bgdir subbg(i).name];
    depthname = dir([subbgdir{i} '/*_clean_depth.png']);
    negsam = negsam + size(depthname,1);
end
for i = 1:length(subbg)
    subbgdir{i} = [bgdir subbg(i).name];
    depthname = dir([subbgdir{i} '/*_clean_depth.png']);
    for j = 1:size(depthname,1)
        aa = aa + 1;
        
        % generate image paths
        imdir{1,aa} = [subbgdir{i} '/' depthname(j).name(1:end-16) '.png'];
        % generate depth paths
        depthdir{1,aa} = [subbgdir{i} '/' depthname(j).name];
    
        % generate negative images
        im = imread(imdir{aa});
        depth = imread(depthdir{aa});
        imh = size(im,1);
        imw = size(im,2);
        it = 0;
        for s = 1:grid:(imh - model.height)
            for t = 1:grid:(imw - model.width)
                num = num + 1;
                it = it + 1;
                imsub = im(s+(0:model.height-1),t+(0:model.width-1),:);
                depthsub = depth(s+(0:model.height-1),t+(0:model.width-1),:);
                negfea = cropfea(imsub, depthsub, 1, model);
                if i == 1 & j ==1 & s==1 & t==1
                   model.negfea = sparse(zeros(length(negfea(:)), negsam*length(1:grid:(imh-model.height))*length(1:grid:(imw-model.width))));
                end
                model.negfea(:,num) = sparse(negfea(:));
 
               if mod(num,300) == 0
                   disp(['Current iteration is ' num2str(num)]);
                end

            end
        end
    end
end


