superpix_group.m 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. function superMask = superpix_group(mask, N_svox_in, orthoPlot)
  2. % this function contains supervoxel grouping
  3. fprintf(['\n' 'Creating ' num2str(N_svox_in) ' SupVox:\n' ])
  4. canvas2 = sqrt(bwdist(1-mask));
  5. % orthoslice (canvas2)
  6. % find box crop of ROI
  7. ymin = 0;
  8. ymax = 0;
  9. for yi = 1:size(mask,1)
  10. data = mask(yi, :,:);
  11. if max(data(:)) >0
  12. if ymin == 0
  13. ymin = yi;
  14. end
  15. ymax = yi;
  16. end
  17. end
  18. xmin = 0;
  19. xmax = 0;
  20. for xi = 1:size(mask,2)
  21. data = mask(:,xi,:);
  22. if max(data(:)) >0
  23. if xmin == 0
  24. xmin = xi;
  25. end
  26. xmax = xi;
  27. end
  28. end
  29. zmin = 0;
  30. zmax = 0;
  31. for zi = 1:size(mask,3)
  32. data = mask(:,:,zi);
  33. if max(data(:)) >0
  34. if zmin == 0
  35. zmin = zi;
  36. end
  37. zmax = zi;
  38. end
  39. end
  40. canvas3 = canvas2(ymin:ymax, xmin:xmax, zmin:zmax);
  41. N_svox = N_svox_in; % number of supervoxels to give as param to parse
  42. converge_factor = 1;
  43. while true
  44. [L,NumLabels] = superpixels3(canvas3,N_svox);
  45. superMask = zeros(size(mask));
  46. superMask(ymin:ymax, xmin:xmax, zmin:zmax) = L;
  47. superMask(logical(1-mask)) = 0;
  48. numSupVox = numel(unique(superMask))-1; % number of created supervoxels
  49. fprintf([num2str(numSupVox) ' areas created' ])
  50. if abs(numSupVox-N_svox_in)/N_svox_in < 0.2/sqrt(converge_factor)
  51. switch orthoPlot
  52. case 'yes'
  53. orthoslice(superMask)
  54. case 'no'
  55. fprintf('\n supervoxel volume created! (orthoslice skipped)')
  56. end
  57. break
  58. end
  59. N_svox2 = N_svox * N_svox_in/numSupVox;
  60. N_svox = round(converge_factor * N_svox2 + (1-converge_factor)*N_svox);
  61. converge_factor = converge_factor * 0.95;
  62. fprintf([' - not ok. ' num2str(numSupVox/N_svox_in) ' of target. New start size: ' num2str(N_svox) '\n'])
  63. if converge_factor < 0.01
  64. % orthoslice(superMask)
  65. break
  66. end
  67. end
  68. fprintf([' - ok.\n'])
  69. end