superpix_group.m 2.0 KB

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