dicomrt2geometry.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. function Geometry = dicomrt2geometry(dicomrt_dir)
  2. %DICOMRT2GEOMETRY Import DicomRT dataset to Ryan's "Geometry" struct
  3. % Usage:
  4. % [ Geometry ] = dicomrt2geometry ( [input_dir] )
  5. % Input:
  6. % input_dir = directory contains DicomRT data
  7. % [] = prompt to select input_dir
  8. % Output:
  9. % Geometry = Ryan's Geometry struct used with RDX TPS
  10. %
  11. % This function imports DicomRT data set and create RDX TPS compatible Geometry
  12. % struct.
  13. %
  14. % TODO: Switch from filename based Dicom identification to StudyID based.
  15. %
  16. % See also readPinnGeometry.m
  17. %
  18. % Author: Xiaohu Mo
  19. Geometry = [];
  20. if nargin < 1
  21. dicomrt_dir = uifile('getdir', 'Select the directory contains the DICOM-RT images');
  22. % !!! Grozomah
  23. % dicomrt_dir = 'C:\010-work\003_localGit\WiscPlan_v2\WiscPlanPhotonkV125\CT with Contours\DICOM\TomoPhantPlan01';
  24. end
  25. if exist(dicomrt_dir, 'dir') ~= 7
  26. msgbox('Cannot find input directory');
  27. return;
  28. end
  29. %% -----===== Start Importing =====-----
  30. % set dicom dictionary before any dicominfo or dicomread
  31. dicomdict('factory');
  32. % change path due to the limitation of DicomRT toolbox
  33. % will cd back later
  34. previousPath = pwd;
  35. cd(dicomrt_dir);
  36. % get the list of filenames
  37. dirlist = dir(dicomrt_dir);
  38. filenames = cell(0);
  39. % remove directories
  40. for i = 1:numel(dirlist)
  41. [tilde, tilde, ext] = fileparts(dirlist(i).name);
  42. if ~dirlist(i).isdir && strcmpi(ext, '.dcm')
  43. filenames{end+1} = dirlist(i).name;
  44. end
  45. end
  46. % get all dicominfo to a cell array the same size as filelist
  47. for i = 1:numel(filenames)
  48. dcminfo{i} = dicominfo(filenames{i});
  49. end
  50. % TODO integrity check
  51. % UID should match
  52. % should be only one RTSTRUCT file present
  53. %% -----===== Import Dicom CT data set =====-----
  54. % Create DicomRT ct list file
  55. fid = fopen('ctlist.txt', 'w');
  56. for i = 1:numel(dcminfo)
  57. if strcmpi(dcminfo{i}.Modality, 'CT')
  58. fprintf(fid, '%s\n', filenames{i});
  59. end
  60. end
  61. fclose(fid);
  62. [cellCt, ct_xmesh, ct_ymesh, ct_zmesh] = dicomrt_loadct('ctlist.txt');
  63. %% -----===== Set Downsampling flag =====-----
  64. button = questdlg('Do you want to downsample the CT dataset?', 'Downsampling', 'Yes', 'No', 'Yes');
  65. if strcmpi(button, 'yes')
  66. downsample_flag = true;
  67. else
  68. downsample_flag = false;
  69. end
  70. %% -----===== Import DicomRT structure =====-----
  71. % File DicomRT structure file
  72. for i = 1:numel(dcminfo)
  73. if strcmpi(dcminfo{i}.Modality, 'RTSTRUCT')
  74. rsfilename = fullfile(dicomrt_dir, filenames{i});
  75. end
  76. end
  77. % prompt if can not find RTSTRUCT file in this directory
  78. if isempty(rsfilename)
  79. [temp_FileName temp_PathName] = uifile('get', '*.dcm', 'Please select the DicomRT-Struct file');
  80. rsfilename = fullfile(temp_PathName, temp_FileName);
  81. end
  82. % Import the struct
  83. cellVoi = dicomrt_loadvoi(rsfilename);
  84. % Downsampling
  85. if downsample_flag == true
  86. if numel(ct_xmesh) < 128
  87. downsample_factor = 0.5;
  88. dx = ct_xmesh(2)-ct_xmesh(1);
  89. dy = ct_ymesh(2)-ct_ymesh(1);
  90. ct_xmesh = linspace(ct_xmesh(1)+dx/2, ct_xmesh(end)-dx/2, numel(ct_xmesh)*downsample_factor);
  91. ct_ymesh = linspace(ct_ymesh(1)+dy/2, ct_ymesh(end)-dy/2, numel(ct_ymesh)*downsample_factor);
  92. else
  93. downsample_factor = 0.125;
  94. dx = ct_xmesh(2)-ct_xmesh(1);
  95. dy = ct_ymesh(2)-ct_ymesh(1);
  96. ct_xmesh = linspace(ct_xmesh(1)+dx/2, ct_xmesh(end)-dx/2, numel(ct_xmesh)*downsample_factor);
  97. ct_ymesh = linspace(ct_ymesh(1)+dy/2, ct_ymesh(end)-dy/2, numel(ct_ymesh)*downsample_factor);
  98. end
  99. end
  100. ROIS = dicomrt_rasterizeVoi(cellVoi, cellCt, ct_xmesh, ct_ymesh, ct_zmesh);
  101. %% -----===== Done Importing =====-----
  102. % change the path back
  103. cd(previousPath);
  104. %% -----===== Create Geometry struct =====-----
  105. Geometry.ROIS = ROIS;
  106. Geometry.data = cellCt{2};
  107. % !!! Grozomah - added abs() to prevent errors in later functions
  108. Geometry.voxel_size = abs([ct_xmesh(2)-ct_xmesh(1) ...
  109. ct_ymesh(2)-ct_ymesh(1) ...
  110. ct_zmesh(2)-ct_zmesh(1)]);
  111. Geometry.start = [ct_xmesh(1) ct_ymesh(1) ct_zmesh(1)];
  112. if downsample_flag == true
  113. Geometry.data = resamplegrid(Geometry.data, [downsample_factor downsample_factor 1]);
  114. % Geometry.data = resamplegrid(Geometry.data, 0.5);
  115. % note that ct_xmesh, ct_ymesh, ct_zmesh is already downsampled
  116. Geometry.start(1:2) = Geometry.start(1:2) + Geometry.voxel_size(1:2) / 4;
  117. end
  118. Geometry.rhomw = CT2dens(Geometry.data, 'pinn');
  119. Geometry.rhomw(Geometry.rhomw < 0.0012) = 0.0012;
  120. [Geometry.Smw Geometry.Fmw2] = dens2mstp(Geometry.rhomw);