How to save unint16 class as tiff image.
17 ビュー (過去 30 日間)
古いコメントを表示
Hello
I have 36 tiff images ( CT scans) :
Imported them and store in a cell array :C
I stacked row slices of each image to generate 3d matrix or 2d cell array : Sinogram
Converted the 2D cell array into uint16 : A
Now Iam looking to save the A (3d matrix) as a tiff image.
However imwrite is not allowing me to do. Kindly requesting for help. Below is my code
% Uploading images
myPath = 'G:\My Drive\Research-Ph.D\Matlab codes\4.23\20220422_5anglestep_001';
fileNames = dir(fullfile(myPath, '*.tif')); % Assigning the output of dir directly into fileNames
% First, define the cell array to have the right size to store all images in a single collection
C = cell(length(fileNames), 1);
I = cell(length(fileNames), 1);
% Now storing each image into a different cell:
for k = 1:length(fileNames)
filename = fileNames(k).name;
C{k, 1} = imread(filename);
I{k} = im2gray(C{k});
end
figure(1)
%% sinogram
sinogram_image= montage(permute(C, [3,2,1]));
sinogram = permute(C, [3,2,1]);
%{
for i=1:size(sinogram, 3)
for k=1:size(sinogram, 1)
sinogram(k,:,i)=C(i,:,k);
end
end
%}
%% Saving each image of the sinogram - Not required
%for k = 1:numel(sinogram)
% imwrite(sinogram{k}, sprintf('s%d.tiff', k), 'compression', 'none');
%end
%%
A = cell2mat(sinogram);
imwrite(A,'resultimage.tif')
%% %% Volshow
%volshow(A)
2 件のコメント
Geoff Hayes
2022 年 4 月 27 日
@Anand Rathnam - please clarify what you mean by imwrite is not allowing me to. If there is an error message, please copy and paste the full message to this question.
回答 (1 件)
Walter Roberson
2022 年 4 月 27 日
See https://www.mathworks.com/matlabcentral/fileexchange/30519-export-image-to-tif-or-tiff-file-of-selected-data-type?s_tid=srchtitle for code that can write to TIFF integer and floating point formats.
If you do not want to use that contribution then see the code outline at https://www.mathworks.com/matlabcentral/answers/7184-how-can-i-write-32-bit-floating-point-tifs-with-nans#comment_15023
3 件のコメント
Walter Roberson
2022 年 4 月 28 日
% Uploading images
myPath = 'G:\My Drive\Research-Ph.D\Matlab codes\4.23\20220422_5anglestep_001';
fileNames = dir(fullfile(myPath, '*.tif')); % Assigning the output of dir directly into fileNames
% First, define the cell array to have the right size to store all images in a single collection
C = cell(length(fileNames), 1);
I = cell(length(fileNames), 1);
% Now storing each image into a different cell:
for k = 1:length(fileNames)
filename = fileNames(k).name;
C{k, 1} = imread(filename);
I{k} = im2gray(C{k});
end
figure(1)
% sinogram
sinogram_image= montage(permute(C, [3,2,1]));
sinogram = permute(C, [3,2,1]);
A= cell2mat(sinogram);
%% Saving A as tiff
filename = 'testtifffp.tiff';
t = Tiff(filename, 'w');
tagstruct.ImageLength = size(A, 1);
tagstruct.ImageWidth = size(A, 2);
tagstruct.Compression = Tiff.Compression.None;
tagstruct.SampleFormat = Tiff.SampleFormat.UInt;
if size(A,3) == 1
tagstruct.Photometric = Tiff.Photometric.MinIsBlack;
else
tagstruct.Photometric = Tiff.Photometric.RGB;
end
tagstruct.BitsPerSample = 16;
tagstruct.SamplesPerPixel = size(A,3);
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
t.setTag(tagstruct);
t.write(A);
t.close();
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!