Counting the unique values in a raster

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.

Getting ready

We will use the SatImage raster available at https://geospatialpython.googlecode.com/files/SatImage.zip.

Place this raster in your /qgis_data/rasters directory.

How to do it...

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:

  1. Start QGIS.
  2. From the Plugins menu, select Python Console.
  3. First, we must import the bridge module called gdalnumeric, which connects GDAL to Numpy in order to perform an array math on geospatial images:
    import gdalnumeric
    
  4. Now, we will load our raster image directly into a multidimensional array:
    a = gdalnumeric.LoadFile("/qgis_data/rasters/satimage.tif")
    
  5. The following code counts the number of pixel combinations in the image:
    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]
    
  6. Now, we can take a look at the size of the resulting one-dimensional array to get the unique values count:
    u.size
    

Lastly, verify that the result is 16085631.

How it works...

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset