Attribute segments with statistics from all image bands using RSGISLib

RSGISLib contains functions for segmenting an image and attributing each segment with statistics from image bands, allowing an object-based classification to be performed.

The standard XML command to attribute image segments is:

<rsgis:commands xmlns:rsgis="">
    <rsgis:command algor="rastergis" option="popattributestats"
        clumps="clumps.kea" >
        <rsgis:band band="1" mean="FPC" />
        <rsgis:band band="2" mean="LHH"/>
        <rsgis:band band="3" mean="LHV" />

Or the python equivalent:

from rsgislib import rastergis
stats2Calc = list()
stats2Calc.append(rastergis.BandAttStats(band=1, meanField="FPC"))
stats2Calc.append(rastergis.BandAttStats(band=2, meanField="LHH"))
stats2Calc.append(rastergis.BandAttStats(band=3, meanField="LHV"))        
rastergis.populateRATWithStats('fpcpalsar_stack.kea', 'clumps.kea', stats2Calc)

Which is very flexible and allows you to calculate different statistics from different bands, pick a subset of bands etc. I wanted to calculate the same stats for all bands in an image, named using the band names from the image, similar to what’s possible with the zonal stats command (see This was pretty easy to accomplish using the GDAL python bindings to read in names for each band, then creating an BandAttStats object from these to be passed to the python bindings.

The script can be downloaded from here, and is run using:

python --inimage image.kea \
--inclumps clumps.kea --mean

Additional statistics can be set using flags (–min, –max etc.,).

Band names can be set using the script, with a text file used to provide names for multiple bands.

More details on classifying the attributed segments, using RIOS, are in our Python course (available to download from here.)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s