Im trying to save an image sequence as a Tiff file to be later used in another program. I read each image in, applied color thresholding to get the mask, and then append that to my 3d array that I initialized earlier.
I read up on the Tiff class needed to write Tiff images and I think I set all the tags properly, but although the file gets created, I cannot open it in an image viewer. I get the error:
Invalid or Unsupported Tif file
There are 290 images in folder, so tiff file will have 290 planes. Below is how I am saving the tiff
file:
clear;
path = 'D:\complete stack';
img_list = dir(fullfile(path, '*.tif'));
img = imread(fullfile(img_list(1).folder, img_list(1).name));
tiff_array = zeros(size(img, 1), size(img, 2), numel(img_list), 'uint8');
clear img;
for i = 1:numel(img_list)
img = imread(fullfile(img_list(i).folder, img_list(i).name));
[bw, ~] = createMask(img);
tiff_array(:,:,i) = bw;
end
t = Tiff('myfile.tif', 'w');
setTag(t,'Photometric',Tiff.Photometric.Mask);
setTag(t, 'Compression', Tiff.Compression.None);
setTag(t,'BitsPerSample',8);
setTag(t,'SamplesPerPixel',size(tiff_array, 3));
setTag(t,'ImageLength',size(tiff_array, 1));
setTag(t,'ImageWidth',size(tiff_array, 2));
setTag(t,'PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
write(t, tiff_array);
close(t);
thanks
edit:
A more reproducible, more minimal example:
clear;
img = ones(750, 750, 'uint8');
tiff_array = zeros(size(img, 1), size(img, 2), 290, 'uint8');
clear img;
for i = 1:290
bw = ones(750, 750, 'uint8');
% [bw, ~] = createMask(img);
tiff_array(:,:,i) = bw;
end
t = Tiff('myfile.tif', 'w');
setTag(t,'Photometric',Tiff.Photometric.Mask);
setTag(t, 'Compression', Tiff.Compression.None);
setTag(t,'BitsPerSample',8);
setTag(t,'SamplesPerPixel',size(tiff_array, 3));
setTag(t,'ImageLength',size(tiff_array, 1));
setTag(t,'ImageWidth',size(tiff_array, 2));
setTag(t,'PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
write(t, tiff_array);
close(t);