equalizeHist()函数
void cv::equalizeHist ( InputArray src,
OutputArray dst
)
该函数只支持但通道的灰度图均衡化,所以对于彩色图像来说可以先将多通道分离成单通道,然后再合并成多通道
通道分离
void cv::split(inputArray, src //多通道数组
vector<>channels //把多个数组放到vector容器中
)//图像通道分离
通道合并
void cv::merge(vector<>channels //待混合的容器
inputArray, dst ) //混合后的数组
代码演示
#include <iostream>
#include <string>
#include<cctype>
#include <fstream>
#include<vector>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
using namespace std;
int main()
{
Mat m = imread("D:\\My code\\Python\\opencv\\study\\01.jpg");
vector<Mat> channels;//定义存储的容器
split(m, channels);
Mat dst;
Mat bluechannel = channels[0];//b通道的图像
equalizeHist(bluechannel, bluechannel);//均衡化
Mat greenchannel = channels[1];//g通道的图像
equalizeHist(greenchannel, greenchannel);
Mat redchannel = channels[2];//r通道的图像
equalizeHist(redchannel, redchannel);
merge(channels, dst);//合并通道
imshow("before",m);
imshow("after",dst);
waitKey(0);
system("pause");
}
运行效果
处理之前的图片
处理之后的图像
小结
灰度图像均值化主要用于增强图像的对比度,特别在图像对比度不好时,可以先增强对比度后再对图像做其他处理。