Chapter 6. Python and Remote Sensing

In this chapter, we will discuss remote sensing. This field grows more exciting everyday as more satellites are launched and the distribution of data becomes easier. The high availability of satellite and aerial images as well as interesting new types of sensors launching each year is changing the role that remote sensing plays in understanding our world.

In this field, Python is quite capable. However, in this chapter, we will rely more on the Python bindings to the C libraries than we have in the previous chapters, where the focus was more on using pure Python. The only reason for this change is the size and complexity of remotely-sensed data. In remote sensing, we step through each pixel in an image and perform some form of query or mathematical process. An image can be thought of as a large numerical array. In remote sensing, these arrays can be quite large to the order of tens of megabytes to several gigabytes. While Python is fast, only C-based libraries can provide the speed needed to loop through arrays at a tolerable speed.

The compromise that we make in this chapter is that whenever possible, we'll use the Python Imaging Library (PIL) for image processing and NumPy, which provides multidimensional array mathematics. While written in C for speed, these libraries are designed for Python and provide a pythonic API.

In this chapter, we'll start with basic image manipulation and build on each exercise all the way to automatic change detection. The following are the topics that we'll cover:

  • Swapping image bands
  • Creating image histograms
  • Classifying images
  • Extracting features from images
  • Using change detection

Swapping image bands

Our eyes can see colors only in the visible spectrum as combinations of Red, Green, and Blue (RGB). Air- and space-borne sensors can collect wavelengths of the energy outside the visible spectrum. In order to view this data, we move images representing different wavelengths of light reflectance in and out of the RGB channels to make color images. These images often end up as bizarre and alien color combinations that can make visual analysis difficult. An example of a typical satellite image is seen in the following Landsat 7 satellite scene near the NASA Stennis Space Center in Mississippi along the Gulf of Mexico, which is a leading center for remote sensing and geospatial analysis in general:

Swapping image bands

Most of the vegetation appears red, and water appears almost black. This image is a type of false color image meaning that the color of the image is not based on RGB light. However, we can change the order of the bands or swap certain bands to create another type of false-color image that looks more like the world that we are used to seeing. In order to do so, you first need to download this image as a ZIP file from http://git.io/vqs41.

We installed the GDAL library with Python bindings in the Installing GDAL and NumPy sections in Chapter 4, Geospatial Python Toolbox. The GDAL library includes a module called gdal_array that loads and saves remotely-sensed images to and from the NumPy arrays for easy manipulation. GDAL itself is a data access library and does not provide much in the name of processing. So, in this chapter, we will rely heavily on NumPy to actually change images.

In this example, we'll load the image to a NumPy array using gdal_array, and then we'll immediately save it back to a new GeoTIFF file. However, on saving, we'll use NumPy's advanced array-slicing feature to change the order of the bands. Images in NumPy are multidimensional arrays in the order of band, height, and width. So, an image with three bands will be an array of length three, containing an array for each band, height, and width of the image. It's important to note that NumPy references the array locations as y,x (row, column) instead of the usual x, y (column, row) format that we work with in spreadsheets and other software:

from osgeo import gdal_array
# name of our source image
src = "FalseColor.tif"
# load the source image into an array
arr = gdal_array.LoadFile(src)
# swap bands 1 and 2 for a natural color image.
# We will use numpy "advanced slicing" to reorder the bands.
# Using the source image
output = gdal_array.SaveArray(arr[[1, 0, 2], :], "swap.tif",
                      format="GTiff", prototype=src)
# Dereference output to avoid corrupted file on some platforms
output = None

In the SaveArray method, the last argument is called prototype. This argument lets you specify another image for GDAL from which to copy spatial reference information and some other image parameters. Without this argument, we'd end up with an image without georeferencing information, which could not be used in a GIS. In this case, we specified our input image file name because the images are identical except for the band order.

The result of this example produces the swap.tif image, which is a much more visually appealing image with green vegetation and blue water:

Swapping image bands

There's only one problem with this image. It's kind of dark and difficult to see. Let's see if we can figure out why.

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

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