# Superpixel and Earth Observation – Intro

• blog

The Rub’ al Khali, also known as the Empty Quarter, is beautiful and is also massive. It is the world’s largest sand desert (also known as an erg) covering an area larger than France. If you have watched Star Wars: Force Awakens you might be interested to know that 6 months of filming took place here. There is something about a an erg that I find captivating.

I wrote a series on Interactive Image Segementation earlier in the year. I used the Watershed Algorithm to map circular irrigation systems, firstly by looking at coins. This time I will look at Superpixels in the Rub’ al Khali, but also show how to segment this year’s squashes!

What can I say, deserts and image segmentation, they are just made for each other.

### First up – What is a Superpixel?

Think about pixels. They are mostly square. An image is a made up of a regular grid of pixels.

The pixel-grid, however, is not a natural representation of visual scenes. It is rather an “artifact” of a digital imaging process. It would be more natural, and presumably more efficient, to work with perceptually meaningful entities obtained from a low-level grouping process. For example, we can apply the Normalized Cuts algorithm to partition an image into, say, 500 segments (what we call superpixels).

The above definition is taken from the webpage shown below. “Superpixel: Empirical Studies and Application”

http://ttic.uchicago.edu/~xren/research/superpixel/

Let’s apply image segmentation to satellite images and create superpixel maps.

Scikit-image has a segementation model that we can implement pretty fast in Python. It has an algorithm built in called Simple Linear Iterative Clustering (SLIC). Details of this algorithm are found here; it’s an interesting read.

Below is the implementation of the algorithm in skimage. Don’t be put off with the number of arguments to parse, you can run this algorithm just by specifying the n_segments parameter (the number of segments you think are in the image); adjust this parameter to fit your own images (the more complex, the more segments).

```skimage.segmentation.slic(image, n_segments=100, compactness=10.0,
max_iter=10, sigma=0, spacing=None, multichannel=True,
convert2lab=None, enforce_connectivity=True, min_size_factor=0.5,
max_size_factor=3, slic_zero=False)```

You can run the segmentation on any image. As it is Autumn time let’s run it on my squash harvest.

It gives a passable result. I set the n_segments to 50 and sigma to 4: the lower the value of sigma the more jagged the edges of the segmentation are; the higher, the smoother. It is a playoff as to what you wish to achieve :). Compactness is also another interesting parameter; the higher that value the ‘squarer’ your result.

### The code

Image segmentation is not very complex to implement. Its basic form is shown below.

```from skimage.segmentation import slic
from skimage.segmentation import mark_boundaries
from skimage.util import img_as_float
import cv2

# load the image and apply SLIC
## Change the n_segments and sigma values to fit your needs
segments = slic(img_as_float(image), n_segments = 50, sigma = 4)

bounds = mark_boundaries(image, segments)

cv2.imshow("bounds", bounds)
cv2.waitKey(0)
cv2.destroyAllWindows```

### Superpixel and the Rub’ al Khali

Back to the desert. Segmenting images presents a nice way to map sand dunes. These are complex geomorphological features, varied in elevation and orientation. Navigating can be tricky, modelling them also complex.

Top left (100 segments), Top right (300), Bottom left (500), Bottom right (1000)

Segmenting images in this way is a very fast, unsupervised way of classifying data. Like a k-means unsupervised image classification the user control and iterating the model are critical steps. The great thing about this is that it is fast. Really fast. Think about natural disasters and being able to quickly classify the imagery on hand, comparing before and after imagery and all of a sudden this becomes an incredibly valuable way of identifying change and areas of damage. You are on your way to further insights about the world around us.

Once segmented, you can just separate out image ‘chips’. Say for example an isolated dune. Why? Input to classification, input to template matching, an input into machine learning!

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