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

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Deploying Machine Learning Models with Flask for Beginners
In this MLOps on GCP project you will learn to deploy a sales forecasting ML Model using Flask.

Create Your First Chatbot with RASA NLU Model and Python
Learn the basic aspects of chatbot development and open source conversational AI RASA to create a simple AI powered chatbot on your own.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

Build Customer Propensity to Purchase Model in Python
In this machine learning project, you will learn to build a machine learning model to estimate customer propensity to purchase.

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

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.

Learn to Build a Polynomial Regression Model from Scratch
In this Machine Learning Regression project, you will learn to build a polynomial regression model to predict points scored by the sports team.