Self-Organizing Maps for Sentinel 2 Image Segmentation using Python

GeoSense ✅
Published in
6 min readApr 29


A step-by-step guide to segmenting a Sentinel-2 image using Self-Organizing Maps (SOM) in Python

Self-Organizing Maps (SOMs) are a type of unsupervised artificial neural network that can be used for various applications, including image segmentation. In this article, we’ll explore how SOMs can be used for Sentinel-2 image segmentation using Python.

Sentinel-2 is a satellite-based Earth observation mission that provides high-resolution multispectral images with 10 spectral bands. These images can be used for various applications, including land cover mapping, crop monitoring, and environmental monitoring. Image segmentation is an important task in remote sensing that involves partitioning an image into several homogeneous regions based on their spectral properties. This can be achieved using various algorithms, including SOMs.

To get started, we’ll need to load the Sentinel 2 image into Python. We can use the rasterio library to read the image and convert it to a numpy array. Here's the code to load the image:

import rasterio
from rasterio.plot import show

# Open the image
with'Data/data/SENTINEL2A.tif') as src:
# Read the image data
img =[3,2,1])
# Display the image
profile = src.profile
Source: author

We’re reading the image using the function and converting it to a float32 numpy array using the astype method. This is important because SOMs require the input data to be normalized.

Next, we’ll need to reshape the image into a 2D array. We can use the reshape method of numpy arrays to achieve this. Here's the code:

import numpy as np

# Reshape the image into a 2D array
img = img.transpose(1, 2, 0)
h, w, bands = img.shape
data = np.reshape(img, (h*w, bands))

# Normalize the data
data = (data - np.mean(data, axis=0)) / np.std(data, axis=0)

We’re using the shape attribute of the numpy array to get the height, width, and number of bands of the image. We're then using the reshape method to convert the image into a 2D array where each row corresponds to a pixel in the image and each column corresponds to a spectral band.



GeoSense ✅

🌏 Remote sensing | 🛰️ Geographic Information Systems (GIS) | ℹ️