How to save unint16 class as tiff image.

26 views (last 30 days)
Anand Ra
Anand Ra on 27 Apr 2022
Commented: Walter Roberson on 28 Apr 2022
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 Comments
Geoff Hayes
Geoff Hayes on 27 Apr 2022
@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 on 28 Apr 2022
@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')

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 27 Apr 2022
  3 Comments
Anand Ra
Anand Ra on 28 Apr 2022
@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 on 28 Apr 2022
% 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();

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!