[GIS] preserve a fading alpha layer when mosaicing images with gdal

gdalgdal-translategdalbuildvrtgdalwarpgeotiff-tiff

I am mosaicing some images with gdal and would like to improve the final result by using a fading / gradual alpha layer towards the edge of each image to remove the sharp edges in the middle of the mosaic. The issue I'm having is that the portion of each individual image with the gradual alpha layer is masking the images beneath it in the final mosaic, rather than being semi-transparent, as shown below:

mosaic with gradual alpha layers masking images

Ideally I'd like one image to fade into the next using this gradual transparency.

The steps I perform to generate the mosaic are as follows:

Add gcps to the original images to geolocate them and orient them properly (done to each image in turn):

gdal_translate -of GTiff -a_srs EPSG:4326 -a_srs EPSG:4326 -gcp 1616 0 -88.2728612066 40.5175787437 -gcp <etc., etc.> <original_image_with_gradual_alpha>.tif <image_with_gradual_alpha_and_gcps>.tif

Warp the images to new geotiffs which are oriented properly (done to each image in turn):

gdalwarp -s_srs EPSG:4326 -t_srs EPSG:4326 -dstnodata 0 <image_with_gradual_alpha_and_gcps>.tif <warped_geotiff_with_alpha>.tif

Combine all the warped images together into one mosaic:

gdalbuildvrt -srcnodata 0 mosaic.vrt <warped_geotiff_with_alpha_root>*.tif
gdal_translate mosaic.vrt mosaic.tif

The image I linked is mosaic.tif.

gdalinfo for a sample input file:

Driver: GTiff/GeoTIFF
Files: dsc00562.tif
Size is 1616, 1080
Coordinate System is `'
Metadata:
  TIFFTAG_RESOLUTIONUNIT=2 (pixels/inch)
  TIFFTAG_XRESOLUTION=350
  TIFFTAG_YRESOLUTION=350
Image Structure Metadata:
  INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left  (    0.0,    0.0)
Lower Left  (    0.0, 1080.0)
Upper Right ( 1616.0,    0.0)
Lower Right ( 1616.0, 1080.0)
Center      (  808.0,  540.0)
Band 1 Block=1616x1 Type=Byte, ColorInterp=Red
  Mask Flags: PER_DATASET ALPHA 
Band 2 Block=1616x1 Type=Byte, ColorInterp=Green
  Mask Flags: PER_DATASET ALPHA 
Band 3 Block=1616x1 Type=Byte, ColorInterp=Blue
  Mask Flags: PER_DATASET ALPHA 
Band 4 Block=1616x1 Type=Byte, ColorInterp=Alpha

gdalinfo for the warped geotiff with gradual alpha layer:

Driver: GTiff/GeoTIFF
Files: geo_dsc00603.tif
Size is 1944, 1356
Coordinate System is:
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0],
    UNIT["degree",0.0174532925199433],
    AUTHORITY["EPSG","4326"]]
Origin = (-88.275727919349990,40.518829195724997)
Pixel Size = (0.000001599004942,-0.000001599004942)
Metadata:
  AREA_OR_POINT=Area
  TIFFTAG_RESOLUTIONUNIT=2 (pixels/inch)
  TIFFTAG_XRESOLUTION=350
  TIFFTAG_YRESOLUTION=350
Image Structure Metadata:
  INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left  ( -88.2757279,  40.5188292) ( 88d16'32.62"W, 40d31' 7.79"N)
Lower Left  ( -88.2757279,  40.5166609) ( 88d16'32.62"W, 40d30'59.98"N)
Upper Right ( -88.2726195,  40.5188292) ( 88d16'21.43"W, 40d31' 7.79"N)
Lower Right ( -88.2726195,  40.5166609) ( 88d16'21.43"W, 40d30'59.98"N)
Center      ( -88.2741737,  40.5177451) ( 88d16'27.03"W, 40d31' 3.88"N)
Band 1 Block=1944x1 Type=Byte, ColorInterp=Red
  NoData Value=0
Band 2 Block=1944x1 Type=Byte, ColorInterp=Green
  NoData Value=0
Band 3 Block=1944x1 Type=Byte, ColorInterp=Blue
  NoData Value=0
Band 4 Block=1944x1 Type=Byte, ColorInterp=Alpha
  NoData Value=0

gdalinfo for the final mosaic:

Driver: GTiff/GeoTIFF
Files: mosaic.tif
Size is 5702, 6846
Coordinate System is:
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0],
    UNIT["degree",0.0174532925199433],
    AUTHORITY["EPSG","4326"]]
Origin = (-88.278946072799997,40.524561377550008)
Pixel Size = (0.000001509761581,-0.000001509761581)
Metadata:
  AREA_OR_POINT=Area
Image Structure Metadata:
  INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left  ( -88.2789461,  40.5245614) ( 88d16'44.21"W, 40d31'28.42"N)
Lower Left  ( -88.2789461,  40.5142255) ( 88d16'44.21"W, 40d30'51.21"N)
Upper Right ( -88.2703374,  40.5245614) ( 88d16'13.21"W, 40d31'28.42"N)
Lower Right ( -88.2703374,  40.5142255) ( 88d16'13.21"W, 40d30'51.21"N)
Center      ( -88.2746417,  40.5193935) ( 88d16'28.71"W, 40d31' 9.82"N)
Band 1 Block=5702x1 Type=Byte, ColorInterp=Red
  NoData Value=0
Band 2 Block=5702x1 Type=Byte, ColorInterp=Green
  NoData Value=0
Band 3 Block=5702x1 Type=Byte, ColorInterp=Blue
  NoData Value=0
Band 4 Block=5702x1 Type=Byte, ColorInterp=Alpha
  NoData Value=0

I've included a sample image after each stage of the process and the final mosaic at in a dropbox link here – I can provide the entire image set if necessary.

Best Answer

The problem with your workflow is not linked to the alpha layer, but the fact that only the last image is used when you create a vrt.

gdalmerge doc

In areas of overlap, the last image will be copied over earlier ones.

gdalbuildvrt doc :

If there is some amount of spatial overlapping between files, the order of files appearing in the list of source matter: files that are listed at the end are the ones from which the content will be fetched. Note that nodata will be taken into account to potentially fetch data from less prioritary datasets, but currently, alpha channel is not taken into account to do alpha compositing (so a source with alpha=0 appearing on top of another source will override is content). This might be changed in later versions.

So in fact the transparent area is simply transparent, and there is nothing to see under it.

If you want to use some blending, you need to do so using gdalwarp: it handles the alpha bands as well as blending based on a given distance in pixels (-cblend distance )

Related Question