function superMask = superpix_group(mask, N_svox_in, orthoPlot) % this function contains supervoxel grouping fprintf(['\n' 'Creating ' num2str(N_svox_in) ' SupVox:\n' ]) canvas2 = mask; % 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 converge_factor = 1; while true [L,NumLabels] = superpixels3(canvas3,N_svox); superMask = zeros(size(mask)); superMask(ymin:ymax, xmin:xmax, zmin:zmax) = L; superMask(mask==0) = 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/sqrt(converge_factor) switch orthoPlot case 'yes' orthoslice(superMask) case 'no' fprintf('\n supervoxel volume created! (orthoslice skipped)') end break end N_svox2 = N_svox * N_svox_in/numSupVox; N_svox = round(converge_factor * N_svox2 + (1-converge_factor)*N_svox); converge_factor = converge_factor * 0.95; fprintf([' - not ok. ' num2str(numSupVox/N_svox_in) ' of target. New start size: ' num2str(N_svox) '\n']) if converge_factor < 0.01 % orthoslice(superMask) break end end fprintf([' - ok.\n']) end