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="http://www.rsgislib.org/xml/">
    <rsgis:command algor="rastergis" option="popattributestats"
        input="fpcpalsar_stack.kea"
        clumps="clumps.kea" >
        <rsgis:band band="1" mean="FPC" />
        <rsgis:band band="2" mean="LHH"/>
        <rsgis:band band="3" mean="LHV" />
    </rsgis:command>
</rsgis:commands>

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 rsgislib_zonalStats.py). 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 rsgislib_attributeRAT.py --inimage image.kea \
--inclumps clumps.kea --mean

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

Band names can be set using the setbandname.py 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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s