matlabPatientResample.asv 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. % Downsamples a structure of patient data as well as all of the ROI masks
  2. % that reside in the structure. The structures must be the type created
  3. % with roireadin from Pinnacle patient data files.
  4. infile = 'HN003.mat';
  5. outfile = 'HN003final.mat';
  6. % downsamples patient Geometry files, including their tissue masks
  7. % crop ranges (if empty, no cropping is done)
  8. xCrop = [-8.718 13.382];
  9. yCrop = [-66.635 -37.463];
  10. zCrop = [-17.678 9.726];
  11. % resolution after resampling (if empty, resolution is unchanged
  12. dxNew = 0.442;
  13. dyNew = 0.442;
  14. dzNew = 0.442;
  15. x0New = -8.718;
  16. y0New = -66.635;
  17. z0New = -17.678;
  18. load(infile);
  19. % get dimensions of original data
  20. [M,N,Q] = size(Geometry.data);
  21. dx = Geometry.voxel_size(1);
  22. dy = Geometry.voxel_size(2);
  23. dz = Geometry.voxel_size(3);
  24. x0 = Geometry.start(1);
  25. y0 = Geometry.start(2);
  26. z0 = Geometry.start(3);
  27. % x,y,z coordinates of original data
  28. x = single([0:M-1]*dx + x0);
  29. y = single([0:N-1]*dy + y0);
  30. z = single([0:Q-1]*dz + z0);
  31. % If no cropping is desired, the resampling will be performed such that the
  32. % bounding planes of the 3D grid data are unchanged.
  33. % bounding plane coordinates:
  34. xp1 = x(1) - dx/2;
  35. xpM = x(end) + dx/2;
  36. yp1 = y(1) - dy/2;
  37. ypN = y(end) + dy/2;
  38. zp1 = z(1) - dz/2;
  39. zpQ = z(end) + dz/2;
  40. % create the new coordinate system
  41. if ~isempty(xCrop)
  42. xp1 = xCrop(1);
  43. xpM = xCrop(2);
  44. end
  45. if ~isempty(yCrop)
  46. yp1 = yCrop(1);
  47. ypN = yCrop(2);
  48. end
  49. if ~isempty(zCrop)
  50. zp1 = zCrop(1);
  51. zpQ = zCrop(2);
  52. end
  53. if isempty(dxNew)
  54. dxNew = dx;
  55. end
  56. if isempty(dyNew)
  57. dyNew = dy;
  58. end
  59. if isempty(dzNew)
  60. dzNew = dz;
  61. end
  62. % create new coordinate systems
  63. if isempty(x0New)
  64. xNew = single(xp1+dxNew/2:dxNew:xpM-dxNew/2);
  65. else
  66. xNew = x0New:dxNew:xpM;
  67. xNew = xNew(xNew >= xp1 & xNew <= xpM);
  68. end
  69. if isempty(y0New)
  70. yNew = single(yp1+dyNew/2:dyNew:ypN-dyNew/2);
  71. else
  72. yNew = y0New:dyNew:ypN;
  73. yNew = yNew(yNew >= yp1 & yNew <= ypN);
  74. end
  75. if isempty(z0New)
  76. zNew = single(zp1+dzNew/2:dzNew:zpQ-dzNew/2);
  77. else
  78. zNew = z0New:dzNew:zpQ;
  79. zNew = zNew(zNew >= zp1 & zNew <= zpQ);
  80. end
  81. clear Geometry_ds;
  82. Geometry_ds.voxel_size = [dxNew dyNew dzNew];
  83. Geometry_ds.start = [xNew(1) yNew(1) zNew(1)];
  84. fprintf('Resampling CT data:\n');
  85. Geometry_ds.data = gridResample3D(x,y,z,Geometry.data,xNew,yNew,zNew,'linear');
  86. fprintf('Finished resampling CT data.\n');
  87. Geometry_ds.ROIS = Geometry.ROIS;
  88. % resample the ROIs
  89. for k=1:length(Geometry.ROIS)
  90. % expand the original ROI mask
  91. roimask = zeros(M,N,Q,'int8'); % create the original mask
  92. roimask(Geometry.ROIS{k}.ind) = 1;
  93. % resample the ROI mask
  94. fprintf('Resampling ROI mask: %s',Geometry.ROIS{k}.name);
  95. roimaskNew = gridResample3D(x,y,z,roimask,xNew,yNew,zNew,'nearest');
  96. fprintf(' Finished resampling %s.\n',Geometry.ROIS{k}.name);
  97. % save the new roi mask data
  98. Geometry_ds.ROIS{k}.ind = int32(find(roimaskNew));
  99. Geometry_ds.ROIS{k}.dim = size(Geometry_ds.data);
  100. Geometry_ds.ROIS{k}.pixdim = [dxNew dyNew dzNew];
  101. Geometry_ds.ROIS{k}.start = [xNew(1) yNew(1) zNew(1)];
  102. clear roimask roimaskNew;
  103. end
  104. if isfield(Geometry,'Trial');
  105. Geometry_ds.Trial = Geometry.Trial;
  106. end
  107. Geometry = Geometry_ds;
  108. save(outfile,'Geometry');