//ace 自适应对比度均衡研究
//by jsxyhelu
//感谢 imageshop
#include "stdafx.h"
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace std;
using namespace cv;
//点乘法 elementWiseMultiplication
cv::Mat EWM(cv::Mat m1,cv::Mat m2){
Mat dst=m1.mul(m2);
return dst;
}
//图像局部对比度增强算法
cv::Mat ACE(cv::Mat src,int C = 4,int n=20,int MaxCG = 5){
Mat meanMask;
Mat varMask;
Mat meanGlobal;
Mat varGlobal;
Mat dst;
Mat tmp;
Mat tmp2;
blur(src.clone(),meanMask,Size(50,50));//meanMask为局部均值
tmp = src - meanMask;
varMask = EWM(tmp,tmp);
blur(varMask,varMask,Size(50,50)); //varMask为局部方差
//换算成局部标准差
varMask.convertTo(varMask,CV_32F);
for (int i=0;i<varMask.rows;i++){
for (int j=0;j<varMask.cols;j++){
varMask.at<float>(i,j) = (float)sqrt(varMask.at<float>(i,j));
}
}
meanStdDev(src,meanGlobal,varGlobal); //meanGlobal为全局均值 varGlobal为全局标准差
tmp2 = varGlobal/varMask;
for (int i=0;i<tmp2.rows;i++){
for (int j=0;j<tmp2.cols;j++){
if (tmp2.at<float>(i,j)>MaxCG){
tmp2.at<float>(i,j) = MaxCG;
}
}
}
tmp2.convertTo(tmp2,CV_8U);
tmp2 = EWM(tmp2,tmp);
dst = meanMask + tmp2;
imshow("D方法",dst);
dst = meanMask + C*tmp;
imshow("C方法",dst);
return dst;
}
void main()
{
Mat src = imread("plant.bmp",0);
imshow("src",src);
ACE(src);
waitKey();
}