matlabPatientResample.asv 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. % Stephen R Bowen April 2009
  2. %
  3. % Crops and resamples the planning CT data set and associated ROI masks,
  4. % typically imported from Pinnacle using pinn2matlab.m. CT data is
  5. % linearly resampled and ROI masks are nearest-neighbor resampled so as to
  6. % preserve binary values. Bounding box coordinates are specified in the
  7. % crop ranges user inputs [xmin xmax], [ymin ymax], [zmin zmax]. Be aware
  8. % of half-voxel shifts in the boundary box used by Amira and remain
  9. % consistent in defining your crop ranges.
  10. %%%%% Start of user supplied inputs %%%%%
  11. %
  12. infile = '../plan/HN003/HN003.mat';
  13. outfile = '../plan/HN003/HN003final.mat';
  14. % downsamples patient Geometry files, including their tissue masks
  15. % crop ranges (if empty, no cropping is done)
  16. xCrop = [-10.695 12.289];
  17. yCrop = [-66.202 -39.682];
  18. zCrop = [-6.801 8.669];
  19. % resolution after resampling (if empty, resolution is unchanged
  20. dxNew = 0.442;
  21. dyNew = 0.442;
  22. dzNew = 0.442;
  23. x0New = -10.695;
  24. y0New = -66.202;
  25. z0New = -6.801;
  26. %%%%% End of user supplied inputs %%%%%
  27. load(infile);
  28. % get dimensions of original data
  29. [M,N,Q] = size(Geometry.data);
  30. dx = Geometry.voxel_size(1);
  31. dy = Geometry.voxel_size(2);
  32. dz = Geometry.voxel_size(3);
  33. x0 = Geometry.start(1);
  34. y0 = Geometry.start(2);
  35. z0 = Geometry.start(3);
  36. % x,y,z coordinates of original data
  37. x = single([0:M-1]*dx + x0);
  38. y = single([0:N-1]*dy + y0);
  39. z = single([0:Q-1]*dz + z0);
  40. % If no cropping is desired, the resampling will be performed such that the
  41. % bounding planes of the 3D grid data are unchanged.
  42. % bounding plane coordinates:
  43. xp1 = x(1) - dx/2;
  44. xpM = x(end) + dx/2;
  45. yp1 = y(1) - dy/2;
  46. ypN = y(end) + dy/2;
  47. zp1 = z(1) - dz/2;
  48. zpQ = z(end) + dz/2;
  49. % create the new coordinate system
  50. if ~isempty(xCrop)
  51. xp1 = xCrop(1);
  52. xpM = xCrop(2);
  53. end
  54. if ~isempty(yCrop)
  55. yp1 = yCrop(1);
  56. ypN = yCrop(2);
  57. end
  58. if ~isempty(zCrop)
  59. zp1 = zCrop(1);
  60. zpQ = zCrop(2);
  61. end
  62. if isempty(dxNew)
  63. dxNew = dx;
  64. end
  65. if isempty(dyNew)
  66. dyNew = dy;
  67. end
  68. if isempty(dzNew)
  69. dzNew = dz;
  70. end
  71. % create new coordinate systems
  72. if isempty(x0New)
  73. xNew = single(xp1+dxNew/2:dxNew:xpM-dxNew/2);
  74. else
  75. xNew = x0New:dxNew:xpM;
  76. xNew = xNew(xNew >= xp1 & xNew <= xpM);
  77. end
  78. if isempty(y0New)
  79. yNew = single(yp1+dyNew/2:dyNew:ypN-dyNew/2);
  80. else
  81. yNew = y0New:dyNew:ypN;
  82. yNew = yNew(yNew >= yp1 & yNew <= ypN);
  83. end
  84. if isempty(z0New)
  85. zNew = single(zp1+dzNew/2:dzNew:zpQ-dzNew/2);
  86. else
  87. zNew = z0New:dzNew:zpQ;
  88. zNew = zNew(zNew >= zp1 & zNew <= zpQ);
  89. end
  90. clear Geometry_ds;
  91. Geometry_ds.voxel_size = [dxNew dyNew dzNew];
  92. Geometry_ds.start = [xNew(1) yNew(1) zNew(1)];
  93. fprintf('Resampling CT data:\n');
  94. Geometry_ds.data = gridResample3D(x,y,z,Geometry.data,xNew,yNew,zNew,'linear');
  95. fprintf('Finished resampling CT data.\n');
  96. Geometry_ds.ROIS = Geometry.ROIS;
  97. % resample the ROIs
  98. for k=1:length(Geometry.ROIS)
  99. % expand the original ROI mask
  100. roimask = zeros(M,N,Q,'int8'); % create the original mask
  101. roimask(Geometry.ROIS{k}.ind) = 1;
  102. % resample the ROI mask
  103. fprintf('Resampling ROI mask: %s',Geometry.ROIS{k}.name);
  104. roimaskNew = gridResample3D(x,y,z,roimask,xNew,yNew,zNew,'nearest');
  105. fprintf(' Finished resampling %s.\n',Geometry.ROIS{k}.name);
  106. % save the new roi mask data
  107. Geometry_ds.ROIS{k}.ind = int32(find(roimaskNew));
  108. Geometry_ds.ROIS{k}.dim = size(Geometry_ds.data);
  109. Geometry_ds.ROIS{k}.pixdim = [dxNew dyNew dzNew];
  110. Geometry_ds.ROIS{k}.start = [xNew(1) yNew(1) zNew(1)];
  111. clear roimask roimaskNew;
  112. end
  113. if isfield(Geometry,'Trial');
  114. Geometry_ds.Trial = Geometry.Trial;
  115. end
  116. Geometry = Geometry_ds;
  117. save(outfile,'Geometry');