Last updated on 2023-12-08 | Edit this page
- How can we create grayscale and colour histograms to understand the distribution of colour values in an image?
- Explain what a histogram is.
- Load an image in grayscale format.
- Create and display grayscale and colour histograms for entire images.
- Create and display grayscale and colour histograms for certain areas of images, via masks.
In this episode, we will learn how to use scikit-image functions to create and display histograms for images.
import imageio.v3 as iio import ipympl import matplotlib.pyplot as plt import numpy as np import skimage as ski %matplotlib widget
As it pertains to images, a histogram is a graphical representation showing how frequently various colour values occur in the image. We saw in the Image Basics episode that we could use a histogram to visualise the differences in uncompressed and compressed image formats. If your project involves detecting colour changes between images, histograms will prove to be very useful, and histograms are also quite handy as a preparatory step before performing thresholding.
We will start with grayscale images, and then move on to colour images. We will use this image of a plant seedling as an example:
Here we load the image in grayscale instead of full colour, and display it:
# read the image of a plant seedling as grayscale from the outset = iio.imread(uri="data/plant-seedling.jpg", mode="L") plant_seedling # convert the image to float dtype with a value range from 0 to 1 = ski.util.img_as_float(plant_seedling) plant_seedling # display the image = plt.subplots() fig, ax ="gray")plt.imshow(plant_seedling, cmap
Again, we use the
iio.imread() function to load our
image. Then, we convert the grayscale image of integer dtype, with 0-255
range, into a floating-point one with 0-1 range, by calling the function
ski.util.img_as_float. We can also calculate histograms for
8 bit images as we will see in the subsequent exercises.
We now use the function
np.histogram to compute the
histogram of our image which, after all, is a NumPy array:
# create the histogram = np.histogram(plant_seedling, bins=256, range=(0, 1))histogram, bin_edges
bins determines the number of “bins” to
use for the histogram. We pass in
256 because we want to
see the pixel count for each of the 256 possible values in the grayscale
range is the range of values each of the
pixels in the image can have. Here, we pass 0 and 1, which is the value
range of our input image after conversion to floating-point.
The first output of the
np.histogram function is a
one-dimensional NumPy array, with 256 rows and one column, representing
the number of pixels with the intensity value corresponding to the
index. I.e., the first number in the array is the number of pixels found
with intensity value 0, and the final number in the array is the number
of pixels found with intensity value 255. The second output of
np.histogram is an array with the bin edges and one column
and 257 rows (one more than the histogram itself). There are no gaps
between the bins, which means that the end of the first bin, is the
start of the second and so on. For the last bin, the array also has to
contain the stop, so it has one more element, than the histogram.
Next, we turn our attention to displaying the histogram, by taking advantage of the plotting facilities of the Matplotlib library.
# configure and draw the histogram figure plt.figure()"Grayscale Histogram") plt.title("grayscale value") plt.xlabel("pixel count") plt.ylabel(0.0, 1.0]) # <- named arguments do not work here plt.xlim([ 0:-1], histogram) # <- or hereplt.plot(bin_edges[
We create the plot with
plt.figure(), then label the
figure and the coordinate axes with
plt.ylabel() functions. The
last step in the preparation of the figure is to set the limits on the
values on the x-axis with the
plt.xlim([0.0, 1.0]) function
Note that we cannot used named parameters for the
plt.plot() functions. This is
because these functions are defined to take an arbitrary number of
unnamed arguments. The designers wrote the functions this way
because they are very versatile, and creating named parameters for all
of the possible ways to use them would be complicated.
Finally, we create the histogram plot itself with
plt.plot(bin_edges[0:-1], histogram). We use the
left bin edges as x-positions for the histogram values
by indexing the
bin_edges array to ignore the last value
(the right edge of the last bin). When we run the
program on this image of a plant seedling, it produces this
Matplotlib provides a dedicated function to compute and display
plt.hist(). We will not use it in this lesson
in order to understand how to calculate histograms in more detail. In
practice, it is a good idea to use this function, because it visualises
histograms more appropriately than
plt.plot(). Here, you
could use it by calling
plt.hist(image.flatten(), bins=256, range=(0, 1)) instead
*.flatten() is a NumPy function that converts our
two-dimensional image into a one-dimensional array).
Looking at the histogram above, you will notice that there is a large number of very dark pixels, as indicated in the chart by the spike around the grayscale value 0.12. That is not so surprising, since the original image is mostly black background. What if we want to focus more closely on the leaf of the seedling? That is where a mask enters the picture!
First, hover over the plant seedling image with your mouse to determine the (x, y) coordinates of a bounding box around the leaf of the seedling. Then, using techniques from the Drawing and Bitwise Operations episode, create a mask with a white rectangle covering that bounding box.
After you have created the mask, apply it to the input image before
passing it to the
# read the image as grayscale from the outset = iio.imread(uri="data/plant-seedling.jpg", mode="L") plant_seedling # convert the image to float dtype with a value range from 0 to 1 = ski.util.img_as_float(plant_seedling) plant_seedling # display the image = plt.subplots() fig, ax ="gray") plt.imshow(plant_seedling, cmap # create mask here, using np.zeros() and ski.draw.rectangle() = np.zeros(shape=plant_seedling.shape, dtype="bool") mask = ski.draw.rectangle(start=(199, 410), end=(384, 485)) rr, cc = True mask[rr, cc] # display the mask = plt.subplots() fig, ax ="gray") plt.imshow(mask, cmap # mask the image and create the new histogram = np.histogram(plant_seedling[mask], bins=256, range=(0.0, 1.0)) histogram, bin_edges # configure and draw the histogram figure plt.figure() "Grayscale Histogram") plt.title("grayscale value") plt.xlabel("pixel count") plt.ylabel(0.0, 1.0]) plt.xlim([0:-1], histogram)plt.plot(bin_edges[
Your histogram of the masked area should look something like this:
We can also create histograms for full colour images, in addition to grayscale histograms. We have seen colour histograms before, in the Image Basics episode. A program to create colour histograms starts in a familiar way:
# read original image, in full color = iio.imread(uri="data/plant-seedling.jpg") plant_seedling # display the image = plt.subplots() fig, ax plt.imshow(plant_seedling)
We read the original image, now in full colour, and display it.
Next, we create the histogram, by calling the
np.histogram function three times, once for each of the
channels. We obtain the individual channels, by slicing the image along
the last axis. For example, we can obtain the red colour channel by
r_chan = image[:, :, 0].
# tuple to select colors of each channel line = ("red", "green", "blue") colors # create the histogram plot, with three lines, one for # each color plt.figure()0, 256]) plt.xlim([for channel_id, color in enumerate(colors): = np.histogram( histogram, bin_edges =256, range=(0, 256) plant_seedling[:, :, channel_id], bins )0:-1], histogram, color=color) plt.plot(bin_edges[ "Color Histogram") plt.title("Color value") plt.xlabel("Pixel count")plt.ylabel(
We will draw the histogram line for each channel in a different colour, and so we create a tuple of the colours to use for the three lines with the
colors = ("red", "green", "blue")
line of code. Then, we limit the range of the x-axis with the
plt.xlim() function call.
Next, we use the
for control structure to iterate
through the three channels, plotting an appropriately-coloured histogram
line for each. This may be new Python syntax for you, so we will take a
moment to discuss what is happening in the
The Python built-in
enumerate() function takes a list
and returns an iterator of tuples, where the first
element of the tuple is the index and the second element is the element
of the list.
In Python, an iterator, or an iterable object, is
something that can be iterated over with the
structure. A tuple is a sequence of objects, just like a list.
However, a tuple cannot be changed, and a tuple is indicated by
parentheses instead of square brackets. The
function takes an iterable object, and returns an iterator of tuples
consisting of the 0-based index and the corresponding object.
For example, consider this small Python program:
list = ("a", "b", "c", "d", "e") for x in enumerate(list): print(x)
Executing this program would produce the following output:
(0, 'a') (1, 'b') (2, 'c') (3, 'd') (4, 'e')
In our colour histogram program, we are using a tuple,
(channel_id, color), as the
for variable. The
first time through the loop, the
channel_id variable takes
0, referring to the position of the red colour
channel, and the
color variable contains the string
"red". The second time through the loop the values are the
green channels index
"green", and the
third time they are the blue channel index
for loop, our code looks much like it did for
the grayscale example. We calculate the histogram for the current
channel with the
histogram, bin_edges = np.histogram(image[:, :, channel_id], bins=256, range=(0, 256))
function call, and then add a histogram line of the correct colour to the plot with the
plt.plot(bin_edges[0:-1], histogram, color=color)
function call. Note the use of our loop variables,
Finally we label our axes and display the histogram, shown here:
We can also apply a mask to the images we apply the colour histogram process to, in the same way we did for grayscale histograms. Consider this image of a well plate, where various chemical sensors have been applied to water and various concentrations of hydrochloric acid and sodium hydroxide:
# read the image = iio.imread(uri="data/wellplate-02.tif") wellplate # display the image = plt.subplots() fig, ax plt.imshow(wellplate)
Suppose we are interested in the colour histogram of one of the sensors in the well plate image, specifically, the seventh well from the left in the topmost row, which shows Erythrosin B reacting with water.
Hover over the image with your mouse to find the centre of that well and the radius (in pixels) of the well. Then create a circular mask to select only the desired well. Then, use that mask to apply the colour histogram operation to that well.
Your masked image should look like this:
And, the program should produce a colour histogram that looks like this:
# create a circular mask to select the 7th well in the first row = np.zeros(shape=wellplate.shape[0:2], dtype="bool") mask = ski.draw.disk(center=(240, 1053), radius=49, shape=wellplate.shape[0:2]) circle = 1 mask[circle] # just for display: # make a copy of the image, call it masked_image, and # zero values where mask is False = np.array(wellplate) masked_img ~mask] = 0 masked_img[ # create a new figure and display masked_img, to verify the # validity of your mask = plt.subplots() fig, ax plt.imshow(masked_img) # list to select colors of each channel line = ("red", "green", "blue") colors # create the histogram plot, with three lines, one for # each color plt.figure()0, 256]) plt.xlim([for (channel_id, color) in enumerate(colors): # use your circular mask to apply the histogram # operation to the 7th well of the first row = np.histogram( histogram, bin_edges =256, range=(0, 256) wellplate[:, :, channel_id][mask], bins ) =color) plt.plot(histogram, color "color value") plt.xlabel("pixel count")plt.ylabel(
- In many cases, we can load images in grayscale by passing the
mode="L"argument to the
- We can create histograms of images with the
- We can separate the RGB channels of an image using slicing operations.
- We can display histograms using the