Remotely-sensed images are not just pictures; they are data. The value of the pixels has meaning that can be automatically analyzed by a computer. The ability to run statistical algorithms on a dataset is key to remote sensing. This recipe counts the number of unique combinations of pixels across multiple bands. A use case for this recipe will be to assess the results of image classification, which is a recipe that we'll cover later in this chapter. This recipe is in contrast to the typical histogram function, which totals the unique values and the frequency of each value per band.
We will use the SatImage raster available at https://geospatialpython.googlecode.com/files/SatImage.zip.
Place this raster in your /qgis_data/rasters
directory.
This algorithm relies completely on the numpy
module, which is included with PyQGIS. Numpy can be accessed through the GDAL package's gdalnumeric
module. To do this, we need to perform the following steps:
gdalnumeric
, which connects GDAL to Numpy in order to perform an array math on geospatial images:import gdalnumeric
a = gdalnumeric.LoadFile("/qgis_data/rasters/satimage.tif")
b = a.T.ravel() c=b.reshape((b.size/3,3)) order = gdalnumeric.numpy.lexsort(c.T) c = c[order] diff = gdalnumeric.numpy.diff(c, axis=0) ui = gdalnumeric.numpy.ones(len(c), 'bool') ui[1:] = (diff != 0).any(axis=1) u = c[ui]
u.size
Lastly, verify that the result is 16085631
.
The numpy
module is an open source equivalent of the commercial package Matlab
. You can learn more about Numpy at: http://Numpy.org.
When you load an image using Numpy, it is loaded as a multidimensional array of numbers. Numpy allows you to do an array math on the entire array using operators and specialized functions, in the same way you would on variables containing a single numeric value.