Saving large Tiff in MATLAB leads to corrupt file
20 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Andrew Robbins
le 22 Mai 2021
Réponse apportée : Andrew Robbins
le 2 Juin 2021
I am saving an image matrix to a Tiff file, and have not had any trouble until recently. Lately, when I save large files, the files are unreadable both in MATLAB and other image software.
In my case, I'm saving a color image approximately 25,000 x 25,000 pixels, with a 4th layer to save alpha information. The saved file is 880 Mb with compression, but attempts to read it give the following error:
% Error using imread (line 440)
% Unable to read TIFF file "test1.tif". File is corrupt or
% image does not contain any readable strips.
Note I can still call imfinfo on the saved file and that works fine.
The problem goes away for smaller images.
I have made a minimum example code to demonstrate the problem. This example generates a file that's ~2.8 Gb, so keep that in mind before running it.
I'm running MATLAB R2020b on Windows 10.
Is there a way to save the file that works? Should I use strips or tiles? I thought Tiffs could save up to 4Gb files.
%% Save large Tiff (will be ~2.8 Gb)
% Create image "data"
L = 25000;
mat = uint8(randi(256, [L L 3]) - 1);
mat(:, :, 4) = uint8(randi(2, [L L]) - 1); % Add alpha layer
% Create tags
tags = struct;
tags.Photometric = Tiff.Photometric.RGB;
tags.BitsPerSample = 8;
tags.ImageLength = size(mat, 1);
tags.ImageWidth = size(mat, 2);
tags.SamplesPerPixel = size(mat, 3);
tags.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
tags.Compression = Tiff.Compression.LZW;
tags.ExtraSamples = Tiff.ExtraSamples.Unspecified;
% Save Tiff
Tobj = Tiff('test1.tif', 'w');
setTag(Tobj, tags) % add tags
write(Tobj, mat) % add data
close(Tobj) % close
im1 = imread('test1.tif'); % errors :(
% Error:
% Error using imread (line 440)
% Unable to read TIFF file "test1.tif". File is corrupt or
% image does not contain any readable strips.
% Clean up
clear mat im1
%% Repeat with smaller file (will be ~400 Mb)
L = 10000;
mat = uint8(randi(256, [L L 3]) - 1);
mat(:, :, 4) = uint8(randi(2, [L L]) - 1);
% Create tags
tags = struct;
tags.Photometric = Tiff.Photometric.RGB;
tags.BitsPerSample = 8;
tags.ImageLength = size(mat, 1);
tags.ImageWidth = size(mat, 2);
tags.SamplesPerPixel = size(mat, 3);
tags.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
tags.Compression = Tiff.Compression.LZW;
tags.ExtraSamples = Tiff.ExtraSamples.Unspecified;
Tobj = Tiff('test2.tif', 'w');
setTag(Tobj, tags) % add tags
write(Tobj, mat) % add data
close(Tobj) % close
im2 = imread('test2.tif'); % works fine!
% Clean up
clear mat im2
6 commentaires
DGM
le 25 Mai 2021
That may be part of it. I don't have access to a Windows machine that's even remotely close (R2012a on Win7), so I can't really verify.
I really never use TIFF objects like this, but is it possible to read the image without imread?
Tobj2 = Tiff('test1.tif', 'r');
mat2 = read(Tobj2);
close(Tobj2) % close
I haven't gone to test this on the other computer with the big file, but maybe it's worth a shot. Then again, for all I know, it might be using the same underlying helper files/library.
Réponse acceptée
Plus de réponses (1)
Jan
le 25 Mai 2021
I can reproduce the problem with Matlab 2018b. imwrite(mat, 'test.tif') is not successful also.
0 commentaires
Voir également
Catégories
En savoir plus sur Data Import and Analysis dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!