function [ feature ] = pool_region_feats(feature_map, map_info, mask, type)
% Get superpixel region coordinates
[y_coord, x_coord] = find(mask);

% Convert image coordinates to pooled feature map coordinates
x_map = round( (x_coord - map_info.center)/map_info.scale + 1 ) ;
x_map = max(x_map, 1);
x_map = min(x_map, map_info.width);
y_map = round( (y_coord - map_info.center)/map_info.scale + 1 ) ;
y_map = max(y_map, 1);
y_map = min(y_map, map_info.height);

% Convert subscripts to linear indices
inds = sub2ind([map_info.height map_info.width], y_map, x_map);
inds = unique(inds);

% Extract the appropriate codes
region_feats = feature_map(:,inds);

%% PLOT for DEBUG
% temp_map = zeros(map_info.height, map_info.width);
% temp_map(inds) = 1;
% subplot(1,2,1)
% imagesc(mask);
% subplot(1,2,2)
% imagesc(temp_map)
% keyboard

switch type
    case 'max'
        feature = max(region_feats, [], 2);
    otherwise
        error('Unknown aggregation method!');
end

end
