Histogram Equalization
Table of Contents
Prev Tutorial: Affine Transformations
Next Tutorial: Histogram Calculation
Prev Tutorial: Affine Transformations
Next Tutorial: Histogram Calculation
| Original author | Ana Huamán |
| Compatibility | OpenCV >= 3.0 |
In this tutorial you will learn:
To accomplish the equalization effect, the remapping should be the cumulative distribution function (cdf) (more details, refer to Learning OpenCV). For the histogram \(H(i)\), its cumulative distribution \(H^{'}(i)\) is:
To use this as a remapping function, we have to normalize \(H^{'}(i)\) such that the maximum value is 255 ( or the maximum value for the intensity of the image ). From the example above, the cumulative function is:
Finally, we use a simple remapping procedure to obtain the intensity values of the equalized image:
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
CommandLineParser parser( argc, argv, "{@input | lena.jpg | input image}" );
Mat src = imread( samples::findFile( parser.get<String>( "@input" ) ), IMREAD_COLOR );
if( src.empty() )
{
cout << "Could not open or find the image!\n" << endl;
cout << "Usage: " << argv[0] << " <Input image>" << endl;
return -1;
}
cvtColor( src, src, COLOR_BGR2GRAY );
Mat dst;
equalizeHist( src, dst );
imshow( "Source image", src );
imshow( "Equalized Image", dst );
waitKey();
return 0;
}import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
class EqualizeHist {
public void run(String[] args) {
String filename = args.length > 0 ? args[0] : "../data/lena.jpg";
Mat src = Imgcodecs.imread(filename);
if (src.empty()) {
System.err.println("Cannot read image: " + filename);
System.exit(0);
}
Imgproc.cvtColor(src, src, Imgproc.COLOR_BGR2GRAY);
Mat dst = new Mat();
Imgproc.equalizeHist( src, dst );
HighGui.imshow( "Source image", src );
HighGui.imshow( "Equalized Image", dst );
HighGui.waitKey(0);
System.exit(0);
}
}
public class EqualizeHistDemo {
public static void main(String[] args) {
// Load the native OpenCV library
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
new EqualizeHist().run(args);
}
}from __future__ import print_function
import cv2 as cv
import argparse
parser = argparse.ArgumentParser(description='Code for Histogram Equalization tutorial.')
parser.add_argument('--input', help='Path to input image.', default='lena.jpg')
args = parser.parse_args()
src = cv.imread(cv.samples.findFile(args.input))
if src is None:
print('Could not open or find the image:', args.input)
exit(0)
src = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
dst = cv.equalizeHist(src)
cv.imshow('Source image', src)
cv.imshow('Equalized Image', dst)
cv.waitKey()
Load the source image:
CommandLineParser parser( argc, argv, "{@input | lena.jpg | input image}" );
Mat src = imread( samples::findFile( parser.get<String>( "@input" ) ), IMREAD_COLOR );
if( src.empty() )
{
cout << "Could not open or find the image!\n" << endl;
cout << "Usage: " << argv[0] << " <Input image>" << endl;
return -1;
} String filename = args.length > 0 ? args[0] : "../data/lena.jpg";
Mat src = Imgcodecs.imread(filename);
if (src.empty()) {
System.err.println("Cannot read image: " + filename);
System.exit(0);
}parser = argparse.ArgumentParser(description='Code for Histogram Equalization tutorial.')
parser.add_argument('--input', help='Path to input image.', default='lena.jpg')
args = parser.parse_args()
src = cv.imread(cv.samples.findFile(args.input))
if src is None:
print('Could not open or find the image:', args.input)
exit(0)Convert it to grayscale:
cvtColor( src, src, COLOR_BGR2GRAY ); Imgproc.cvtColor(src, src, Imgproc.COLOR_BGR2GRAY);src = cv.cvtColor(src, cv.COLOR_BGR2GRAY)Apply histogram equalization with the function cv::equalizeHist :
Mat dst;
equalizeHist( src, dst ); Mat dst = new Mat();
Imgproc.equalizeHist( src, dst );dst = cv.equalizeHist(src)As it can be easily seen, the only arguments are the original image and the output (equalized) image.
Display both images (original and equalized):
imshow( "Source image", src );
imshow( "Equalized Image", dst ); HighGui.imshow( "Source image", src );
HighGui.imshow( "Equalized Image", dst );cv.imshow('Source image', src)
cv.imshow('Equalized Image', dst)Wait until user exists the program
waitKey(); HighGui.waitKey(0);cv.waitKey()
which, by the way, has this histogram:
notice that the pixels are clustered around the center of the histogram.
this image has certainly more contrast. Check out its new histogram like this:
Notice how the number of pixels is more distributed through the intensity range.