dicomrt_loadct.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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; % CT offset!
  68. % CToffset=0; % CT offset!
  69. nct=0;
  70. % Define cell array to store info
  71. case_study_info=cell(1);
  72. %loop until the end-of-file is reached and build 3D CT matrix
  73. disp('Loading ct volume ...');
  74. % Progress bar
  75. h = waitbar(0,['Loading progress:']);
  76. set(h,'Name','dicomrt_loadct: loading CT images');
  77. fid=fopen(filename_sorted);
  78. while (feof(fid)~=1);
  79. clear info_temp temp
  80. nct=nct+1; % counting
  81. nctcheck=nct; % check for eof
  82. ct_file_location{1,nct}=fgetl(fid);
  83. if isnumeric(ct_file_location{1,nct}), nct=nct-1; break, end %end of line reached
  84. temp=dicomread(deblank(ct_file_location{1,nct}));
  85. dictFlg = checkDictUse;
  86. if dictFlg
  87. info_temp=dicominfo(deblank(ct_file_location{1,nct}),'dictionary', 'ES - IPT4.1CompatibleDictionary.mat');
  88. else
  89. info_temp=dicominfo(deblank(ct_file_location{1,nct}));
  90. end
  91. zmesh(nct)=info_temp.ImagePositionPatient(3);
  92. if isfield(info_temp,'RescaleSlope')~=0 | isfield(info_temp,'RescaleIntercept')~=0
  93. temp=double(temp)*info_temp.RescaleSlope+info_temp.RescaleIntercept+CToffset;
  94. else
  95. warning('dicomrt_loadct: no DICOM Rescale data were found. Assuming RescaleSlope = 1, RescaleIntercept = 0 and CToffset = 1000');
  96. temp=double(temp);
  97. end
  98. case_study_info{nct}=info_temp;
  99. case_study(:,:,nct)=uint16(temp);
  100. waitbar(nct/nlines,h);
  101. end
  102. % Make zmesh a column vector
  103. zmesh=dicomrt_makevertical(zmesh);
  104. fclose(fid);
  105. if nctcheck~=nct;
  106. warning('dicomrt_loadct: End of file was prematurely reached. Check the expected dimensions of your data. It may be OK to continue');
  107. end
  108. [PatientPositionCODE]=dicomrt_getPatientPosition(info_temp);
  109. % PatientOrientation ImageOrientationPatient
  110. %
  111. % HFS (1,0,0) (0,1,0)
  112. % FFS (1,0,0) (0,1,0)
  113. % HFP (-1,0,0) (0,-1,0)
  114. % FFP (-1,0,0) (0,-1,0)
  115. %
  116. %
  117. if PatientPositionCODE == 1 | PatientPositionCODE == 2
  118. min_x=info_temp.ImagePositionPatient(1);
  119. pixel_spacing_x=info_temp.PixelSpacing(1);
  120. min_y=info_temp.ImagePositionPatient(2);
  121. pixel_spacing_y=info_temp.PixelSpacing(2);
  122. [xmesh] = dicomrt_create1dmesh(min_x,pixel_spacing_x,size(temp,2),0);
  123. [ymesh] = dicomrt_create1dmesh(min_y,pixel_spacing_y,size(temp,1),0);
  124. else
  125. max_x=info_temp.ImagePositionPatient(1);
  126. pixel_spacing_x=info_temp.PixelSpacing(1);
  127. max_y=info_temp.ImagePositionPatient(2);
  128. pixel_spacing_y=info_temp.PixelSpacing(2);
  129. [xmesh] = dicomrt_create1dmesh(max_x,pixel_spacing_x,size(temp,1),1);
  130. [ymesh] = dicomrt_create1dmesh(max_y,pixel_spacing_y,size(temp,2),1);
  131. end
  132. ct_zmesh=dicomrt_mmdigit(zmesh*0.1,7);
  133. ct_ymesh=dicomrt_mmdigit(ymesh*0.1,7);
  134. ct_xmesh=dicomrt_mmdigit(xmesh*0.1,7);
  135. disp('Loading complete ...');
  136. % 3D CT matrix and mesh matrix imported
  137. %
  138. % Some CT images info have private tags or fragmented info.
  139. % If so, go into manual mode.
  140. %
  141. % Check support for current Patient Position
  142. if PatientPositionCODE == 1 % supported Patient Position: HFS
  143. disp('dicomrt_loadct: Patient Position is Head First Supine (HFS).');
  144. elseif PatientPositionCODE == 2 % supported Patient Position: FFS
  145. disp('dicomrt_loadct: Patient Position is Feet First Supine (FFS).');
  146. elseif PatientPositionCODE == 3 % unsupported Patient Position: HFP
  147. disp('dicomrt_loadct: Patient Position is Head First Prone (HFP).');
  148. elseif PatientPositionCODE == 4 % unsupported Patient Position: FFP
  149. disp('dicomrt_loadct: Patient Position is Feet First Prone (FFP).');
  150. else
  151. warning('Unable to determine Patient Position');
  152. PatientPosition=input('dicomrt_loadct: Please specify Patient Position: HFS(default),FFS,HFP,FFP: ','s');
  153. if isempty(PatientPosition)==1
  154. PatientPosition='HFS';
  155. end
  156. if strcmpi(PatientPosition, 'HFS')
  157. PatientPositionCODE = 1;
  158. elseif strcmpi(PatientPosition, 'FFS')
  159. PatientPositionCODE = 2;
  160. elseif strcmpi(PatientPosition, 'HFP')
  161. PatientPositionCODE = 3;
  162. elseif strcmpi(PatientPosition, 'FFP')
  163. PatientPositionCODE = 4;
  164. end
  165. end
  166. % Create cell array
  167. ctcell=cell(3,1);
  168. ctcell{1,1}=case_study_info;
  169. ctcell{2,1}=case_study;
  170. ctcell{3,1}=[];
  171. % Close progress bar
  172. close(h);
  173. pack