nrrd2geometry.m 6.0 KB

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