How to save unint16 class as tiff image.
18 次查看(过去 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!