“Hello, World” of Remote Sensing, Hyperspectral, Multispectral Imaging

Arif
5 min readMar 9, 2024

All codes and files mentioned in this article are available on GitHub: https://github.com/arf-themascoteers/rmh

Remote sensing involves using various techniques to gather information from a distance. This includes hyperspectral, multispectral, and other types of sensing. For example, the Sentinel-2 satellite observes Earth surfaces using multispectral imaging.

From a programming perspective, if you grasp the concept of only one of them (for example, multispectral), that should be sufficient. Other kind of imaging or sensing data mostly differ only in the data structure (more specifically, shape of the array), or the library to be used to read them.

Let’s contrast Hyperspectral and Multispectral imaging with our familiar types of images to better understand them.

Grayscale Image: Commonly known as “black-and-while” image. Stored as a 2D array. Each value corresponds to the intensity of the pixel.

Example:

Sample grayscale image

Array Shape: 2D: Height x Width(e.g., the example image: 357 x 254)

A part of the 2D Array of the example image above:

2D array representation of the grayscale image

Each pixel contains a value between 0 to 255 representing intensity of the pixel (higher value indicates whiter/brighter pixel). 0 means complete black and 255 means complete white.

Python Code to Read and Visualize:

# Need to install cv2 and matplotlib using the following command in the terminal:
# pip install opencv-python matplotlib
import cv2
import matplotlib.pyplot as plt

file_path = 'gray.png'
gray_image = cv2.imread(file_path, cv2.IMREAD_GRAYSCALE)
plt.imshow(gray_image, cmap='gray')
plt.show()
print(gray_image.shape)

RGB Image: Any typical image you see on the web or print media, or you take with your smartphone, commonly known as “colour image”.

Example:

Sample RGB image

Array Shape:

3D (Height x Width x Channel). RGB images have 3 channels (Red, Green, Blue), therefore array shape is Height x Width x 3. For example, array shape of the example image: 301 x 293 x 3. We can conceptualize RGB images as three 2D arrays, each representing the Red, Green, and Blue channels, which are then stacked together to form a single 3D array.

Partial view of each 2D Array for Blue, Green, Red for the example image above:

Again, each pixel represents a value between 0 to 255. A pixel value 0 in the 2D array for red means there is no red component in the colour. On the other hand, a value of 255 would indicate a complete presence of the red component in the colour.

Python Code to Read and Visualize:

# Need to install cv2 and matplotlib using the following command in the terminal:
# pip install opencv-python matplotlib
import cv2
import matplotlib.pyplot as plt

file_path = 'rgb.png'
image = cv2.imread(file_path)
print(image.shape)

blue, green, red = cv2.split(image)

plt.imshow(blue, cmap="gray")
plt.show()

plt.imshow(green, cmap="gray")
plt.show()

plt.imshow(red, cmap="gray")
plt.show()

The code above shows each channel in grayscale separately.

Visualizing individual channels of RGB image

As we see, overall the red channel image is a bit brighter than the other 2 channels, meaning the 2D array for red has higher values than the 2D arrays for blue and green. It happened as the face and dress of Sefuda (the guy in the photo) are kind of “reddish”.

Multispectral Image: They are similar to RGB image but include additional bands, which often are not perceivable by human eyes. For example, some multispectral images include 4 channels (or bands), Red, Green, Blue, and NIR (Near-Infrared). Although NIR cannot be seen by human eyes, the data can be processed with machine and/or statistical analyses to understand different phenomena (e.g., crop disease detection).

Array Shape:

Similar to RGB — 3D (Height x Width x Channel), with the difference that Channel > 3.

Python Code to Read and Visualize:

# Need to install numpy, matplotlib, and rasterio using the following command in the terminal:
# pip install numpy matplotlib rasterio
import rasterio
from rasterio.plot import show
import matplotlib.pyplot as plt
import numpy as np

file_path = 'multispectral.tif'
with rasterio.open(file_path) as src:
print("Number of Channels",src.count)
blue_channel = src.read(1)
green_channel = src.read(2)
red_channel = src.read(3)
nir_channel = src.read(4)

show(blue_channel, cmap='gray')
show(green_channel, cmap='gray')
show(red_channel, cmap='gray')
show(nir_channel, cmap='gray')

rgb_image = np.array([red_channel, green_channel, blue_channel])
rgb_image = rgb_image/np.max(rgb_image) #Normalize betwen 0 and 1
show(rgb_image)

The code above shows each channel in grayscale separately. In addition, it shows an RGB visualization of the multispectral image (how the image would look like if it was taken with a typical digital camera instead of a multispectral camera).

Note that we cannot visualize the original image as our eyes cannot see the Near-Infrared light. However, from the grayscale visualization of the Near-Infrared band data, we understand that the Near-Infrared light can capture some information or physical features that are not available in any of Red, Green, or Blue band data. I hope you are starting to understand why and how multispectral images can be valuable!

Visualizing multispectral image

Hyperspectral Image: Hyperspectral images, similar to multispectral images, include additional bands, but usually, they consist of many more bands compared to multispectral images. Multispectral devices typically include 4–15 bands, while hyperspectral devices can have 50, 100, or even thousands of bands.

The python code above that reads and visualizes a multispectral image can be used for reading and visualizing hyperspectral images only by locating the right band indices in the source (i.e., src.read(index)).

Bye bye, guys! Like, comment, share, press the bell icon, and subscribe to my channel (I don’t have any at this moment though).

--

--