Tiff file corrupted when using Imwrite

7 views (last 30 days)
Sébastien MAILFERT
Sébastien MAILFERT on 12 Jul 2022
Answered: Siraj on 28 Sep 2023
Dear all,
I'm using for a while the imwrite function to create, save and append data to a tiff file:
...
ind_rand = randperm(nb_frames) ;
for f = 1:nb_frames
if(f==1)
imwrite(x_UINT16,name_stk_file,'WriteMode', 'overwrite','Compression','none');
else
imwrite(x_UINT16,name_stk_file,'WriteMode', 'append','Compression','none');
end%if
end
The file is created but when I try to open it with ImageJ for example, the file is corrupted even if its size is correct.
When I open it and display it with Matlab (fopen -> imshow), the file is OK.
Could you help me please?
There is no Matlab error, just the file can not be opened and ImageJ displays this error:
(Fiji Is Just) ImageJ 2.3.0/1.53q; Java 1.8.0_322 [64-bit]; Windows 10 10.0; 596MB of 98096MB (<1%)
java.lang.NegativeArraySizeException
at ij.io.ImageReader.readCompressed16bitImage(ImageReader.java:164)
at ij.io.ImageReader.read16bitImage(ImageReader.java:105)
at ij.io.ImageReader.readPixels(ImageReader.java:785)
at ij.io.FileOpener.readPixels(FileOpener.java:587)
at ij.io.FileOpener.open(FileOpener.java:86)
at ij.io.FileOpener.openImage(FileOpener.java:53)
at ij.io.Opener.openTiff2(Opener.java:1118)
at ij.io.Opener.openTiff(Opener.java:917)
at ij.io.Opener.openImage(Opener.java:335)
at ij.io.Opener.openImage(Opener.java:241)
at ij.io.Opener.open(Opener.java:104)
at ij.io.Opener.openAndAddToRecent(Opener.java:310)
at ij.plugin.DragAndDrop.openFile(DragAndDrop.java:194)
at ij.plugin.DragAndDrop.run(DragAndDrop.java:160)
at java.lang.Thread.run(Thread.java:750)
  4 Comments
Sébastien MAILFERT
Sébastien MAILFERT on 12 Jul 2022
That's interesting but the file has a size of 200 Mo and I created other files with a size > 4Go that can be opened by ImageJ.
Jan
Jan on 12 Jul 2022
"readCompressed16bitImage" from the error message sounds strange for an uncompressed image.

Sign in to comment.

Answers (1)

Siraj
Siraj on 28 Sep 2023
Hi!
I understand that you've created a TIFF file with multiple images using MATLAB, and it opens fine within MATLAB. However, when you try to open it in "ImageJ," it doesn't work.
The error message indicates that the TIFF file is of a large size, and a big TIFF file is not the same as a normal TIFF file. Not all software readers are optimized for reading large TIFF files. MATLAB, on the other hand, can handle both regular and big TIFF files seamlessly.
You may want to consider this potential solution. First, try creating a TIFF object in MATLAB using the "Tiff" function, and make sure to set the access type appropriately as either "w8" or "a." You can refer to this link to learn more about the "Tiff" function and how to specify the file access type.
Next, append your images to this object. When saving a large TIFF file, consider adjusting the default "RowsPerStrip" value to a smaller setting. This may help address the issue you're encountering when opening the file in other software.
You can refer to the provided code snippet to see how to create and write TIFF objects while also configuring and setting their associated tags.
% Prompt the user to select a folder containing PNG files
ImFolder = "Images";
% List all PNG files in the selected directory
pngFiles = dir(fullfile(ImFolder, '*.png'));
% Create a TIFF file named "test.tif" and open it for writing
t = Tiff("test.tif", "w8");
% Read the first PNG image to get its size and metadata
image = imread(fullfile(ImFolder, pngFiles(1).name));
% Loop through each PNG file in the folder
for i = 1:length(pngFiles)
% Read the current PNG image
image = imread(fullfile(ImFolder, pngFiles(i).name));
% Set TIFF tags for the current image
setTag(t,'Photometric',Tiff.Photometric.RGB);
setTag(t,'PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
setTag(t,'BitsPerSample',8);
setTag(t,'SamplesPerPixel',3);
setTag(t,'ImageLength',size(image ,1));
setTag(t,'ImageWidth',size(image ,2));
% Write the current image data to the TIFF file
write(t, image);
% Create a new directory (frame) in the TIFF file for the next image
writeDirectory(t);
end
% Close the TIFF file to save it
close(t);
Hope this helps.

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!