OpenCV — Histogram Equalization

HOW TO Equalize Histograms Of Images — #PyVisionSeries — Episode #04

J3
Jungletronics
5 min readOct 7, 2022

--

What is an Image Histogram?

It is a graphical representation of the intensity distribution of an image. It quantifies the number of pixels for each intensity value considered.

01 step — Equalizing Manually

Displaying the color image

Convert image into a numpy array, so OpenCV can work with:

Converting RGB to Gray Scale:

display the image:

In gray tones now we have how to work the histogram…

Flatting it out:

show the histogram

Note that the grayscale values ​​are badly distributed around some value…

What is Histogram Equalization?

To make it clearer, from the image above, you can see that the pixels seem clustered around the middle of the available range of intensities. What Histogram Equalization does is to stretch out this range.

Casting:

Get CDF:

How does it work?

Equalization implies mapping one distribution (the given histogram) to another distribution (a wider and more uniform distribution of intensity values) so the intensity values are spread over the whole range.

Check it out:

Using OpenCV equalizeHist(img) Method

02 step — And by using the OpenCV function

Ready! that’s all you need to do!

👉Jupiter notebook link :)

👉Github (EX_04)

Credits & References:

Jose Portilla — Python for Computer Vision with OpenCV and Deep Learning — Learn the latest techniques in computer vision with Python, OpenCV, and Deep Learning!

https://www.tutorialspoint.com/dip/histogram_equalization.htm

https://medium.com/@kyawsawhtoon/a-tutorial-to-histogram-equalization-497600f270e2

https://docs.opencv.org/3.4/d4/d1b/tutorial_histogram_equalization.html

https://docs.opencv.org/4.x/d4/d86/group__imgproc__filter.html

https://stackoverflow.com/questions/32838802/numpy-with-python-convert-3d-array-to-2d

https://techtutorialsx.com/2018/06/02/python-opencv-converting-an-image-to-gray-scale/

https://docs.opencv.org/3.4/d4/d1b/tutorial_histogram_equalization.html

https://docs.opencv.org/4.x/d5/daf/tutorial_py_histogram_equalization.html

https://stackoverflow.com/questions/61583991/opencv-python-error-unsupported-data-type-4-in-function-cvopt-avx2getmo

https://ai.stanford.edu/~syyeung/cvweb/tutorial1.html

https://medium.com/hackernoon/histogram-equalization-in-python-from-scratch-ebb9c8aa3f23

Posts Related:

00 Episode#Hi Python Computer Vision — PIL! — An Intro To Python Imaging Library #PyVisionSeries

01 Episode# Jupyter-lab — Python — OpenCV — Image Processing Exercises #PyVisionSeries

02 Episode# OpenCV — Image Basics — Create Image From Scratch #PyVisionSeries

03 Episode# OpenCV — Morphological Operations — How To Erode, Dilate, Edge Detect w/ Gradient #PyVisionSeries

04 Episode# OpenCV — Histogram Equalization — HOW TO Equalize Histograms Of Images — #PyVisionSeries

05 Episode# OpenCV — OpenCV — Resize an Image — How To Resize Without Distortion

07 Episode# YOLO — Object Detection — The state of the art in object detection Framework!

08 Episode# OpenCV — HaashCascate — Object Detection — Viola–Jones object detection framework — #PyVisionSeries

--

--

J3
Jungletronics

😎 Gilberto Oliveira Jr | 🖥️ Computer Engineer | 🐍 Python | 🧩 C | 💎 Rails | 🤖 AI & IoT | ✍️