Adding an alpha channel to geoTIFF image

조회 수: 5 (최근 30일)
S Jones
S Jones 2017년 9월 8일
댓글: S Jones 2017년 9월 14일
Trying to add an alpha (transparency) channel to the RGB data in a georeferenced tiff image. I was led through the process of creating the geotiff in a separate question. The first part of the code creates a geotiff from the attached png file, the second part creates a mask from white regions, appends it to the RGB data and attempts to modify the appropriate tags before rewriting the geotiff. However the resultant image is blank. I suspect I'm doing something wrong with the 'photometric' tag but can't see the problem.
file = 'uk_dT.png' ;
[path,name,ext] = fileparts(file) ;
I = imread(file) ;
I = flipud(I);
lonmin = -17; lonmax = 10; latmin = 47; latmax = 63;
% Write to geotiff
R = georasterref('RasterSize',size(I),'LatitudeLimits',[latmin,latmax],'LongitudeLimits',[lonmin,lonmax]);
tiffile = strcat(name,'.tif') ;
geotiffwrite(tiffile,I,R) % original code
%%Now we have a geotiff but need transparency.
% Make alpha mask by identifying white regions
S = sum(I,3);
mask = 255*ones(size(I,1),size(I,2));
mask(S==765) = 0; % create mask
I = cat(3,I,mask); % concatenate alpha channel onto RGB data
info = geotiffinfo(tiffile); % reload RGB tiff data
% use Tiff tags to append alpha data
tiffile2 = strcat(name,'2.tif') ;
geotiffwrite(tiffile2,I,R,'GeoKeyDirectoryTag', ...
info.GeoTIFFTags.GeoKeyDirectoryTag,'TiffTags', ...
struct('ExtraSamples',Tiff.ExtraSamples.AssociatedAlpha, ...
'Photometric',Tiff.Photometric.Separated));
  댓글 수: 1
S Jones
S Jones 2017년 9월 14일
Edit: Having played with this problem a bit more, it appears it is impossible to add an alpha channel to a geoTIFF in Matlab (at least without some clever workaround) for a couple of reasons:
1: the 'Photometric' tag should be set to 'RGB', as the colour space we're creating is RGB+alpha. However there is an interlock in geotiffwrite which prevents you from writing anything other than an x*y*3 array when this tag is set to RGB. The code I utilised above (found somewhere online) gets round an error by using 'Separated' instead, but this is associated with the CMYK colour space so is surely not the right setting.
2: You set the 'ExtraSamples' tag to inform that something other than RGB data is expected. But in other examples I've seen, you also have to set the 'SamplesPerPixel' tag to 4 to reflect this. Geotiffwrite does not recognise this as a valid tag. In fact, diving into the geotiffwrite code and looking at the tags which are valid, there are only a fraction of the options available compared to the tiff() command.
I would love to be proved wrong but it appears that I'm going to have to manually load hundreds of files in an external package and change white areas to transparent!

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by