13

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.

10
  • 2
    There's a known bug with alpha channels in gdal_warp. Try warping the alpha band of each raster separately and then recombine later (see gis.stackexchange.com/questions/49706/…) Commented Jun 17, 2015 at 1:09
  • great, thanks for the quick response! do you mean separate the alpha layer from the rgb bands before performing gdalwarp, then recombine after? Commented Jun 17, 2015 at 1:41
  • That's it. gdal_warp has issues warping with alpha so treat as RGB and not RGBA. To separate gdal_translate -of GTIFF -b 1 -b 2 -b 3 (creates an RGB image from RGBA). Commented Jun 17, 2015 at 1:50
  • ok cool, then recombine via a vrt as per your link? when recombining using gdalbuildvrt -separate, is there a way to take 3 bands from the first image and 1 from the second, or should I be using a gdalbuildvrt option to combine? Commented Jun 17, 2015 at 2:02
  • 1
    No, I don't think any of the command line tools can do alpha blending. Do you have QGIS (or ArcGis) both have raster calculators that could perform this function after warping. It certainly wouldn't be as easy as just dropping them into a VRT... I believe that VRT completely overwrites underlying pixels, not Alpha blend; perhaps that's something that could be put to the developers as an improvement request. Commented Jun 17, 2015 at 3:04

2 Answers 2

1

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 )

0

Radouxju is right, gdalwarp allows you to fade layers based on the alpha band

-cblend appears to be an alternative to having a fading alpha band

Without using -cblend, I found that

import os
cmd = r'gdalwarp -of GTiff "C:\Temp\Image1.tif" "C:\Temp\Image2.tif" "C:\Temp\Output.tif" '
os.system(cmd)

was enough to layer two rasters with fading alpha bands in the same way that they appear in a map window

The above method is useful for those wishing to use a program with a python window like QGIS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.