TD14: Display the histogram of a color composite¶

Vincent GODARD - V1- 30/01/2024

GIS and Remote Sensing - PhD curriculum¶

Debre Berhan University and University of Paris 8¶

Freely inspired by :

  • Creating Histograms https://datacarpentry.org/image-processing/05-creating-histograms.html#introduction-to-histograms

Sources :

  • Landsat Thematic Mapper (TM) data from September 10, 1987, West of Worcester, Massachusetts (from the https://clarklabs.org/download/)

  • scikit-image https://scikit-image.org/

  • matplotlib https://matplotlib.org/


Download required documents :

Compressed file to download => here.

1. Loading data and libraries¶

1.1. Loading data¶

Upload the following file from your storage area to the data directory (TD14/data) under this notebook is located (.ipynb):

○ "how87tm234.jpg";

○ "how87tm123.jpg".


This noteboock and the "data" directory will be at the same level in the tree, both under TD14.

1.2. Loading libraries¶

Some libraries are already preinstalled (e.g. Matplotlib). The Skimage library is not, and must be installed.

You then need to load them (Import function).

In [ ]:
# To install Skimage, delete the # (before the "!").
# !pip install scikit-image

# !pip install numpy
# !pip install scikit-image
# !pip install matplotlib
# !pip install ipympl # if version prior to Python 3.01.2, otherwise error message with widget (window gadget)
In [ ]:
## Loading libraries

import numpy as np
import skimage.color
import skimage.io
import matplotlib.pyplot as plt


#%matplotlib widget # if version prior to Python 3.01.2, otherwise error message with widget (window gadget)

2. Display of the color composite and its histogram in grayscale¶

2.1 Display of HOW87TM234 color composition (CC) in grayscale¶

In [ ]:
## Reading the false color composite of How Hill (MA) in grayscale

image = skimage.io.imread(fname='data/how87tm234.jpg', as_gray=True)


## Display image

fig, ax = plt.subplots()
plt.imshow(image, cmap='gray')
plt.show()

Why is it so dark?

Have a look at the histogram

2.2. Histogram of HOW87TM234 grayscale color composite (CC)¶

In [ ]:
## skimage.util.img_as_ubyte => to transform image into byte
## if data coded between o and 1 or more than 8 bit return to https://datacarpentry.org/image-processing/05-creating-histograms/ for further instructions
## how to use skimage.util.img => https://www.programcreek.com/python/example/125519/skimage.util.img_as_ubyte

image = skimage.util.img_as_ubyte(image)

histogram, bin_edges = np.histogram(image, bins=256, range=(0, 255))

# Reminder:
# 'histogram': table containing the frequencies of occurrences of each intensity value in the image
# 'bin_edges': edges of bins or intervals into which the data has been distributed

2.3. HOW87TM234 Color Composite (CC) Histogram Display in Grayscale¶

In [ ]:
## Histogram setting and plotting

plt.figure()
plt.title("Grayscale Histogram")
plt.xlabel("grayscale value")
plt.ylabel("pixel count")
plt.xlim([0, 255])  # to select only part of the range vary the limits

plt.plot(bin_edges[0:-1], histogram)
plt.show()

# Reminder:
# 'bin_edges[0:-1]': extracts all elements of the bin_edges list from the beginning to the penultimate element.

3. Display of the color composite and its color histogram¶

3.1. Display of color composite (CC) in color¶

In [ ]:
## Reading the color image

image = skimage.io.imread('data/how87tm234.jpg')

## Display image

fig, ax = plt.subplots()
plt.imshow(image)
plt.show()

3.2. Preparing to display color histogram bands¶

In [ ]:
## Creation of "tuples" to select colors for each histogram curve

colors = ("red", "green", "blue")
channel_ids = (0, 1, 2)

3.3. Color histogram display¶

In [ ]:
## Creation of the "plot", with one color per curve (those assigned to the display)

plt.figure()
plt.xlim([0, 256])
for channel_id, c in zip(channel_ids, colors):
    histogram, bin_edges = np.histogram(
        image[:, :, channel_id], bins=255, range=(0, 256)
    )
    plt.plot(bin_edges[0:-1], histogram, color=c)

plt.title("HOW87TM234, Color Histogram with TM4 (Red), TM3 (Green), TM2 (Blue)")
plt.xlabel("Color value")
plt.ylabel("Pixel count")

plt.show()

# Reminder:
# The loop for channel_id, c in zip(channel_ids, colors): iterates over the image channels.
# channel_ids is a list of channel identifiers, and colors is a list of colors associated with each channel.

4. Color composite display (CC) HOW87TM123¶

Could you display the histogram of this true color CC?

In [ ]:
 
In [ ]:
 
In [ ]: