Saving large Tiff in MATLAB leads to corrupt file

22 次查看(过去 30 天)
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 个评论
DGM
DGM 2021-5-25
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.
Andrew Robbins
Andrew Robbins 2021-5-25
Tried it, but it errors when I call read. I think to read it otherwise I'd have to code up my own reader, so probably not worth it.
Maybe I will look for alternative file formats to use (need ones where I can save some complex metadata too).
Thank you

请先登录,再进行评论。

采纳的回答

Andrew Robbins
Andrew Robbins 2021-6-2
For anyone having this problem, I found another answer that was relevant:
This is not an intrinsic problem with the Tiff format, but rather some internal bug in either matlab or the library. It can be solved by using tiles or strips.
For example, adding:
tags.RowsPerStrip = 32;
or
tags.TileLength = 256;
tags.TileWidth = 256;
Keep in mind that choices here will affect save speed, though I found that it could actually speed up the process for the right strip or tile sizes.

更多回答(1 个)

Jan
Jan 2021-5-25
I can reproduce the problem with Matlab 2018b. imwrite(mat, 'test.tif') is not successful also.

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by