首页 > 代码库 > OpenCV Tutorials —— Histogram Equalization

OpenCV Tutorials —— Histogram Equalization

直方图均衡化 —— 其潜在的数学原理是一个分布(输入的亮度直方图)被映射到另一个分布

其目的是拉伸原始图像直方图,增强其对比度

../../../../../_images/Histogram_Equalization_Theory_0.jpg

../../../../../_images/Histogram_Equalization_Theory_1.jpg

 

  • 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:

    H^{'}(i) = \sum_{0 \le j < i} H(j) 累计分布函数作为映射函数,计算的过程中需要归一化直方图

    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:

    ../../../../../_images/Histogram_Equalization_Theory_2.jpg

  • Finally, we use a simple remapping procedure to obtain the intensity values of the equalized image:

    equalized( x, y ) = H^{'}( src(x,y) )

 

 

Code

 

#include "stdafx.h"#include "opencv2/highgui/highgui.hpp"#include "opencv2/imgproc/imgproc.hpp"#include <iostream>#include <stdio.h>using namespace cv;using namespace std;/**  @function main */int main( int argc, char** argv ){	Mat src, dst;	char* source_window = "Source image";	char* equalized_window = "Equalized Image";	/// Load image	src = http://www.mamicode.com/imread("test1.jpg", 1 );	if( !src.data )	{ cout<<"Usage: ./Histogram_Demo <path_to_image>"<<endl;	return -1;}	/// Convert to grayscale	cvtColor( src, src, CV_BGR2GRAY );	/// Apply Histogram Equalization	equalizeHist( src, dst );	// 全封装进去了 ~~	/// Display results	namedWindow( source_window, CV_WINDOW_AUTOSIZE );	namedWindow( equalized_window, CV_WINDOW_AUTOSIZE );	imshow( source_window, src );	imshow( equalized_window, dst );	/// Wait until user exits the program	waitKey(0);	return 0;}

 

直方图均衡化 —— 频谱被展开

对于彩色图像,必须先将每个通道分开,再分别进行处理

适用于直方图分布过于集中(对比不明显的)图像

OpenCV Tutorials —— Histogram Equalization