nrrd2geometry.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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']);
  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']);
  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. [target_idx okay] = listdlg('ListString', ROI_names, ...
  66. 'SelectionMode', 'single', 'Name', 'Target Selection', ...
  67. 'PromptString', 'Please select the target ROI. ');
  68. if okay ~= 1
  69. msgbox('Plan creation aborted');
  70. delete(hWaitbar);
  71. return;
  72. end
  73. [BTV_margin_answer] = inputdlg({sprintf('Please enter the BTV margin (cm):\n(default 0.6 cm or 1 sigma, enter 0 to skip)')}, ...
  74. 'BTV margin specification', 1, {'0.6'});
  75. if isempty(BTV_margin_answer)
  76. BTV_margin = 0.6;
  77. else
  78. BTV_margin = str2double(BTV_margin_answer{1});
  79. end
  80. % target_idx and BTV_margin are set. Expand PTV to BTV
  81. PTVmask = false(size(Geometry.rhomw));
  82. % for target_idx = target_indices
  83. PTVmask(Geometry.ROIS{target_idx}.ind) = 1;
  84. % end
  85. if BTV_margin > 0
  86. if exist('BTV_margin', 'var') && BTV_margin >= min(Geometry.voxel_size)
  87. bwD = bwdistsc(PTVmask, Geometry.voxel_size);
  88. Geometry.BTV = bwD <= BTV_margin;
  89. end
  90. end
  91. % Create btv
  92. Geometry.ROIS{end+1} = Geometry.ROIS{end};
  93. Geometry.ROIS{end}.name = 'BTV';
  94. Geometry.ROIS{end}.num_curves = 0;
  95. Geometry.ROIS{end}.curves = {};
  96. Geometry.ROIS{end}.ind = find(Geometry.BTV);
  97. Geometry.ROIS{end}.visible = false;
  98. % Create ring
  99. [ring_margin_answer] = inputdlg({sprintf('Please enter the ring margin (cm):')}, ...
  100. 'Ring margin specification', 1, {'1'});
  101. if isempty(ring_margin_answer)
  102. ring_margin = 1;
  103. else
  104. ring_margin = str2double(ring_margin_answer{1});
  105. end
  106. bwD = bwdistsc(PTVmask, Geometry.voxel_size);
  107. Geometry.Ring = bwD <= ring_margin; % default ring radius 3 cm
  108. Geometry.Ring = xor(Geometry.Ring, PTVmask);
  109. Geometry.ROIS{end+1} = Geometry.ROIS{end};
  110. Geometry.ROIS{end}.name = 'Ring';
  111. Geometry.ROIS{end}.num_curves = 0;
  112. Geometry.ROIS{end}.curves = {};
  113. Geometry.ROIS{end}.ind = find(Geometry.Ring);
  114. Geometry.ROIS{end}.visible = false;
  115. waitbar(2.33/total_num_steps, hWaitbar, 'Step 3: Save matlab geometry files');
  116. % Save matlab geometry file
  117. save(fullfile(patient_dir, 'matlab_files', 'Geometry.mat'), 'Geometry');
  118. waitbar(2.66/total_num_steps, hWaitbar, 'Step 3: Save raw geometry files');
  119. % Write binary geometry files
  120. fid = fopen(fullfile(patient_dir, 'geometry_files', 'rhomw.bin'), 'w');
  121. fwrite(fid, Geometry.rhomw, 'single');
  122. fclose(fid);
  123. fid = fopen(fullfile(patient_dir, 'geometry_files', 'Smw.bin'), 'w');
  124. fwrite(fid, Geometry.Smw, 'single');
  125. fclose(fid);
  126. fid = fopen(fullfile(patient_dir, 'geometry_files', 'Fmw2.bin'), 'w');
  127. fwrite(fid, Geometry.Fmw2, 'single');
  128. fclose(fid);
  129. fid = fopen(fullfile(patient_dir, 'geometry_files', 'target_mask.bin'), 'w');
  130. fwrite(fid, Geometry.BTV, 'single');
  131. fclose(fid);
  132. % hWaitbar = waitbar(1/total_num_steps, 'Step 4, Create optimization geometry');
  133. delete(hWaitbar);
  134. waitfor(msgbox(['Plan geometry created successfully in ' '"' patient_dir '"']));
  135. end