dicomrt_loadct.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. function [ctcell,ct_xmesh,ct_ymesh,ct_zmesh] = dicomrt_loadct(filename,sorting)
  2. % dicomrt_loadct(filename,sorting)
  3. %
  4. % Read dicom CT for a case using MATLAB native function dicomread. Light Version.
  5. %
  6. % filename contains a list of CT slices to import
  7. %
  8. % CT are stored in a single 3D matrix: case_study
  9. % x-y-z coordinates of the center of the ct-pixel are stored in ct_xmesh, ct_ymesh and ct_zmesh.
  10. %
  11. % NOTE: CT numbers range from -1000 for air to 1000 for bone with that for water set to 0.
  12. % CT numbers normalized in this manner are called Hounfield numbers or units (HU):
  13. %
  14. % HU = ((mu_tissue-mu_water)/mu_water)*1000
  15. %
  16. % Following DICOM RT standard HU = m*(SV)+b where m is RescaleSlope, b RescaleIntercept
  17. % and SV are pixel (stored) values.
  18. %
  19. % Often CT scale is shifted so that HU(water)=1000 (=CToffset) instead of 0.
  20. %
  21. % NOTE: as opposed to dicomrt_loadct ct_xmesh, ct_ymesh and ct_zmesh are vectors and not
  22. % matrices. This allow to run this functions also in "low" memory pcs.
  23. %
  24. % See also dicomrt_loaddose, dicomrt_getPatientPosition
  25. %
  26. % Copyright (C) 2002 Emiliano Spezi (emiliano.spezi@physics.org)
  27. % Sort CT slices first
  28. %
  29. % load ct slices info
  30. % Check number of argument and set-up some parameters and variables
  31. error(nargchk(1,2,nargin))
  32. if exist('sorting')==0
  33. sorting=1; % perform sorting check
  34. end
  35. % Retrieve the number of images
  36. nlines=dicomrt_nASCIIlines(filename);
  37. if nlines>1
  38. disp('Loading CT slice information ...');
  39. [filelist,xlocation,ylocation,zlocation,modifUID,user_request] = dicomrt_loadctlist(filename);
  40. if user_request==1;
  41. return
  42. end
  43. disp('CT slice information loaded.');
  44. if sorting==1
  45. % sort ct slices
  46. disp('Sorting CT slices ...');
  47. [modifZ,user_request]=dicomrt_sortct(filelist,xlocation,ylocation,zlocation,filename);
  48. if user_request==1;
  49. return
  50. end
  51. disp('CT slices sorted.');
  52. else
  53. modifZ=0;
  54. modifUID=0;
  55. end
  56. if modifZ~=0 | modifUID~=0 % a modification to the filelist was made by dicomrt_sortct
  57. filename_sorted=[filename,'.sort.txt'];
  58. else % no modification have been made
  59. filename_sorted=filename;
  60. end
  61. else
  62. disp('Loading one image. Zmesh will have no sense. Slice position can be loaded using dicominfo.');
  63. filename_sorted=filename;
  64. end
  65. % Now get CT images and create 3D Volume
  66. % Set parameters
  67. CToffset=1000;
  68. nct=0;
  69. % Define cell array to store info
  70. case_study_info=cell(1);
  71. %loop until the end-of-file is reached and build 3D CT matrix
  72. disp('Loading ct volume ...');
  73. % Progress bar
  74. h = waitbar(0,['Loading progress:']);
  75. set(h,'Name','dicomrt_loadct: loading CT images');
  76. fid=fopen(filename_sorted);
  77. while (feof(fid)~=1);
  78. clear info_temp temp
  79. nct=nct+1; % counting
  80. nctcheck=nct; % check for eof
  81. ct_file_location{1,nct}=fgetl(fid);
  82. if isnumeric(ct_file_location{1,nct}), nct=nct-1; break, end %end of line reached
  83. temp=dicomread(deblank(ct_file_location{1,nct}));
  84. dictFlg = checkDictUse;
  85. if dictFlg
  86. info_temp=dicominfo(deblank(ct_file_location{1,nct}),'dictionary', 'ES - IPT4.1CompatibleDictionary.mat');
  87. else
  88. info_temp=dicominfo(deblank(ct_file_location{1,nct}));
  89. end
  90. zmesh(nct)=info_temp.ImagePositionPatient(3);
  91. if isfield(info_temp,'RescaleSlope')~=0 | isfield(info_temp,'RescaleIntercept')~=0
  92. temp=double(temp)*info_temp.RescaleSlope+info_temp.RescaleIntercept+CToffset;
  93. else
  94. warning('dicomrt_loadct: no DICOM Rescale data were found. Assuming RescaleSlope = 1, RescaleIntercept = 0 and CToffset = 1000');
  95. temp=double(temp);
  96. end
  97. case_study_info{nct}=info_temp;
  98. case_study(:,:,nct)=uint16(temp);
  99. waitbar(nct/nlines,h);
  100. end
  101. % Make zmesh a column vector
  102. zmesh=dicomrt_makevertical(zmesh);
  103. fclose(fid);
  104. if nctcheck~=nct;
  105. warning('dicomrt_loadct: End of file was prematurely reached. Check the expected dimensions of your data. It may be OK to continue');
  106. end
  107. [PatientPositionCODE]=dicomrt_getPatientPosition(info_temp);
  108. % PatientOrientation ImageOrientationPatient
  109. %
  110. % HFS (1,0,0) (0,1,0)
  111. % FFS (1,0,0) (0,1,0)
  112. % HFP (-1,0,0) (0,-1,0)
  113. % FFP (-1,0,0) (0,-1,0)
  114. %
  115. %
  116. if PatientPositionCODE == 1 | PatientPositionCODE == 2
  117. min_x=info_temp.ImagePositionPatient(1);
  118. pixel_spacing_x=info_temp.PixelSpacing(1);
  119. min_y=info_temp.ImagePositionPatient(2);
  120. pixel_spacing_y=info_temp.PixelSpacing(2);
  121. [xmesh] = dicomrt_create1dmesh(min_x,pixel_spacing_x,size(temp,2),0);
  122. [ymesh] = dicomrt_create1dmesh(min_y,pixel_spacing_y,size(temp,1),0);
  123. else
  124. max_x=info_temp.ImagePositionPatient(1);
  125. pixel_spacing_x=info_temp.PixelSpacing(1);
  126. max_y=info_temp.ImagePositionPatient(2);
  127. pixel_spacing_y=info_temp.PixelSpacing(2);
  128. [xmesh] = dicomrt_create1dmesh(max_x,pixel_spacing_x,size(temp,1),1);
  129. [ymesh] = dicomrt_create1dmesh(max_y,pixel_spacing_y,size(temp,2),1);
  130. end
  131. ct_zmesh=dicomrt_mmdigit(zmesh*0.1,7);
  132. ct_ymesh=dicomrt_mmdigit(ymesh*0.1,7);
  133. ct_xmesh=dicomrt_mmdigit(xmesh*0.1,7);
  134. disp('Loading complete ...');
  135. % 3D CT matrix and mesh matrix imported
  136. %
  137. % Some CT images info have private tags or fragmented info.
  138. % If so, go into manual mode.
  139. %
  140. % Check support for current Patient Position
  141. if PatientPositionCODE == 1 % supported Patient Position: HFS
  142. disp('dicomrt_loadct: Patient Position is Head First Supine (HFS).');
  143. elseif PatientPositionCODE == 2 % supported Patient Position: FFS
  144. disp('dicomrt_loadct: Patient Position is Feet First Supine (FFS).');
  145. elseif PatientPositionCODE == 3 % unsupported Patient Position: HFP
  146. disp('dicomrt_loadct: Patient Position is Head First Prone (HFP).');
  147. elseif PatientPositionCODE == 4 % unsupported Patient Position: FFP
  148. disp('dicomrt_loadct: Patient Position is Feet First Prone (FFP).');
  149. else
  150. warning('Unable to determine Patient Position');
  151. PatientPosition=input('dicomrt_loadct: Please specify Patient Position: HFS(default),FFS,HFP,FFP: ','s');
  152. if isempty(PatientPosition)==1
  153. PatientPosition='HFS';
  154. end
  155. if strcmpi(PatientPosition, 'HFS')
  156. PatientPositionCODE = 1;
  157. elseif strcmpi(PatientPosition, 'FFS')
  158. PatientPositionCODE = 2;
  159. elseif strcmpi(PatientPosition, 'HFP')
  160. PatientPositionCODE = 3;
  161. elseif strcmpi(PatientPosition, 'FFP')
  162. PatientPositionCODE = 4;
  163. end
  164. end
  165. % Create cell array
  166. ctcell=cell(3,1);
  167. ctcell{1,1}=case_study_info;
  168. ctcell{2,1}=case_study;
  169. ctcell{3,1}=[];
  170. % Close progress bar
  171. close(h);
  172. pack