Reader small image

You're reading from  Mastering OpenCV 4 with Python

Product typeBook
Published inMar 2019
Reading LevelExpert
PublisherPackt
ISBN-139781789344912
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Alberto Fernández Villán
Alberto Fernández Villán
author image
Alberto Fernández Villán

Alberto Fernndez Villn is a software engineer with more than 12 years of experience in developing innovative solutions. In the last couple of years, he has been working in various projects related to monitoring systems for industrial plants, applying both Internet of Things (IoT) and big data technologies. He has a Ph.D. in computer vision (2017), a deep learning certification (2018), and several publications in connection with computer vision and machine learning in journals such as Machine Vision and Applications, IEEE Transactions on Industrial Informatics, Sensors, IEEE Transactions on Industry Applications, IEEE Latin America Transactions, and more. As of 2013, he is a registered and active user (albertofernandez) on the Q&A OpenCV forum.
Read more about Alberto Fernández Villán

Right arrow

Constructing and Building Histograms

Histograms are a powerful technique used to better understand image content. For example, many cameras display in real time the histogram of the scene that is being captured in order to adjust some parameters of the camera acquisition (for example, exposure time, brightness, or contrast) with the purpose of capturing appropriate images and helping detect image acquisition issues.

In this chapter, you will see how to create and understand histograms.

This chapter will tackle the main concepts concerning histograms, and the following topics will be covered:

  • A theoretical introduction to histograms
  • Grayscale histograms
  • Color histograms
  • Custom visualizations of histograms
  • Comparing OpenCV, NumPy, and Matplotlib histograms
  • Histogram equalization
  • Adaptive histogram equalization
  • Comparing CLAHE and histogram equalization
  • Histogram comparison

...

Technical requirements

The technical requirements are as follows:

  • Python and OpenCV
  • Python-specific IDE
  • NumPy and Matplotlib packages
  • Git client

Further details about how to install these requirements are discussed in Chapter 1, Setting Up OpenCV. The GitHub repository for Mastering OpenCV 4 with Python, containing all the supporting project files necessary to work through the book from the first chapter to the last one, can be accessed at this URL: https://github.com/PacktPublishing/Mastering-OpenCV-4-with-Python.

A theoretical introduction to histograms

An image histogram is a type of histogram that reflects the tonal distribution of the image, plotting the number of pixels for each tonal value. The number of pixels for each tonal value is also called frequency. Therefore, a histogram for a grayscale image with intensity values in the range [0, K-1] would contain exactly K entries. For example, in the case of 8-bit grayscale images, K = 256 (28 = 256), and hence, the intensity values are in the range [0, 255]. Each entry of the histogram is defined as follows:

For example, h(80) = number of pixels with intensity 80.

In the next screenshot, you can see that the image (left) has 7 distinct gray levels. The gray levels are: 30, 60, 90, 120, 150, 180 and 210. The histogram (right) shows how many times (frequency) each tonal value appears in the image. In this case, as each region is 50 x...

Grayscale histograms

OpenCV provides the cv2.calcHist() function in order to calculate the histogram of one or more arrays. Therefore, this function can be applied to single-channel images (for example, grayscale images) and to multi-channel images (for example, BGR images).

In this section, we are going to see how to calculate histograms for grayscale images. The signature for this function is as follows:

cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]])

To this, the following applies:

  • images: It represents the source image of type uint8 or float32 provided as a list (example, [gray_img]).
  • channels: It represents the index of the channel for which we calculate the histogram provided as a list (for example, [0] for grayscale images, or [0],[1],[2] for multi-channel images to calculate the histogram for the first, second, or third channel, respectively...

Color histograms

In this section, we will see how to calculate color histograms. The script that performs this functionality is color_histogram.py. In the case of a multi-channel image (for example, a BGR image), the process of calculating the color histogram involves calculating the histogram in each of the channels. In this case, we have created a function to calculate the histogram from a three-channel image:

def hist_color_img(img):
"""Calculates the histogram from a three-channel image"""

histr = []
histr.append(cv2.calcHist([img], [0], None, [256], [0, 256]))
histr.append(cv2.calcHist([img], [1], None, [256], [0, 256]))
histr.append(cv2.calcHist([img], [2], None, [256], [0, 256]))
return histr

It should be noted that we could have created a for loop or a similar approach in order to call the cv2.calcHist() function three times...

Custom visualizations of histograms

In order to visualize histograms, we have made use of the plt.plot() function. If we want to visualize a histogram by using only OpenCV capabilities, there is no OpenCV function to draw histograms. In this case, we have to make use of OpenCV primitives (for example, cv2.polylines() and cv2.rectangle(), among others) to create some (basic) functionality for plotting histograms. In the histogram_custom_visualization.py script, we have created the plot_hist() function, which performs this functionality. This function creates a BGR color image, plotting the histogram in it. The code for this function is as follows:

def plot_hist(hist_items, color):
"""Plots the histogram of a image"""

# For visualization purposes we add some offset:
offset_down = 10
offset_up = 10

# This will be used for creating the...

Comparing OpenCV, NumPy, and Matplotlib histograms

We have already seen that OpenCV provides the cv2.calcHist() function to calculate histograms. Additionally, NumPy and Matplotlib offer similar functions for the creation of histograms. In the comparing_opencv_numpy_mpl_hist.py script, we are comparing these functions for performance purposes. In this sense, we are going to see how to create histograms with OpenCV, NumPy, and Matplotlib, and then measure the execution time for each one and plot the results in a figure.

With the purpose of measuring the execution time, we are using timeit.default_timer because it provides the best clock available on your platform and version of Python automatically. In this way, we import it at the beginning of the script:

from timeit import default_timer as timer

The way we use the timer is summarized here:

start = timer()
# ...
end = timer()
execution_time...

Histogram equalization

In this section, we will see how to perform histogram equalization using the OpenCV function, cv2.equalizeHist(), and how to apply it to both grayscale and color images. The cv2.equalizeHist() function normalizes the brightness and also increases the contrast of the image. Therefore, the histogram of the image is modified after applying this function. In the next subsections, we will explore both the original and the modified histogram in order to see how it is changed.

Grayscale histogram equalization

Using the cv2.equalizeHist() function with the purpose of equalizing the contrast of a given grayscale image is pretty easy:

image = cv2.imread('lenna.png')
gray_image = cv2.cvtColor(image, cv2...

Contrast Limited Adaptive Histogram Equalization

In this section, we are going to see how to apply contrast limited adaptive histogram equalization (CLAHE) to equalize images, which is a variant of adaptive histogram equalization (AHE), in which contrast amplification is limited. The noise in relatively homogeneous regions of the image is overamplified by AHE, while CLAHE tackles this problem by limiting the contrast amplification. This algorithm can be applied to improve the contrast of images. This algorithm works by creating several histograms of the original image, and uses all of these histograms to redistribute the lightness of the image.

In the clahe_histogram_equalization.py script, we are applying CLAHE to both grayscale and color images. When applying CLAHE, there are two parameters to tune. The first one is clipLimit, which sets the threshold for contrast limiting...

Comparing CLAHE and histogram equalization

For the sake of completeness, in the comparing_hist_equalization_clahe.py script, you can see how both CLAHE and histogram equalization (cv2.equalizeHist()) work on the same image, visualizing both the resulting image and the resulting histogram.

This can be seen in the following screenshot:

It is safe to say that CLAHE gives better results and performance than applying histogram equalization in many situations. In this sense, CLAHE is commonly used as the first step in many computer vision applications (for example, face processing, among others).

Histogram comparison

One interesting functionality offered by OpenCV in connection with histograms is the cv2.compareHist() function, which can be used to get a numerical parameter expressing how well two histograms match each other. In this sense, as histograms reflect the intensity distributions of the pixel values in the image, this function can be used to compare images. As previously commented, the histograms show only statistical information and not the location of pixels. Therefore, a common approach for image comparison is to divide the image into a certain number of regions (commonly with the same size), calculate the histogram for each region, and, finally, concatenate all the histograms to create the feature representation of the image. In this example, for simplicity, we are not going to divide the image into a certain number of regions, so only one region (the full...

Summary

In this chapter, all the main concepts related to histograms have been reviewed. In this sense, we have seen what a histogram represents and how it can be calculated by using OpenCV, NumPy, and Matplotlib functions. Additionally, we have seen the difference between grayscale and color histograms, showing how to calculate and show both types. Histogram equalization is also an important factor when working with histograms, and we have seen how to perform histogram equalization to both grayscale and color images. Finally, a histogram comparison can also be very helpful in order to perform an image comparison. We have seen the four metrics OpenCV provides to measure the similarity between two histograms.

In connection with the next chapter, the main thresholding techniques (simple thresholding, adaptive thresholding, and Otsu's thresholding, among others) will be covered...

Questions

  1. What is an image histogram?
  2. Calculate the histogram of a grayscale image using 64 bins.
  3. Add 50 to every pixel on a grayscale image (the result will look lighter) and calculate the histogram.
  4. Calculate the red channel histogram of a BGR image without a mask.
  5. What functions do OpenCV, NumPy, and Matplotlib provide for calculating histograms?
  6. Modify the grayscale_histogram.py script to compute the brightness of these three images (gray_image, added_image, and subtracted_image). Rename the script to grayscale_histogram_brightness.py.
  7. Modify the comparing_hist_equalization_clahe.py script to show the execution time of both cv2.equalizeHist() and CLAHE. Rename it to comparing_hist_equalization_clahe_time.py.

Further reading

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering OpenCV 4 with Python
Published in: Mar 2019Publisher: PacktISBN-13: 9781789344912
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Alberto Fernández Villán

Alberto Fernndez Villn is a software engineer with more than 12 years of experience in developing innovative solutions. In the last couple of years, he has been working in various projects related to monitoring systems for industrial plants, applying both Internet of Things (IoT) and big data technologies. He has a Ph.D. in computer vision (2017), a deep learning certification (2018), and several publications in connection with computer vision and machine learning in journals such as Machine Vision and Applications, IEEE Transactions on Industrial Informatics, Sensors, IEEE Transactions on Industry Applications, IEEE Latin America Transactions, and more. As of 2013, he is a registered and active user (albertofernandez) on the Q&A OpenCV forum.
Read more about Alberto Fernández Villán