function bbox = rgbdbbox(score, pfea, model, overlap)

if nargin < 4
   overlap = 0.5;
end

for i = 1:length(model)
    onebbox = getbbox(i, score, pfea, model);
end

bbox = bboxstructure(onebbox, score, model);

function bbox = bboxstructure(bboxmatrix, score, model)

for i = 1:size(bboxmatrix,1)
    bbox(i).category = model{bboxmatrix(i,9)}.category;
    bbox(i).instance = bboxmatrix(i,10);
    bbox(i).left = bboxmatrix(i,1);
    bbox(i).right = bboxmatrix(i,3);
    bbox(i).top = bboxmatrix(i,2);
    bbox(i).bottom = bboxmatrix(i,4);
    bbox(i).score = bboxmatrix(i,5);
    bbox(i).scale = bboxmatrix(i,6);
    bbox(i).cheight = bboxmatrix(i,7);
    bbox(i).cwidth = bboxmatrix(i,8);
    
    pixdims = [bbox(i).bottom-bbox(i).top+1 bbox(i).right-bbox(i).left+1];
    modelnum = bboxmatrix(i,9);

    pixScores = imresize(score{bboxmatrix(i,9),bboxmatrix(i,11)},model{modelnum}.grid/bbox(i).scale,'nearest');
    center = [round(size(pixScores,1)/2) round(size(pixScores,2)/2)];
    bbox(i).pixScores = nan(480,640);
    bbox(i).pixScores(240-center(1)+(1:size(pixScores,1)),320-center(2)+(1:size(pixScores,2))) = pixScores;
end


function bbox = nmscategory(bboxcell, overlap)

bbox = [];
for i = 1:length(bboxcell)
    bbox = [bbox; bboxcell{i}];
end
pick = nms(bbox, overlap);
bbox = bbox(pick,:);

function bbox = nmsinstance(bboxcell, overlap)

bbox = [];
for i = 1:length(bboxcell)
    bbox = [bbox; bboxcell{i}];
end
pick = nmsi(bbox, overlap);
bbox = bbox(pick,:);

function bbox = getbbox(modelnum, score, pfea, model)

grid = model{modelnum}.grid;
fheight = model{modelnum}.fheight;
fwidth = model{modelnum}.fwidth;
height = model{modelnum}.height;
width = model{modelnum}.width;

it = 0;
bbox = zeros(size(score,2),13);
for i = 1:size(score,2)
    mscore(i,1) = max(score{modelnum,i}(:));
    [mtop, mleft] = find(score{modelnum,i} == mscore(i,1));
    it=it+1;
    % compute bounding boxes
    bbox(it,9) = modelnum; % categroy
    bbox(it,10) = model{modelnum}.instance; % instance
    bbox(it,11:13) = [i mtop(1) mleft(1)];
    bbox(it,5) = score{modelnum,i}(mtop(1), mleft(1)); % score
    bbox(it,6) = pfea.scale(i); % scale
    bbox(it,2) = max(round(grid/pfea.scale(i)*(mtop(1)-1))+1,1); % top
    bbox(it,1) = max(round(grid/pfea.scale(i)*(mleft(1)-1))+1,1); % left
    bbox(it,4) = min(round(bbox(it,2) + grid/pfea.scale(i)*(fheight + 2)), pfea.imsize(1)); % bottom
    bbox(it,3) = min(round(bbox(it,1) + grid/pfea.scale(i)*(fwidth + 2)), pfea.imsize(2)); % right
    bbox(it,7) = round((bbox(it,2) + (bbox(it,4) - bbox(it,2))/2.0)); % height center
    bbox(it,8) = round((bbox(it,1) + (bbox(it,3) - bbox(it,1))/2.0)); % width center
end 

function pick = nmsi(boxes, overlap)
% pick = nmsi(boxes, overlap)
% non-maximum suppression.
% Greedily select high-scoring detections and skip detections
% that are significantly covered by a previously selected detection.

if isempty(boxes)
  pick = [];
else
  x1 = boxes(:,1);
  y1 = boxes(:,2);
  x2 = boxes(:,3);
  y2 = boxes(:,4);
  s = boxes(:,5);
  area = (x2-x1+1) .* (y2-y1+1);

  [vals, I] = sort(s);
  pick = [];
  while ~isempty(I)
    last = length(I);
    i = I(last);
    pick = [pick; i];
    suppress = [last];
    for pos = 1:last-1
      j = I(pos);
      xx1 = max(x1(i), x1(j));
      yy1 = max(y1(i), y1(j));
      xx2 = min(x2(i), x2(j));
      yy2 = min(y2(i), y2(j));
      w = xx2-xx1+1;
      h = yy2-yy1+1;
      if w > 0 && h > 0
        % compute overlap
        o = w * h / area(j);
        if o > overlap
          suppress = [suppress; pos];
        end
      end
      tag = boxes(i,9) == boxes(j,9) | boxes(i,10) == boxes(j,10);
      if tag & suppress(end) ~= pos
         suppress = [suppress; pos];
      end
    end
    I(suppress) = [];
  end
end


function pick = nms(boxes, overlap)
% pick = nms(boxes, overlap)
% non-maximum suppression.
% Greedily select high-scoring detections and skip detections
% that are significantly covered by a previously selected detection.

if isempty(boxes)
  pick = [];
else
  x1 = boxes(:,1);
  y1 = boxes(:,2);
  x2 = boxes(:,3);
  y2 = boxes(:,4);
  s = boxes(:,5);
  area = (x2-x1+1) .* (y2-y1+1);

  [vals, I] = sort(s);
  pick = [];
  while ~isempty(I)
    last = length(I);
    i = I(last);
    pick = [pick; i];
    suppress = [last];
    for pos = 1:last-1
      j = I(pos);
      xx1 = max(x1(i), x1(j));
      yy1 = max(y1(i), y1(j));
      xx2 = min(x2(i), x2(j));
      yy2 = min(y2(i), y2(j));
      w = xx2-xx1+1;
      h = yy2-yy1+1;
      if w > 0 && h > 0
        % compute overlap
        o = w * h / area(j);
        if o > overlap
          suppress = [suppress; pos];
        end
      end
    end
    I(suppress) = [];
  end
end


