dicomrt2geometry.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. end
  23. if exist(dicomrt_dir, 'dir') ~= 7
  24. msgbox('Cannot find input directory');
  25. return;
  26. end
  27. %% -----===== Start Importing =====-----
  28. % set dicom dictionary before any dicominfo or dicomread
  29. dicomdict('factory');
  30. % change path due to the limitation of DicomRT toolbox
  31. % will cd back later
  32. previousPath = pwd;
  33. cd(dicomrt_dir);
  34. % get the list of filenames
  35. dirlist = dir(dicomrt_dir);
  36. filenames = cell(0);
  37. % remove directories
  38. for i = 1:numel(dirlist)
  39. [tilde, tilde, ext] = fileparts(dirlist(i).name);
  40. if ~dirlist(i).isdir && strcmpi(ext, '.dcm')
  41. filenames{end+1} = dirlist(i).name;
  42. end
  43. end
  44. % get all dicominfo to a cell array the same size as filelist
  45. for i = 1:numel(filenames)
  46. dcminfo{i} = dicominfo(filenames{i});
  47. end
  48. % TODO integrity check
  49. % UID should match
  50. % should be only one RTSTRUCT file present
  51. %% -----===== Import Dicom CT data set =====-----
  52. % Create DicomRT ct list file
  53. fid = fopen('ctlist.txt', 'w');
  54. for i = 1:numel(dcminfo)
  55. if strcmpi(dcminfo{i}.Modality, 'CT')
  56. fprintf(fid, '%s\n', filenames{i});
  57. end
  58. end
  59. fclose(fid);
  60. [cellCt, ct_xmesh, ct_ymesh, ct_zmesh] = dicomrt_loadct('ctlist.txt');
  61. %% -----===== Set Downsampling flag =====-----
  62. button = questdlg('Do you want to downsample the CT dataset?', 'Downsampling', 'Yes', 'No', 'Yes');
  63. if strcmpi(button, 'yes')
  64. downsample_flag = true;
  65. else
  66. downsample_flag = false;
  67. end
  68. %% -----===== Import DicomRT structure =====-----
  69. % File DicomRT structure file
  70. for i = 1:numel(dcminfo)
  71. if strcmpi(dcminfo{i}.Modality, 'RTSTRUCT')
  72. rsfilename = fullfile(dicomrt_dir, filenames{i});
  73. end
  74. end
  75. % prompt if can not find RTSTRUCT file in this directory
  76. if isempty(rsfilename)
  77. [temp_FileName temp_PathName] = uifile('get', '*.dcm', 'Please select the DicomRT-Struct file');
  78. rsfilename = fullfile(temp_PathName, temp_FileName);
  79. end
  80. % Import the struct
  81. cellVoi = dicomrt_loadvoi(rsfilename);
  82. % Downsampling
  83. if downsample_flag == true
  84. if numel(ct_xmesh) < 128
  85. downsample_factor = 0.5;
  86. dx = ct_xmesh(2)-ct_xmesh(1);
  87. dy = ct_ymesh(2)-ct_ymesh(1);
  88. ct_xmesh = linspace(ct_xmesh(1)+dx/2, ct_xmesh(end)-dx/2, numel(ct_xmesh)*downsample_factor);
  89. ct_ymesh = linspace(ct_ymesh(1)+dy/2, ct_ymesh(end)-dy/2, numel(ct_ymesh)*downsample_factor);
  90. else
  91. downsample_factor = 0.125;
  92. dx = ct_xmesh(2)-ct_xmesh(1);
  93. dy = ct_ymesh(2)-ct_ymesh(1);
  94. ct_xmesh = linspace(ct_xmesh(1)+dx/2, ct_xmesh(end)-dx/2, numel(ct_xmesh)*downsample_factor);
  95. ct_ymesh = linspace(ct_ymesh(1)+dy/2, ct_ymesh(end)-dy/2, numel(ct_ymesh)*downsample_factor);
  96. end
  97. end
  98. ROIS = dicomrt_rasterizeVoi(cellVoi, cellCt, ct_xmesh, ct_ymesh, ct_zmesh);
  99. %% -----===== Done Importing =====-----
  100. % change the path back
  101. cd(previousPath);
  102. %% -----===== Create Geometry struct =====-----
  103. Geometry.ROIS = ROIS;
  104. Geometry.data = cellCt{2};
  105. Geometry.voxel_size = [ct_xmesh(2)-ct_xmesh(1) ...
  106. ct_ymesh(2)-ct_ymesh(1) ...
  107. ct_zmesh(2)-ct_zmesh(1)];
  108. Geometry.start = [ct_xmesh(1) ct_ymesh(1) ct_zmesh(1)];
  109. if downsample_flag == true
  110. Geometry.data = resamplegrid(Geometry.data, [downsample_factor downsample_factor 1]);
  111. % Geometry.data = resamplegrid(Geometry.data, 0.5);
  112. % note that ct_xmesh, ct_ymesh, ct_zmesh is already downsampled
  113. Geometry.start(1:2) = Geometry.start(1:2) + Geometry.voxel_size(1:2) / 4;
  114. end
  115. Geometry.rhomw = CT2dens(Geometry.data, 'pinn');
  116. Geometry.rhomw(Geometry.rhomw < 0.0012) = 0.0012;
  117. [Geometry.Smw Geometry.Fmw2] = dens2mstp(Geometry.rhomw);