4

I have a giant GeoTIFF I'd like to convert to grayscale and add a gaussian blur to. As far as I know, GDAL can strip two of the three color bands and call it grayscale, which might work, but is there a better way? Imagemagick has both grayscale and gaussian blur functionality, but can not process georeferenced TIFFs (as far as I know). Any suggestions how I should go about doing this?

2 Answers 2

21

GDAL has a wonderful file format called VRT, which is an XML wrapper around one or more raster files.

One feature of VRTs is their ability to encode square convolution kernels for any given band. It does involve playing around with XML in a text editor (or programatically), but if you're already used to the GDAL tools, it shouldn't be too hard.

Example's input

To illustrate, I took this image of some Ordnance Survey data from around the old OS building in Southampton:

Crown Copyright (c) 2012

Embed into .vrt

And ran gdalbuildvrt to generate an initial VRT file:

gdalbuildvrt shirley.vrt shirley.tif

Edit metadata

Then I replaced the <SimpleSource> elements with <KernelFilteredSource> elements and add in the element <Kernel normalized="1"></Kernel> with a Size and Coefs (coefficients) for a 5x5 Gaussian convolution kernel:

<KernelFilteredSource>
  <SourceFilename relativeToVRT="1">shirley.tif</SourceFilename>
  <SourceBand>1</SourceBand>
  <SourceProperties RasterXSize="400" RasterYSize="400" DataType="Byte" BlockXSize="256" BlockYSize="256" />
  <SrcRect xOff="0" yOff="0" xSize="400" ySize="400" />
  <DstRect xOff="0" yOff="0" xSize="400" ySize="400" />
  <Kernel normalized="1">
    <Size>5</Size>
    <Coefs>0.0036630037 0.0146520147 0.0256410256 0.0146520147 0.0036630037
           0.0146520147 0.0586080586 0.0952380952 0.0586080586 0.0146520147
           0.0256410256 0.0952380952 0.1501831502 0.0952380952 0.0256410256
           0.0146520147 0.0586080586 0.0952380952 0.0586080586 0.0146520147
           0.0036630037 0.0146520147 0.0256410256 0.0146520147 0.0036630037
    </Coefs>
  </Kernel>
</KernelFilteredSource>

You can get alternative values thanks to an online Gaussian Kernel Calculator.

Convert back to .tiff

Then ran gdal_translate to convert to a TIFF:

gdal_translate -co TILED=YES shirley_gauss.vrt shirley_gauss.tif

Which gives me this image:

Crown Copyright (c) 2012

With its georeferencing data intact, which you can check via :

gdalinfo filename.ext

Grey scale

For the greyscale part, I suggest you use Quantum GIS and its good (if currently slightly quirky) Raster Calculator. Simply load up your blurred image, select Raster | Raster Calculator... and use the following expression:

(shirley_gauss@1 * 0.3) + (shirley_gauss@2 * 0.59) + (shirley_gauss@3 * 0.11)

Loading that image into QGIS, gives me:

Crown Copyright (c) 2012

Other coefficients can be used for the greyscale conversion, but those are a good starting point.

Also, it shouldn't matter whether you blur first then reduce, or the other way round.

5
  • 2
    +1 Very nice! And you're correct, the order of operations does not matter mathematically. In practice it might make a very slight difference due to integer rounding (or truncation) if intermediate results are saved as 24-bit images.
    – whuber
    Commented Feb 13, 2012 at 17:08
  • I'll give this a try and let you know how it goes Commented Feb 14, 2012 at 6:19
  • Is it possible to do this with multiple tiles, with blurring across tile boundaries? I have a large dataset that I'd like to gaussian blur, but using gdalbuildvrt to generate a VRT puts all of the files in separate SimpleSource containers, so I'm guessing this method would do the blurring independently for each tile?
    – naught101
    Commented May 9, 2019 at 5:36
  • Answer: Yes - see gis.stackexchange.com/questions/322224/… I'm not sure about how to actually generate the coefficients though..
    – naught101
    Commented May 11, 2019 at 5:36
  • I used this method to apply a Gaussian blur. My input image as -9999 as nodata value. The output image contains 0 where nodata is expected, and still indicates -9999 as nodata value in its metadata. Which option should I use to keep both the nodata value and the nodata pixels in my output image?
    – tvoirand
    Commented Apr 20, 2021 at 15:04
0

You're right, you'll lose georeferencing information when you process GeoTIFF in most 3rd party packages. One way of extracting the world georeferencing information from a GeoTIFF:

  1. Open ArcCatalog and gather the following properties from your GeoTIFF:
    • CellSize (X, Y)
    • Extent Left
    • Extent Top
  2. Create a text document named filename.tfw with the following 6 lines:

    • CellSize X
    • 0.0
    • 0.0
    • -CellSize Y
    • Extent Left
    • Extent Top

    it's important that the 4th parameter is negative and that you use the Top Left corner.

  3. When you convert your GeoTIFF in a 3rd party tool, you can provide this World file with the output image to regeoreference the image for ArcGIS.

Another way, is to use ArcCatalog convert the GeoTIFF into another Raster format, say, BMP, which forces the creation of the World file (usually .bpw) and the Auxiliary file containing projection (usually .aux.xml). You can discard the BMP itself but keep these additional files so that you can reapply them for your TIFF image.

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.