How to Calculate Image Histogram in MATLAB

Ammar Ali Feb 02, 2024
How to Calculate Image Histogram in MATLAB

This tutorial will discuss calculating an image’s histogram using the imhist() function in Matlab.

Calculate an Image’s Histogram Using the imhist() Function in MATLAB

Image histogram shows the graphical representation of pixel color distribution present in a digital image. An image contains pixels that contain color values. In a histogram, we show the color values present inside an image to check the distribution of colors in an image.

For example, we can check which color is present in abundance and which color is rarely present in an image. We can also spot noise, clipping values, and background at a glance using the image histogram.

We can use Matlab’s built-in function imhist() to calculate an image’s histogram. First, we must read the image using the imread() function and then pass it inside the imhist() function. The imhist() function will return the histogram count and location of bins, and also it will plot the histogram.

For example, let’s use the imhist() function to calculate and plot an image’s histogram. See the code below.

I = imread('cat.jpg');
subplot(1,2,1)
imshow(I)
subplot(1,2,2)
imhist(I)

Output:

image histogram

The histogram shows that black color is present in abundance in the image. We can also specify the number of bins as a second argument used to calculate the histogram. The number of bins should be a positive integer.

The input image is a numeric array of any dimension. If the image is of class single or double, the pixel values should be in the range of 0 to 1. If the values are not in the range, we can use the rescale() function to rescale the values to the required range.

We can also pass a color map inside the imhist() function in the case of an indexed image which will be used to calculate the image’s histogram.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook

Related Article - MATLAB Image