首页 > 代码库 > OpenCV Tutorials —— Histogram Equalization
OpenCV Tutorials —— Histogram Equalization
直方图均衡化 —— 其潜在的数学原理是一个分布(输入的亮度直方图)被映射到另一个分布
其目的是拉伸原始图像直方图,增强其对比度
To accomplish the equalization effect, the remapping should be the cumulative distribution function (cdf) (more details, refer to Learning OpenCV). For the histogram , its cumulative distribution is:
累计分布函数作为映射函数,计算的过程中需要归一化直方图
To use this as a remapping function, we have to normalize 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:
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