Jupyter Notebooks and Satellite Imagery

Notebooks

I have been meaning for the last 6 months to write about Jupyter Notebooks and why they are the perfect toolbox for working with Python and Satellite Imagery. I like Notepad ++ a great deal, it is simple and easy to use and will remain my default choice for the time being for writing code. However, I find that for teaching geospatial Python, Jupyter Notebooks are an incredibly useful way of learning and documenting code. They also provide that all important fast feedback within the browser and they are an excellent visual record.

I am seeing more Jupyter Notebooks being created that are focused on EO and GIS. For example, Digital Globe, Planet and ESRI are increasingly building notebooks (see an ESRI guide here). I am sure there are plenty of others – please do share if you happen to know of any. Whole books are being presented as Jupyter Notebooks, for example this one on Python Data Science. So what are you waiting for?

Installation (windows)

It is super simple. Assuming you have Python installed with Pip, these are the commands:

for Python 3 –

python3 -m pip install jupyter

or for 2.7

python -m pip install jupyter

Once installed just call the following at the command line:

jupyter notebook

It is that easy. No reason not to install it if you are already familiar with Python. Look for the button on the right called ‘New’ and create your new Notebook. Or you could import one. Any libraries already installed are available through the usual import call. So you are ready to code.

Viewing your first Satellite image in Jupyter Notebooks

There are several ways of doing this; all the ways I know use Matplotlib. This seems the simplest way. Note I am using a .jpg image:

import cv2
from matplotlib import pyplot as plt
import numpy as np

img = cv2.imread('../image.jpg')
plt.imshow(img)
plt.show()

Looks like this:

Notebooks

OpenCV loads images in Blue, Green, Red (BGR) as opposed to Red, Green, Blue (RGB). Fortunately it is easy to convert.

img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
plt.show()

Notebook

Using notebooks allows you to display multiple images or graphs alongside each other using the Matplotlib subplot. Here I convert the RGB image above to HSV (Hue, Saturation and Value) and plot alongside the RGB image.

 

img_hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)## convert to HSV

plt.figure(figsize=(10, 3))

plt.subplot(1, 2, 1)
plt.imshow(img_rgb)

plt.subplot(1, 2, 2)
plt.imshow(img_hsv)

plt.show()

Notebooks

The real power I have found with using these notebooks is the ability to paste other code into your workflows to quickly see its impact. For example, to view a histogram of HSV I used this code.

## https://giusedroid.blogspot.co.uk/2015/04/using-python-and-k-means-in-hsv-color.html

hue, sat, val = img_hsv[:,:,0], img_hsv[:,:,1], img_hsv[:,:,2]

plt.figure(figsize=(10,8))
plt.subplot(311)                             #plot in the first cell
plt.subplots_adjust(hspace=.5)
plt.title("Hue")
plt.hist(np.ndarray.flatten(hue), bins=180)
plt.subplot(312)                             #plot in the second cell
plt.title("Saturation")
plt.hist(np.ndarray.flatten(sat), bins=128)
plt.subplot(313)                             #plot in the third cell
plt.title("Luminosity Value")
plt.hist(np.ndarray.flatten(val), bins=128)
plt.show()

which produces:

Matplotlib

Note that in OpenCV the HSV range is: H: 0 to 179 S: 0 to 255 V: 0 to 255.

Hopefully the benefit of being able to see information about your imagery alongside the imagery itself in one continuous web page will be apparent.

More to come

This was just an introduction; the notebook is available on my GitHub page here. It makes quite easy reading. And in that one sentence it demonstrates why using Juypter Notebooks is such a compelling reason to share your code and projects.

I am a freelancer able to help you with your projects. I offer consultancy, training and writing. I’d be delighted to hear from you.

I have grouped all my previous blogs (technical stuff / tutorials / opinions / ideas) are here http://gis.acgeospatial.co.uk

I am @map_andrew on twitter