Is there a way to write a GeoTiff file with Color and Alpha Channel Data using geotiffwrite?

조회 수: 38 (최근 30일)
Is there a way to write a GeoTiff file with Color and Alpha Channel Data using geotiffwrite? I tried defining a TiffTag for "ExtraSamples" as shown below, but I got an warning message. The image (img) is a n x m x 4 RGB image where the 4 index on dimension 3 is the alpha channel.
geotiffwrite('img.tif', img, R, 'TiffTags', struct('ExtraSamples', Tiff.ExtraSamples.Unspecified))
Warning Message:
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.
I get an error if I try defining the "SamplesPerPixel" TiffTag as 4. I don't know how to define the non-color channels as ExtraSamples using geotiffwrite.
I used the following example for Tiff objects to try to create the alpha channel data:

답변 (1개)

Shubham
Shubham 2023년 5월 31일
Hi Sonoma,
Yes, it is possible to write a GeoTiff file with color and alpha channel data using geotiffwrite function in MATLAB. However, you need to correctly define the "ExtraSamples" TiffTag in order to do so.
From the error and warning messages you received, it seems like the "ExtraSamples" TiffTag was not defined correctly. Instead of defining it as "Unspecified", you can define the non-color channels as "ExtraSamples". For example, you can modify the "TiffTags" struct as follows:
TiffTags.Photometric = Tiff.Photometric.RGB;
TiffTags.BitsPerSample = 8;
TiffTags.SamplesPerPixel = 4;
TiffTags.ExtraSamples = Tiff.ExtraSamples.UnassociatedAlpha;
This sets the "Photometric" tag to RGB, "BitsPerSample" to 8 (assuming your image has 8-bit color depth), "SamplesPerPixel" to 4 (indicating that there are 4 color channels), and "ExtraSamples" to "UnassociatedAlpha" (indicating that the extra channel is an alpha channel that is not associated with any specific color).
Then, you can pass this "TiffTags" struct to the geotiffwrite function like this:
geotiffwrite('img.tif', img, R, 'TiffTags', TiffTags);
This should allow you to write a GeoTiff file with color and alpha channel data using geotiffwrite function in MATLAB.
  댓글 수: 10
DGM
DGM 2025년 8월 18일 19:52
편집: DGM 2025년 8월 18일 20:05
Oh. I thought you were referring to the earlier comment.
geotiffwrite() doesn't do what @Taylor Shropshire suggests it's doing. Inserting NaN doesn't implicitly create alpha data. It just inserts NaNs, which may be treated strangely when rendered onscreen.
You're right in that it's not possible with integer-class images. I see why you were after using double now.
% the source image
infile = 'boston_ovr.jpg';
inpict = imread(infile);
outpict = im2double(inpict); % it needs to be float if we want to use NaN
% use a mask to insert some NaNs as pseudo-transparency
sz = size(inpict);
[x y] = meshgrid(1:sz(2),1:sz(1));
mk = repmat(x + y > sz(1),[1 1 size(inpict,3)]);
outpict(mk) = NaN;
% write the geotiff
worldfile = getworldfilename(infile);
R = worldfileread(worldfile, 'geographic', sz);
outfile = 'test.tif';
TiffTags.Photometric = Tiff.Photometric.RGB;
geotiffwrite(outfile, outpict, R, 'TiffTags', TiffTags)
% read the file back
[recpict R] = geotiffread(outfile);
imshow(recpict)
% it's full of NaNs still
nnz(isnan(recpict))
% all the NaNs are exactly as they were
isequal(isnan(outpict),isnan(recpict))
% the non-NaN content also remains unchanged
isequal(outpict(~mk),recpict(~mk))
% try to use imread to double-check that there's no alpha
% we can also check the metadata of the written file
[recpict2,~,alpha] = imread(outfile);
size(alpha) % it's empty
I can't run this on the forum, since for some reason all the map toolbox demo images are missing. When run, this should show that the process of writing the TIFF does not create any actual alpha data. It's up to the application to choose how to render non-numeric data. Unless given extra info, tools like imshow() will render it as black. I don't know what other viewers render it as, since I don't have anything else that can open float TIFFs. If QGIS renders them as transparent (or merely as the same color as the GUI background), then maybe it simply gives that illusion.
As to what's a better choice, I don't know. Like I said, I don't really use mapping toolbox stuff. There may be a way to store the image data and non-image data in separate files, such as would allow the use of a PNG that would be well-suited to simple integer-class RGBA content. That might not be the case, and it might not suit your workflow anyway.
Randall
Randall 2025년 8월 19일 20:37
I successfully relicated @Taylor Shropshire results when using geotiffwrite with an Index Image. The image imports into Google Earth my alpha layer. I don't beleive it is a true alpha layer as I use a binary mask (alpha=0 or 1). I suspect Google Earth is treating a border of 0's as background, thus hiding this part of the image from the viewer.
image is a [mxn] double array where the data I want to hide are NaNs.
climits = 2e-3*[0 1];% in my case
cmap = jet;% in my case
R is a georefcell structure.
rgb = double2rgb(image,cmap,climits);
[X,cmap] = rgb2ind(rgb,256);
geotiffwrite(filename,X,cmap,R);
Thank you for all the help

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by