Vincent GODARD - V1- 30/01/2024
Freely inspired by :
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/
Compressed file to download => here.
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.
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).
# 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)
## 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)
## 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
## 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
## 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.
## Reading the color image
image = skimage.io.imread('data/how87tm234.jpg')
## Display image
fig, ax = plt.subplots()
plt.imshow(image)
plt.show()
## Creation of "tuples" to select colors for each histogram curve
colors = ("red", "green", "blue")
channel_ids = (0, 1, 2)
## 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.
Could you display the histogram of this true color CC?