matplotlib
  1. matplotlib-image-histograms

Image Histograms - ( Images in Matplotlib )

Heading h2

Syntax

import matplotlib.pyplot as plt

plt.hist(image.ravel(), bins = 256, range = [0,256])
plt.show()

Example

import cv2
import numpy as np
import matplotlib.pyplot as plt

image = cv2.imread('image.png',0)

plt.hist(image.ravel(), bins = 256, range = [0,256])
plt.show()

Output

A histogram plot of the image is displayed.

Explanation

An image histogram is a graph showing the distribution of pixel intensities in an image. In Matplotlib, we can use the hist() function from the pyplot module to create a histogram plot. We can pass the flattened image as input to the hist() function along with the number of bins and the range of the histogram.

In the above example, we first read an image using OpenCV and then pass it as input to the hist() function to plot the histogram of the image. We pass the flattened image as input, set the number of bins to 256 (which represents the maximum intensity value of 8-bit images), and set the range of the histogram to [0, 256].

Use

Image histograms are widely used in image processing and computer vision applications. They can help in analyzing the characteristics of an image, such as contrast, brightness, and overall intensity. Histogram equalization is a common technique used to improve the contrast of an image.

Important Points

  • An image histogram is a graph showing the distribution of pixel intensities in an image
  • The hist() function from pyplot module in Matplotlib can be used to create a histogram plot of an image
  • We can pass the flattened image as input, along with the number of bins and the range of the histogram
  • Image histograms are useful in analyzing image characteristics such as contrast, brightness, and overall intensity

Summary

In conclusion, image histograms are a powerful tool in analyzing the characteristics of an image. They can be created using the hist() function from pyplot module in Matplotlib. By analyzing the histogram of an image, we can gain insights into its intensity characteristics and take steps to improve its quality.

Published on: