0

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);
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
StuckInPhDNoMore
  • 2,507
  • 4
  • 41
  • 73
  • 2
    You likely did not set up the tags properly. Do you really need them to start with? why not `imwrite`? Also, you have been long enough in SO to know why a [mcve] is required. – Ander Biguri Jul 12 '19 at 12:22
  • Yes, I have been on SO long enough to know that no matter what you do, people will downvote a valid question and vote for it to close and one will get comments like yours. This is a minimum reproducible example. It shows exactly and specifically what the few lines are trying to achieve, its not my full code. Also YES, I do need all these tags, as per documentation these tags must be set to use tiff object. I knew about ``imwrite`` but wanted to learn tiff as this SO recommends using ``Tiff`` object over ``imwrite``: https://stackoverflow.com/questions/8665825/matlab-how-to-save-tiff-series – StuckInPhDNoMore Jul 12 '19 at 13:19
  • 1
    Just put a valid file for us to test, and I will remove the downvote. Without a file, I can not reproduce the behavior, right now I need access to your computer to test this code. – Ander Biguri Jul 12 '19 at 13:21
  • 4
    You are writing an image with 290 samples per pixel. There are no readers that know what to do with that. TIFF is a very flexible format, you have to look at what your destination application supports, and write the TIFF file in that way. So you need to know: What application do you want to use to open the file? Does it support 3D TIFFs as a single directory? Does it support multi-directory TIFFs? What photometric interpretation values does it support? (“mask” is n uncommon value.) And there are probably more questions you get to when you start reading up on the TIFF format. – Cris Luengo Jul 12 '19 at 13:38
  • 1
    @StuckInPhD if that is the case (you just need some zeros), could you then provide code that I can copy paste (without adding any modifications myself) and run? Such is the definition of a [mcve]. Its not a MRE if it only works on your PC. Copy your code to a different folder and change your paths to test if its a MRE. (Note how the issues seems to be the fact that you have 290 images, which I can not test) – Ander Biguri Jul 12 '19 at 13:45
  • @CrisLuengo Thank you for your comment. I realize I messed up with the tag, ``setTag(t,'SamplesPerPixel',size(tiff_array, 3));`` If I set this to any other value I get the error: ``SamplesPerPixel is 1, but the number of image planes provided was 290`` Thats why i chose the number of images to use here. This is to be read in python but for starters I want it to open in IrfanView, which does support multi-plane tiff files and is the program that gives the error I quoted above – StuckInPhDNoMore Jul 12 '19 at 13:49
  • 1
    In that case you probably want to save it as a series of planes with 1 byte per pixel each. – Cris Luengo Jul 12 '19 at 14:18
  • [tifffile](https://pypi.org/project/tifffile/) can read TIFF with any number of samples per pixel, PlanarConfiguration CONTIG or SEPARATE. – cgohlke Jul 12 '19 at 15:12
  • @cgohlke: Yes, you can always find some software that will read any weird TIFF configuration that you can come up with. OP's TIFF file is not illegal, it is just highly uncommon and therefore not read my most TIFF readers. If the only goal is to transfer data to Python, there are much simpler ways than using an uncommon TIFF file. TIFF files are nice for preserving data long term, and for sharing data more broadly. In these cases, you need to use a TIFF format that readers actually can read. – Cris Luengo Jul 12 '19 at 15:27
  • "I knew about `imwrite` but wanted to learn tiff as this SO recommends using `Tiff` object over `imwrite`" -- The linked SO Q&A recommends `imwrite` (9 upvotes), someone posted an answer saying `Tiff` is recommended, but that has **zero upvotes**, so it's just one person's opinion. – Cris Luengo Jul 12 '19 at 15:29
  • 1
    TIFF files with a larger number of samples per pixels are commonly used in spectral imaging. `GDAL`, `rasterio`, `imread`, `pytiff`, and `Bio-Formats` can also read the file.Besides that, `myfile.tif` is not TIFF compliant as it does not define `ExtraSamples` and `PhotometricInterpretation=4` requires `SamplesPerPixel=1` and `BitsPerSample=1` AFAIK. – cgohlke Jul 12 '19 at 16:39

0 Answers0