function [imcrop, depthcrop, bbox] = rgbdhardneg(im, depth, model)

% extract image and depth filters
b = model.classifier.w(end);
w = model.classifier.w(1:end-1);
filters{1} = reshape(w, model.fheight, model.fwidth, model.dim);

% extract pyramid features
if model.depthtag == 1
   [indzeros(:,1), indzeros(:,2)] = find(depth == 0);
   depth = rmedfilter(depth,indzeros);
else
   depth = [];
end
pfea = pyramidfea(im, depth, model);

% compute responses of hog filter over pyramid hog features
for i = 1:length(pfea.fea)
    if size(pfea.fea{i},1) > size(filters{1},1) & size(pfea.fea{i},2) > size(filters{1},2)
       score = fconv(pfea.fea{i}, filters, 1, length(filters));
       fscore{i} = score{1} + b;
    else
       fscore{i} = -Inf;
    end
end

% find bounding boxes of objects
bbox = getbb(pfea, fscore, model);

% crop image
imcrop = [];
depthcrop = [];
if model.imagetag
  ims = imresize(im,bbox.scale);
  imcrop = ims(bbox.top:bbox.bottom,bbox.left:bbox.right,:);
end
if model.depthtag
   depths = imresize(depth,bbox.scale);
   depthcrop = depths(bbox.top:bbox.bottom,bbox.left:bbox.right,:);
end

function [bbox, bboxscale] = getbb(pfea, fscore, model)

% find bounding boxes
for i = 1:length(fscore)
    mscore(i,1) = max(fscore{i}(:));
    [ttt, lll] = find(fscore{i} == mscore(i));
    mtop(i,1) = ttt(1);
    mleft(i,1) = lll(1);
end
[bbox.score, bind] = max(mscore);
bbox.scale = pfea.scale(bind);

% compute bounding boxes at the corresponding scale
bbox.top = model.grid*(mtop(bind) - 1);
if bbox.top < 1
   bbox.top = 1;
end
bbox.left = model.grid*(mleft(bind) - 1);
if bbox.left < 1
   bbox.left = 1;
end
bbox.bottom = bbox.top + model.grid*(model.fheight + 2) - 1;
if bbox.bottom > bbox.scale*pfea.imsize(1)
   bbox.bottom = floor(bbox.scale*pfea.imsize(1));
   bbox.top = bbox.bottom - model.height + 1;
end
bbox.right = bbox.left + model.grid*(model.fwidth + 2) - 1;
if bbox.right > bbox.scale*pfea.imsize(2)
   bbox.right = floor(bbox.scale*pfea.imsize(2));
   bbox.left = bbox.right - model.width + 1;
end

