1
0

nrrd2geometry.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. function [Geometry patient_dir] = nrrd2geometry
  2. %% -----===== Get CT files =====-----
  3. [IMG_in, IMG_path, filterIdx] = uigetfile([{'*.nrrd'; '*.am'}], 'Select CT image', 'C:\Box Sync\Optimal Stopping\Data\CDP\CDP001\');
  4. % IMG_in = 'test_ct.nrrd';
  5. % IMG_path = 'C:\010-work\003_localGit\WiscPlan_v2\PhantomData\';
  6. total_num_steps = 4;
  7. hWaitbar = waitbar(0);
  8. % geometry_file important parts:
  9. % Geometry.ROIS - actually only to get the binary mask of the target
  10. % Geometry.data - CT image in HU
  11. % Geometry.voxel_size - self explanatory
  12. % Geometry.start - self explanatory
  13. % these are derived from above:
  14. % +Geometry.rhomw - CT image with values in relative water density
  15. % +Geometry.Smw - very similair to rhomw
  16. % +Geometry.Fmw2
  17. % +Geometry.BTV
  18. % +Geometry.ring
  19. switch filterIdx
  20. case 0
  21. warning('No file selected!')
  22. case 1
  23. [Geometry.data, meta]=nrrdread([IMG_path, IMG_in]);
  24. % warning('NRRD file loaded!')
  25. Geometry.rhomw = CT2dens(Geometry.data, 'pinn');
  26. Geometry.rhomw(Geometry.rhomw < 0.0012) = 0.0012;
  27. [Geometry.Smw Geometry.Fmw2] = dens2mstp(Geometry.rhomw);
  28. Geometry.voxel_size = meta.spacedirections;
  29. Geometry.start=meta.spaceorigin;
  30. case 2
  31. imgIn=am2mat([IMG_path, IMG_in]);
  32. Geometry.data = permute(imgIn.data, [2,1,3]);
  33. % warning('NRRD file loaded!')
  34. Geometry.rhomw = CT2dens(Geometry.data, 'pinn');
  35. Geometry.rhomw(Geometry.rhomw < 0.0012) = 0.0012;
  36. [Geometry.Smw Geometry.Fmw2] = dens2mstp(Geometry.rhomw);
  37. Geometry.voxel_size = imgIn.voxel_size;
  38. Geometry.start=imgIn.start;
  39. case 3
  40. % something selected
  41. otherwise
  42. % error('You should never see this.')
  43. end
  44. % Geometry.ROIS
  45. %% -----===== Get mask/contour files =====-----
  46. [TRGT_in, TRGT_path, filterIdx2] = uigetfile([{'*.nrrd'; '*.am'}], 'Select file with Target', 'C:\Box Sync\Optimal Stopping\Data\CDP\CDP001\');
  47. switch filterIdx2
  48. case 0
  49. warning('No file selected!')
  50. case 1
  51. [TRGT_img, meta]=nrrdread([TRGT_path, TRGT_in]);
  52. warning('NRRD file loaded!')
  53. Geometry.ROIS{1}.ind = find(TRGT_img>0);
  54. Geometry.ROIS{1}.name= 'Target';
  55. case 2
  56. TRGT_in=am2mat([TRGT_path, TRGT_in]);
  57. warning('AM file loaded!')
  58. TRGT_img = permute(TRGT_in.data, [2,1,3]);
  59. Geometry.ROIS{1}.ind = find(TRGT_img>0);
  60. Geometry.ROIS{1}.name= 'Target';
  61. case 3
  62. % something selected
  63. otherwise
  64. % error('You should never see this.')
  65. end
  66. %% -----===== Save geometry files =====-----
  67. patient_dir = uifile('getdir', 'Save the patient data to directory');
  68. % patient_dir = 'C:\010-work\003_localGit\WiscPlan_v2\data\PatientData\';
  69. % make directories
  70. mkdir(patient_dir);
  71. mkdir(fullfile(patient_dir, 'beamlet_batch_files'));
  72. mkdir(fullfile(patient_dir, 'geometry_files'));
  73. mkdir(fullfile(patient_dir, 'matlab_files'));
  74. mkdir(fullfile(patient_dir, 'opt_input'));
  75. mkdir(fullfile(patient_dir, 'opt_output'));
  76. %% -----===== BTV and Ring creation =====-----
  77. % waitbar(1/total_num_steps, hWaitbar, 'Step 2: Assign target and BTV margin');
  78. % ROI_names = cellfun(@(c)c.name, Geometry.ROIS, 'UniformOutput', false);
  79. %
  80. % [target_idx okay] = listdlg('ListString', ROI_names, ...
  81. % 'SelectionMode', 'single', 'Name', 'Target Selection', ...
  82. % 'PromptString', 'Please select the target ROI. ');
  83. % if okay ~= 1
  84. % msgbox('Plan creation aborted');
  85. % delete(hWaitbar);
  86. % return;
  87. % end
  88. %
  89. % [BTV_margin_answer] = inputdlg({sprintf('Please enter the BTV margin (cm):\n(default 0.6 cm or 1 sigma, enter 0 to skip)')}, ...
  90. % 'BTV margin specification', 1, {'0.6'});
  91. % if isempty(BTV_margin_answer)
  92. % BTV_margin = 0.6;
  93. % else
  94. % BTV_margin = str2double(BTV_margin_answer{1});
  95. % end
  96. %
  97. % % target_idx and BTV_margin are set. Expand PTV to BTV
  98. % PTVmask = false(size(Geometry.rhomw));
  99. % % for target_idx = target_indices
  100. % PTVmask(Geometry.ROIS{target_idx}.ind) = 1;
  101. % % end
  102. % if BTV_margin > 0
  103. % if exist('BTV_margin', 'var') && BTV_margin >= min(Geometry.voxel_size)
  104. % bwD = bwdistsc(PTVmask, Geometry.voxel_size);
  105. % Geometry.BTV = bwD <= BTV_margin;
  106. % end
  107. % end
  108. %
  109. % % Create btv
  110. % Geometry.ROIS{end+1} = Geometry.ROIS{end};
  111. % Geometry.ROIS{end}.name = 'BTV';
  112. % Geometry.ROIS{end}.num_curves = 0;
  113. % Geometry.ROIS{end}.curves = {};
  114. % Geometry.ROIS{end}.ind = find(Geometry.BTV);
  115. % Geometry.ROIS{end}.visible = false;
  116. %
  117. % % Create ring
  118. % [ring_margin_answer] = inputdlg({sprintf('Please enter the ring margin (cm):')}, ...
  119. % 'Ring margin specification', 1, {'1'});
  120. % if isempty(ring_margin_answer)
  121. % ring_margin = 1;
  122. % else
  123. % ring_margin = str2double(ring_margin_answer{1});
  124. % end
  125. % bwD = bwdistsc(PTVmask, Geometry.voxel_size);
  126. % Geometry.Ring = bwD <= ring_margin; % default ring radius 3 cm
  127. % Geometry.Ring = xor(Geometry.Ring, PTVmask);
  128. % Geometry.ROIS{end+1} = Geometry.ROIS{end};
  129. % Geometry.ROIS{end}.name = 'Ring';
  130. % Geometry.ROIS{end}.num_curves = 0;
  131. % Geometry.ROIS{end}.curves = {};
  132. % Geometry.ROIS{end}.ind = find(Geometry.Ring);
  133. % Geometry.ROIS{end}.visible = false;
  134. %% -----===== Save the matlab files =====-----
  135. waitbar(2.33/total_num_steps, hWaitbar, 'Step 3: Save matlab geometry files');
  136. % Save matlab geometry file
  137. save(fullfile(patient_dir, 'matlab_files', 'Geometry.mat'), 'Geometry');
  138. waitbar(2.66/total_num_steps, hWaitbar, 'Step 3: Save raw geometry files');
  139. % Write binary geometry files
  140. fid = fopen(fullfile(patient_dir, 'geometry_files', 'rhomw.bin'), 'w');
  141. fwrite(fid, Geometry.rhomw, 'single');
  142. fclose(fid);
  143. fid = fopen(fullfile(patient_dir, 'geometry_files', 'Smw.bin'), 'w');
  144. fwrite(fid, Geometry.Smw, 'single');
  145. fclose(fid);
  146. fid = fopen(fullfile(patient_dir, 'geometry_files', 'Fmw2.bin'), 'w');
  147. fwrite(fid, Geometry.Fmw2, 'single');
  148. fclose(fid);
  149. % fid = fopen(fullfile(patient_dir, 'geometry_files', 'target_mask.bin'), 'w');
  150. % fwrite(fid, Geometry.BTV, 'single');
  151. % fclose(fid);
  152. % hWaitbar = waitbar(1/total_num_steps, 'Step 4, Create optimization geometry');
  153. delete(hWaitbar);
  154. waitfor(msgbox(['Plan geometry created successfully in ' '"' patient_dir '"']));
  155. end