Classifying images

Automated Remote Sensing (ARS)is rarely ever done in the visible spectrum. ARS is the processing of an image without any human input. The most commonly available wavelengths outside of the visible spectrum are infrared and near-infrared. The following scene is a thermal image (band 10) from a fairly recent Landsat 8 flyover of the U.S. Gulf Coast from New Orleans, Louisiana to Mobile, Alabama. The major natural features in the image are labeled so that you can orient yourself:

Classifying images

As every pixel in this image has a reflectance value, it is information. Python can see these values and pick out features in the same way that we intuitively do by grouping the related pixel values. We can colorize the pixels based on their relation to each other to simplify the image and view the related features. This technique is called classification.

Classification can range from fairly simple groupings based only on some value distribution algorithm derived from the histogram, to complex methods involving training datasets and even computer learning and artificial intelligence. The simplest forms are called unsupervised classifications in which no additional input is given other than the image itself. Methods involving some sort of training data to guide the computer are called supervised classifications. It should be noted that classification techniques are used across many fields, from medical doctors searching for cancerous cells in a patient's body scan to casinos using a facial recognition software on security videos to automatically spot known con artists at blackjack tables.

To introduce remote sensing classification, we'll just use the histogram to group the pixels with similar colors and intensities and see what we get. First, you'll need to download the Landsat 8 scene:

https://github.com/GeospatialPython/Learn/blob/master/thermal.zip?raw=true

Instead of our histogram() function from the previous examples, we'll use the version included with NumPy that allows you to specify a number of bins easily and returns two arrays with the frequency as well as ranges of the bin values. We'll use the second array with the ranges as our class definition for the image. The lut or look-up table is an arbitrary color palette used to assign colors to the 20 unsupervised classes. You can use any colors that you want.

"""Classify a remotely sensed image"""

# https://github.com/GeospatialPython/Learn/raw/master/thermal.zip

from osgeo import gdal_array

# Input file name (thermal image)
src = "thermal.tif"

# Output file name
tgt = "classified.jpg"

# Load the image into numpy using gdal
srcArr = gdal_array.LoadFile(src)

# Split the histogram into 20 bins as our classes
classes = gdal_array.numpy.histogram(srcArr, bins=20)[1]

# Color look-up table (LUT) - must be len(classes)+1.
# Specified as R, G, B tuples
lut = [[255, 0, 0], [191, 48, 48], [166, 0, 0], [255, 64, 64], [255, 115, 115],
       [255, 116, 0], [191, 113, 48], [255, 178, 115], [0, 153, 153],
       [29, 115, 115], [0, 99, 99], [166, 75, 0], [0, 204, 0], [51, 204, 204],
       [255, 150, 64], [92, 204, 204], [38, 153, 38], [0, 133, 0],
       [57, 230, 57], [103, 230, 103], [184, 138, 0]]

# Starting value for classification
start = 1

# Set up the RGB color JPEG output image
rgb = gdal_array.numpy.zeros((3, srcArr.shape[0],
                              srcArr.shape[1], ), gdal_array.numpy.float32)

# Process all classes and assign colors
for i in range(len(classes)):
    mask = gdal_array.numpy.logical_and(start <= srcArr, srcArr <= classes[i])
    for j in range(len(lut[i])):
        rgb[j] = gdal_array.numpy.choose(mask, (rgb[j], lut[i][j]))
    start = classes[i]+1

# Save the image
output = gdal_array.SaveArray(rgb.astype(gdal_array.numpy.uint8), tgt, format="JPEG")
output = None

The following image is our classification output, which we just saved as JPEG.

We didn't specify the prototype argument when saving an image, so it has no georeferencing information, though we could easily have done otherwise to save the output as a GeoTIFF.

Classifying images

This result isn't bad for a very simple unsupervised classification. The islands and coastal flats show up as different shades of green. The clouds were isolated as shades of orange and dark blues. We did have some confusion in land where the land features were colored the same as the Gulf of Mexico. We could further refine this process by defining the class ranges manually instead of just using the histogram.

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

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