function superMask = superpix_group(mask, N_svox_in) % this function contains supervoxel grouping fprintf(['\n' 'Creating ' num2str(N_svox_in) ' SupVox:\n' ]) canvas2 = sqrt(bwdist(1-mask)); % orthoslice (canvas2) % find box crop of ROI ymin = 0; ymax = 0; for yi = 1:size(mask,1) data = mask(yi, :,:); if max(data(:)) >0 if ymin == 0 ymin = yi; end ymax = yi; end end xmin = 0; xmax = 0; for xi = 1:size(mask,2) data = mask(:,xi,:); if max(data(:)) >0 if xmin == 0 xmin = xi; end xmax = xi; end end zmin = 0; zmax = 0; for zi = 1:size(mask,3) data = mask(:,:,zi); if max(data(:)) >0 if zmin == 0 zmin = zi; end zmax = zi; end end canvas3 = canvas2(ymin:ymax, xmin:xmax, zmin:zmax); N_svox = N_svox_in; % number of supervoxels to give as param to parse while true [L,NumLabels] = superpixels3(canvas3,N_svox); superMask = zeros(size(mask)); superMask(ymin:ymax, xmin:xmax, zmin:zmax) = L; superMask(logical(1-mask)) = 0; numSupVox = numel(unique(superMask))-1; % number of created supervoxels fprintf([num2str(numSupVox) ' areas created' ]) if abs(numSupVox-N_svox_in)/N_svox_in < 0.2 break end N_svox = round(N_svox * N_svox_in/numSupVox); fprintf([' - not ok. ' num2str(numSupVox/N_svox_in) ' of target.\n']) end fprintf([' - ok.\n']) end