manipulating multipage TIFF file

15 views (last 30 days)
Raphael
Raphael on 29 Aug 2023
Commented: Raphael on 2 Apr 2024 at 15:04
I have a geo-refenreced TIFF file with 14 pages corresponding to 14 wavelengths. I want to normalize the image at each wavelength and create a new TIFF file with the normalized images. So far no problem. My problem is that the first page in my original TIFF includes a field called "GPSInfo" that I need to pass along to the new TIFF file. Imwrite does not seem to support the inclusion of such information in a TIFF file. Any suggestions?
Thanks

Answers (1)

Dheeraj
Dheeraj on 6 Sep 2023
Hi,
Writing custom metadata like "GPSInfo" you can use a combination of functions from the MATLAB File Exchange and the built-in functions as "imwrite" does not support the inclusion of such information in a TIFF file.
  1. Use the "imread" function to read the original TIFF file and extract the first page's "GPSInfo" metadata using a library like the "Tiff" class.
  2. Apply the necessary normalization
  3. Use the imwrite function to create a new TIFF file for the normalized images.
  4. After writing each normalized image to the new TIFF file, use a custom function to add the "GPSInfo" metadata to the first page of the new TIFF file.
for i = 1:14 %Iterating through all pages
% Create a new file with normalised images, let it be newNormalisedPage
% Add "GPSInfo" metadata to the first page of the new TIFF file
if i == 1
new_tiff = Tiff( newNormalisedPage , 'r+');
new_tiff.setTag('GPSInfo', gps_info);
new_tiff.close();
end
end
  1 Comment
Raphael
Raphael on 2 Apr 2024 at 15:04
I would like to follow-up on this as this answer does not seem to work for me. I have an image with 14 bands which I write to a tif file as shown below.
t2w=uint16(rand(201,207,14)); % dummy image in same format as actual one
[r,c,w]=size(t2w);
t = Tiff('myrandfile.tif','w');
setTag(t,'Photometric',1)
setTag(t,'PlanarConfiguration',1)
setTag(t,'ImageLength',r)
setTag(t,'ImageWidth',c)
setTag(t,'BitsPerSample',16);
setTag(t,'Compression',1);
setTag(t,'SamplesPerPixel',14);
setTag(t,"ExtraSamples",Tiff.ExtraSamples.Unspecified)
write(t,t2w)
Warning: Sum of Photometric color channels and ExtraSamples does not match the value specified in SamplesPerPixel.
Writing with this configuration will error in a future release. To correct the configuration, define the non-color channels as ExtraSamples.
close(t)
(BTW, can somone explain how to get rid of this warining?). Despite this warning message the tiff file is created correctly for my needs.
When I try to add a new tag as suggested in the answer above I get an error message:
new_tiff=Tiff('myrandfile.tif','r+');
Warning: TIFF library warning - 'TIFFReadDirectory: Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples..'
new_tiff.setTag('GPSInfo','MyInfo');
Error using Tiff/setTagInFile (line 2559)
'GPSInfo' is not a recognized tag name.

Error in Tiff/setTag (line 1412)
obj.setTagInFile(varargin{:});

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!