writeDicom.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. %% Author: Rodrigo de Barros Vimieiro / Aleks Prša
  2. % Date: April, 2018 / May 2022
  3. % rodrigo.vimieiro@gmail.com / alex.prsa@gmail.com
  4. % =========================================================================
  5. %{
  6. % -------------------------------------------------------------------------
  7. % writeDicom(dataRecon3d,infoDicomRecon,filestring,reconmeth,parameter)
  8. % -------------------------------------------------------------------------
  9. % DESCRIPTION:
  10. % This function write a set of Dicom images with respective headers.
  11. %
  12. % INPUT:
  13. % - dataRecon3d = Reconstructed data
  14. % - infoDicomProjs = Dicom header from projections
  15. % - filestring = Path to save
  16. % - reconmeth = Reconstructed method
  17. % - parameter = Parameter of all geometry
  18. %
  19. % ---------------------------------------------------------------------
  20. % Copyright (C) <2018> <Rodrigo de Barros Vimieiro>
  21. %
  22. % This program is free software: you can redistribute it and/or modify
  23. % it under the terms of the GNU General Public License as published by
  24. % the Free Software Foundation, either version 3 of the License, or
  25. % (at your option) any later version.
  26. %
  27. % This program is distributed in the hope that it will be useful,
  28. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. % GNU General Public License for more details.
  31. %
  32. % You should have received a copy of the GNU General Public License
  33. % along with this program. If not, see <http://www.gnu.org/licenses/>.
  34. %}
  35. % =========================================================================
  36. %% Write Dicom images
  37. function writeDicom(dataRecon3d,infoDicomRecon,filestring,reconmeth,parameter)
  38. % Get reconstructed dicom headers
  39. %uiwait(msgbox('Select reconstructed image Dicom file to get headers.','Dicom','Warn'));
  40. %[filename,path] = uigetfile('*.dcm');
  41. %infoDicomRecon = dicominfo([path,filename]);
  42. % Weather is Hologic or GE
  43. dc=0;
  44. if(strcmp(parameter.type,'vct')||strcmp(parameter.type,'hologic'))
  45. dc = -1;
  46. end
  47. % Adjust data to save, based on the recon method
  48. if(strcmp(reconmeth,'BP')||strcmp(reconmeth,'MLEM')||strcmp(reconmeth,'SART')||strcmp(reconmeth,'SIRT'))
  49. for k=1:size(dataRecon3d,2)
  50. dataRecon3d{1,k} = (2^16-1).*mat2gray(dataRecon3d{1,k});
  51. dataRecon3d{1,k} = uint16(dataRecon3d{1,k});
  52. end
  53. else
  54. if(strcmp(reconmeth,'FBP'))
  55. fprintf('\nAdjusting grayscale - windowing ')
  56. for k=1:size(dataRecon3d,2)
  57. dataRecon3d{1,k} = dataRecon3d{1,k} + abs(min(dataRecon3d{1,k}(:)));
  58. dataRecon3d{1,k} = (2^16-1).*mat2gray(dataRecon3d{1,k});
  59. dataRecon3d{1,k} = uint16(dataRecon3d{1,k});
  60. % Adjusting grayscale window - windowing
  61. data = dataRecon3d{1,k};
  62. data = reshape(data,1,numel(data));
  63. data = data ./ 256;
  64. data = fix(data);
  65. nmin = 0;
  66. nmax = 0;
  67. binmin = 0;
  68. binmax = 0;
  69. % Determining lower grayscale limit
  70. fprintf('\n Lower limit')
  71. for bin=1:256
  72. for i=1:numel(data)
  73. if(data(i) == (bin-1))
  74. nmin = nmin + 1;
  75. end
  76. if(nmin > 14739305)
  77. binmin = bin;
  78. break
  79. end
  80. end
  81. if(nmin > 14739305)
  82. binmin = bin;
  83. break
  84. end
  85. end
  86. % Determining upper grayscale limit
  87. fprintf('\n Upper limit')
  88. for bin=1:256
  89. for i=1:numel(data)
  90. if(data(i) == (257-bin))
  91. nmax = nmax + 1;
  92. end
  93. if(nmax > 37331)
  94. binmax = bin;
  95. break
  96. end
  97. end
  98. if(nmax > 37331)
  99. binmax = bin;
  100. break
  101. end
  102. end
  103. wmin = binmin*256; % Lover grayscale limit
  104. wmax = (256-binmax)*256; % Upper grayscale limit
  105. dataRecon3d{1,k}(dataRecon3d{1,k} < wmin) = wmin;
  106. dataRecon3d{1,k}(dataRecon3d{1,k} > wmax) = wmax;
  107. %dataRecon3d{1,k}(dataRecon3d{1,k} < 32325) = 32325;
  108. %dataRecon3d{1,k}(dataRecon3d{1,k} > 58600) = 58600;
  109. dataRecon3d{1,k} = (2^16-1).*mat2gray(dataRecon3d{1,k});
  110. dataRecon3d{1,k} = uint16(dataRecon3d{1,k});
  111. end
  112. else
  113. error('Invalid recon method');
  114. end
  115. end
  116. if(~exist(filestring))
  117. mkdir(filestring)
  118. end
  119. % Write Dicom files in respective folder
  120. fprintf('\nWriting DICOMs ')
  121. fprintf('%3d/%3d', 1, parameter.nz)
  122. for i=1:size(dataRecon3d{1,1},3)
  123. fprintf('\b\b\b\b\b\b\b%3d/%3d', i, parameter.nz)
  124. if(i<10)
  125. fileimg = [filestring,filesep,'0',num2str(i+dc),'.dcm'];
  126. else
  127. fileimg = [filestring,filesep,num2str(i+dc),'.dcm'];
  128. end
  129. dicomwrite(dataRecon3d{1,1}(:,:,i), fileimg, infoDicomRecon(:,1), 'CreateMode', 'copy');
  130. end
  131. end