Tiff file corrupted when using Imwrite
7 次查看(过去 30 天)
显示 更早的评论
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 个评论
Jan
2022-7-12
"readCompressed16bitImage" from the error message sounds strange for an uncompressed image.
回答(1 个)
Siraj
2023-9-28
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!