Skip to content

Commit

Permalink
- Empty UCMs fixed for the full package
Browse files Browse the repository at this point in the history
Fixes #2
  • Loading branch information
jponttuset committed Dec 11, 2014
1 parent e0abd46 commit 0d6aa1c
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 47 deletions.
21 changes: 17 additions & 4 deletions full/im2mcg.m
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,13 @@
[f_lp,f_ms,cands,start_ths,end_ths] = full_cands_from_hiers(lps,ms,ths,pareto_n_cands);

% Hole filling and complementary candidates
[cands_hf, cands_comp] = hole_filling(double(f_lp), double(f_ms), cands); %#ok<NASGU>

if ~isempty(f_ms)
[cands_hf, cands_comp] = hole_filling(double(f_lp), double(f_ms), cands); %#ok<NASGU>
else
cands_hf = cands;
cands_comp = cands; %#ok<NASGU>
end

% Select which candidates to keep (Uncomment just one line)
cands = cands_hf; % Just the candidates with holes filled
% cands = [cands_hf; cands_comp]; % Holes filled and the complementary
Expand Down Expand Up @@ -145,9 +150,17 @@

% Get the labels of leave regions that form each candidates
candidates.superpixels = f_lp;
candidates.labels = cands2labels(cand_labels,f_ms);
if ~isempty(f_ms)
candidates.labels = cands2labels(cand_labels,f_ms);
else
candidates.labels = {1};
end

% Transform the results to masks
if compute_masks
candidates.masks = cands2masks(cand_labels, f_lp, f_ms);
if ~isempty(f_ms)
candidates.masks = cands2masks(cand_labels, f_lp, f_ms);
else
candidates.masks = true(size(f_lp));
end
end
3 changes: 3 additions & 0 deletions full/src/aux/ms_matrix2struct.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@
children(children==0) = [];
ms_struct(ii).children = children; %#ok<AGROW>
end
if size(ms_matrix,1)==0
ms_struct = [];
end
end
108 changes: 65 additions & 43 deletions full/src/cands/full_cands_from_hiers.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,48 @@
% Scan all hierarchies
all_cands = cell(n_hiers,n_r_cand);
for ii=1:n_hiers
n_r_hier = ms{ii}(end,end);
assert(length(unique(lps(:,:,ii)))==ms{ii}(1,end)-1)
assert(ms{ii}(1,end)-1+size(ms{ii},1)==n_r_hier)

% Get all pairs of neighboring leave regions
[~, idx_neighbors] = seg2gridbmap(lps(:,:,ii));
K = max(idx_neighbors.matrix_max(:)) + 1;
neigh_pairs = unique(idx_neighbors.matrix_min+K*idx_neighbors.matrix_max);
neigh_pairs(neigh_pairs==0) = [];
neigh_pairs_min = mod(neigh_pairs,K);
neigh_pairs_max = (neigh_pairs-neigh_pairs_min)/K;

if isrow(neigh_pairs_min)
neigh_pairs_min = neigh_pairs_min';
end
if isrow(neigh_pairs_max)
neigh_pairs_max = neigh_pairs_max';
end
if size(ms{ii},1)>0
n_r_hier = ms{ii}(end,end);
assert(length(unique(lps(:,:,ii)))==ms{ii}(1,end)-1)
assert(ms{ii}(1,end)-1+size(ms{ii},1)==n_r_hier)

% Get all pairs of neighboring leave regions
[~, idx_neighbors] = seg2gridbmap(lps(:,:,ii));
K = max(idx_neighbors.matrix_max(:)) + 1;
neigh_pairs = unique(idx_neighbors.matrix_min+K*idx_neighbors.matrix_max);
neigh_pairs(neigh_pairs==0) = [];
neigh_pairs_min = mod(neigh_pairs,K);
neigh_pairs_max = (neigh_pairs-neigh_pairs_min)/K;

if isrow(neigh_pairs_min)
neigh_pairs_min = neigh_pairs_min';
end
if isrow(neigh_pairs_max)
neigh_pairs_max = neigh_pairs_max';
end

% Get the 'n_cands' top candidates from each hierarchy
% Singletons
if n_r_hier<=n_cands(1,ii)
all_cands{ii,1} = (1:n_r_hier)';
% Get the 'n_cands' top candidates from each hierarchy
% Singletons
if n_r_hier<=n_cands(1,ii)
all_cands{ii,1} = (1:n_r_hier)';
else
all_cands{ii,1} = (n_r_hier-n_cands(1,ii)+1:n_r_hier)';
end

% % Pairs, triplets
% [all_cands{ii,2}, all_cands{ii,3}] = ...
% mex_get_tree_cands(double(lps(:,:,ii))-1, double(ms{ii})-1,...
% neigh_pairs_min-1, neigh_pairs_max-1,...
% [n_cands(2,ii), n_cands(3,ii)]);

% Pairs, triplets, etc.
all_cands(ii,2:n_r_cand) = ...
mex_get_tree_cands(double(lps(:,:,ii))-1, double(ms{ii})-1,...
neigh_pairs_min-1, neigh_pairs_max-1,...
n_cands(2:end,ii));
else
all_cands{ii,1} = (n_r_hier-n_cands(1,ii)+1:n_r_hier)';
all_cands{1} = 1;
end

% % Pairs, triplets
% [all_cands{ii,2}, all_cands{ii,3}] = ...
% mex_get_tree_cands(double(lps(:,:,ii))-1, double(ms{ii})-1,...
% neigh_pairs_min-1, neigh_pairs_max-1,...
% [n_cands(2,ii), n_cands(3,ii)]);

% Pairs, triplets, etc.
all_cands(ii,2:n_r_cand) = ...
mex_get_tree_cands(double(lps(:,:,ii))-1, double(ms{ii})-1,...
neigh_pairs_min-1, neigh_pairs_max-1,...
n_cands(2:end,ii));

end

% Put all them together (for each hierarchy)
Expand All @@ -72,11 +75,16 @@
n_tot_cand = n_tot_cand + size(all_cands{ii,jj},1);
end
full_cands{ii} = zeros(n_tot_cand,n_r_cand);
% Store
curr_n = 0;
for jj=1:n_r_cand
full_cands{ii}(curr_n+1:curr_n+size(all_cands{ii,jj},1),1:jj) = all_cands{ii,jj};
curr_n = curr_n+size(all_cands{ii,jj},1);

if n_tot_cand
% Store
curr_n = 0;
for jj=1:n_r_cand
if size(all_cands{ii,jj},1)>0
full_cands{ii}(curr_n+1:curr_n+size(all_cands{ii,jj},1),1:jj) = all_cands{ii,jj};
curr_n = curr_n+size(all_cands{ii,jj},1);
end
end
end
end

Expand All @@ -87,7 +95,11 @@
needed_regs = unique(full_cands{ii}(:));
needed_regs(needed_regs==0) = [];

[lps(:,:,ii),ms{ii},luts{ii}] = mex_prune_tree_to_regions(lps(:,:,ii)-1,ms{ii}-1,needed_regs-1);
if isempty(needed_regs) || isempty(ms{ii})
lps(:,:,ii) = ones(size(lps,1),size(lps,2));
else
[lps(:,:,ii),ms{ii},luts{ii}] = mex_prune_tree_to_regions(lps(:,:,ii)-1,ms{ii}-1,needed_regs-1);
end
end

% Fuse
Expand Down Expand Up @@ -120,9 +132,19 @@
curr_n_regs = curr_n_regs + ms{jj}(end,end);
% Sanity check
assert(ms{jj}(end,end)==length(unique(lps(:,:,jj))) + size(ms{jj},1))
else
if ~isempty(full_cands{jj})
cands = [cands; full_cands{jj}]; %#ok<AGROW>
end
end
end
assert(isequal(size(cell2mat(full_cands)), size(cands)))

% Sanity checks
if ~isempty(cands)
assert(isequal(size(cell2mat(full_cands)), size(cands)))
else
assert(isempty(cell2mat(full_cands)))
end

% Redo ths
start_ths = zeros(1,n_int);
Expand Down
23 changes: 23 additions & 0 deletions full/src/tests/empty_ucm.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
% ------------------------------------------------------------------------
% Copyright (C)
% Universitat Politecnica de Catalunya BarcelonaTech (UPC) - Spain
% University of California Berkeley (UCB) - USA
%
% Jordi Pont-Tuset <[email protected]>
% Pablo Arbelaez <[email protected]>
% June 2014
% ------------------------------------------------------------------------
% This file is part of the MCG package presented in:
% Arbelaez P, Pont-Tuset J, Barron J, Marques F, Malik J,
% "Multiscale Combinatorial Grouping,"
% Computer Vision and Pattern Recognition (CVPR) 2014.
% Please consider citing the paper if you use this code.
% ------------------------------------------------------------------------
% Test robustness to empty ucms
% ------------------------------------------------------------------------

% Empty input image
I = zeros(40,80);

candidates_scg = im2mcg(I,'fast');
candidates_mcg = im2mcg(I,'accurate');

0 comments on commit 0d6aa1c

Please sign in to comment.