Explain image histograms How to equalize it in OpenCV

This recipe explains what are image histograms and how to equalize an image histogram in OpenCV. Image histograms are plots that display the frequency distribution and helps to visualize the color intensity distribution.

Recipe Objective: What are image histograms? How to equalize an image histogram in OpenCV?

In this recipe, we will understand what image histograms are and how to equalize an image histogram.

Step 1: Import the libraries and read the image.

Let us first import the necessary libraries and read the image. The image that we are using here is the one shown below. Let us read the image in grayscale this time for better results.

Input Image

import cv2
import numpy as np
from matplotlib import pyplot as plt
image = cv2.imread("flower_lb.jpg",0)

As we can see, the image is very dull and does not have that much brightness.

Step 2: Understanding image histograms and histogram equalization

Image histograms are plots that display the frequency distribution of different color intensities. Image histograms help visualize the color intensity distribution. For some images, there might be more pixels of the same or close color intensity. Or in other words, for some images, the image histogram might be skewed, which sometimes reduces the quality of the image. The image is either more bright or less bright than necessary. To correct this, we equalize the histogram, or in simple terms, we try to flatten the histogram. By doing so, the image's contrast is adjusted, and thus we get a better image.

Step 3: Implementing histogram equalization

It's effortless to equalize a histogram of an image. The function cv2.equalizeHist() does the job for us. We have to pass the skewed image as input. The function returns a new image with adjusted contrast.

eh = cv2.equalizeHist(image)

Step 4: Displaying the output

Let us display the output using matplotlib.

plt.figure(figsize=(13,5))
plt.subplot(1,2,1)
plt.imshow(image,cmap = 'gray')
plt.title('Original Image')
plt.xticks([])
plt.yticks([])

plt.subplot(1,2,2)
plt.imshow(eh,cmap = 'gray')
plt.title('Image with equalized histogram')
plt.xticks([]), plt.yticks([])

plt.show()
plt.tight_layout()

Output:

Histogram equalization

Download Materials

What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

BigMart Sales Prediction ML Project in Python
The goal of the BigMart Sales Prediction ML project is to build and evaluate different predictive models and determine the sales of each product at a store.

GCP MLOps Project to Deploy ARIMA Model using uWSGI Flask
Build an end-to-end MLOps Pipeline to deploy a Time Series ARIMA Model on GCP using uWSGI and Flask

Learn Object Tracking (SOT, MOT) using OpenCV and Python
Get Started with Object Tracking using OpenCV and Python - Learn to implement Multiple Instance Learning Tracker (MIL) algorithm, Generic Object Tracking Using Regression Networks Tracker (GOTURN) algorithm, Kernelized Correlation Filters Tracker (KCF) algorithm, Tracking, Learning, Detection Tracker (TLD) algorithm for single and multiple object tracking from various video clips.

Recommender System Machine Learning Project for Beginners-1
Recommender System Machine Learning Project for Beginners - Learn how to design, implement and train a rule-based recommender system in Python

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.

Build an AI Chatbot from Scratch using Keras Sequential Model
In this NLP Project, you will learn how to build an AI Chatbot from Scratch using Keras Sequential Model.

Azure Text Analytics for Medical Search Engine Deployment
Microsoft Azure Project - Use Azure text analytics cognitive service to deploy a machine learning model into Azure Databricks

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.