nrrd2geometry.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. function [Geometry patient_dir] = nrrd2geometry
  2. %% -----===== Get CT files =====-----
  3. [IMG_in, IMG_path, ~] = uigetfile(['C:\010-work\003_localGit\WiscPlan_v2\data\', '*.nrrd'], 'Select CT image');
  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. if IMG_in==0 % no file selected, no change to CT data
  20. warning('No file selected!')
  21. else
  22. [Geometry.data, meta]=nrrdread([IMG_path, IMG_in]);
  23. end
  24. % Geometry.rhomw
  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
  29. % VoxSizeString = meta.spacedirections;
  30. % C=textscan(VoxSizeString(2:end-1), '%f', 9,'Delimiter',{',',') ('});
  31. % C= C{1};
  32. % Geometry.voxel_size = [C(1), C(5), C(9)];
  33. Geometry.voxel_size = meta.spacedirections;
  34. % Geometry.start
  35. % StartString = meta.spaceorigin;
  36. % C=textscan(StartString(2:end-1), '%f', 3,'Delimiter',',');
  37. % Geometry.start=C{1};
  38. % Geometry.start=Geometry.start';
  39. Geometry.start=meta.spaceorigin;
  40. % Geometry.ROIS
  41. %% -----===== Get mask/contour files =====-----
  42. [TRGT_in, TRGT_path, ~] = uigetfile(['C:\010-work\003_localGit\WiscPlan_v2\CDP_data\','*.nrrd'], 'Select file with Target');
  43. % TRGT_in = 'test_seg.nrrd';
  44. % TRGT_path = 'C:\010-work\003_localGit\WiscPlan_v2\PhantomData\';
  45. if TRGT_in==0 % no file selected, no change to CT data
  46. warning('No file selected!')
  47. else
  48. [TRGT_img, meta]=nrrdread([TRGT_path, TRGT_in]);
  49. end
  50. Geometry.ROIS{1}.ind = find(TRGT_img>0);
  51. Geometry.ROIS{1}.name= 'Target';
  52. %% -----===== Save geometry files =====-----
  53. patient_dir = uifile('getdir', 'Save the patient data to directory');
  54. % patient_dir = 'C:\010-work\003_localGit\WiscPlan_v2\data\PatientData\';
  55. % make directories
  56. mkdir(patient_dir);
  57. mkdir(fullfile(patient_dir, 'beamlet_batch_files'));
  58. mkdir(fullfile(patient_dir, 'geometry_files'));
  59. mkdir(fullfile(patient_dir, 'matlab_files'));
  60. mkdir(fullfile(patient_dir, 'opt_input'));
  61. mkdir(fullfile(patient_dir, 'opt_output'));
  62. %% -----===== BTV and Ring creation =====-----
  63. % waitbar(1/total_num_steps, hWaitbar, 'Step 2: Assign target and BTV margin');
  64. % ROI_names = cellfun(@(c)c.name, Geometry.ROIS, 'UniformOutput', false);
  65. %
  66. % [target_idx okay] = listdlg('ListString', ROI_names, ...
  67. % 'SelectionMode', 'single', 'Name', 'Target Selection', ...
  68. % 'PromptString', 'Please select the target ROI. ');
  69. % if okay ~= 1
  70. % msgbox('Plan creation aborted');
  71. % delete(hWaitbar);
  72. % return;
  73. % end
  74. %
  75. % [BTV_margin_answer] = inputdlg({sprintf('Please enter the BTV margin (cm):\n(default 0.6 cm or 1 sigma, enter 0 to skip)')}, ...
  76. % 'BTV margin specification', 1, {'0.6'});
  77. % if isempty(BTV_margin_answer)
  78. % BTV_margin = 0.6;
  79. % else
  80. % BTV_margin = str2double(BTV_margin_answer{1});
  81. % end
  82. %
  83. % % target_idx and BTV_margin are set. Expand PTV to BTV
  84. % PTVmask = false(size(Geometry.rhomw));
  85. % % for target_idx = target_indices
  86. % PTVmask(Geometry.ROIS{target_idx}.ind) = 1;
  87. % % end
  88. % if BTV_margin > 0
  89. % if exist('BTV_margin', 'var') && BTV_margin >= min(Geometry.voxel_size)
  90. % bwD = bwdistsc(PTVmask, Geometry.voxel_size);
  91. % Geometry.BTV = bwD <= BTV_margin;
  92. % end
  93. % end
  94. %
  95. % % Create btv
  96. % Geometry.ROIS{end+1} = Geometry.ROIS{end};
  97. % Geometry.ROIS{end}.name = 'BTV';
  98. % Geometry.ROIS{end}.num_curves = 0;
  99. % Geometry.ROIS{end}.curves = {};
  100. % Geometry.ROIS{end}.ind = find(Geometry.BTV);
  101. % Geometry.ROIS{end}.visible = false;
  102. %
  103. % % Create ring
  104. % [ring_margin_answer] = inputdlg({sprintf('Please enter the ring margin (cm):')}, ...
  105. % 'Ring margin specification', 1, {'1'});
  106. % if isempty(ring_margin_answer)
  107. % ring_margin = 1;
  108. % else
  109. % ring_margin = str2double(ring_margin_answer{1});
  110. % end
  111. % bwD = bwdistsc(PTVmask, Geometry.voxel_size);
  112. % Geometry.Ring = bwD <= ring_margin; % default ring radius 3 cm
  113. % Geometry.Ring = xor(Geometry.Ring, PTVmask);
  114. % Geometry.ROIS{end+1} = Geometry.ROIS{end};
  115. % Geometry.ROIS{end}.name = 'Ring';
  116. % Geometry.ROIS{end}.num_curves = 0;
  117. % Geometry.ROIS{end}.curves = {};
  118. % Geometry.ROIS{end}.ind = find(Geometry.Ring);
  119. % Geometry.ROIS{end}.visible = false;
  120. %% -----===== Save the matlab files =====-----
  121. waitbar(2.33/total_num_steps, hWaitbar, 'Step 3: Save matlab geometry files');
  122. % Save matlab geometry file
  123. save(fullfile(patient_dir, 'matlab_files', 'Geometry.mat'), 'Geometry');
  124. waitbar(2.66/total_num_steps, hWaitbar, 'Step 3: Save raw geometry files');
  125. % Write binary geometry files
  126. fid = fopen(fullfile(patient_dir, 'geometry_files', 'rhomw.bin'), 'w');
  127. fwrite(fid, Geometry.rhomw, 'single');
  128. fclose(fid);
  129. fid = fopen(fullfile(patient_dir, 'geometry_files', 'Smw.bin'), 'w');
  130. fwrite(fid, Geometry.Smw, 'single');
  131. fclose(fid);
  132. fid = fopen(fullfile(patient_dir, 'geometry_files', 'Fmw2.bin'), 'w');
  133. fwrite(fid, Geometry.Fmw2, 'single');
  134. fclose(fid);
  135. % fid = fopen(fullfile(patient_dir, 'geometry_files', 'target_mask.bin'), 'w');
  136. % fwrite(fid, Geometry.BTV, 'single');
  137. % fclose(fid);
  138. % hWaitbar = waitbar(1/total_num_steps, 'Step 4, Create optimization geometry');
  139. delete(hWaitbar);
  140. waitfor(msgbox(['Plan geometry created successfully in ' '"' patient_dir '"']));
  141. end