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
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.
Anand Ra
Anand Ra 2022-4-28
@Geoff Hayes Sorry should have included that. Below was the error I received:
Error using writetif (line 40)
Writing TIFFs with 36 components is not supported with IMWRITE.
Use Tiff instead. Type "help Tiff" for more information.
Error in imwrite (line 566)
feval(fmt_s.write, data, map, filename, paramPairs{:});
Error in fullprg_2 (line 42)
imwrite(A,'resultimage.tif')

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2022-4-27
  3 个评论
Anand Ra
Anand Ra 2022-4-28
@Walter Roberson Also wanted to update you that I receive the following warning when I used the file exchange. Btw, the saved tif cannot be opened in normal windows photo viewer?
Warning: Sum of Photometric color channels and ExtraSamples does
not match the value specified in SamplesPerPixel.
Writing with this configuration will error in a future release. To
correct the configuration, define the non-color channels as
ExtraSamples.
> In Tiff/writeAllStrips (line 1979)
In Tiff/write (line 1486)
In imwrite2tif (line 154)
In fullprg_2 (line 53)
>>
Walter Roberson
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!

Translated by