论文原文《Real-time adaptive contrast enhancement for imaging sensors》自适应局部对比度增强算法

算法原理

\quad 一张图片,总是由低频部分和高频部分构成的,低频部分可以由图像的低通滤波来得到,而高频部分可以由原图减去低频部分来得到。而本算法的目标是增强代表细节的高频部分,即是对高频部分乘上一个系数,然后重组得到增强的图像。所以ACE算法的核心就是高频部分增益系数的计算,一种方法诗将这个系数设为一个常数,第二种方法诗将增益表示为与方差相关的量。假设图像中的某个点表示为x(i, j),那么以(i, j)为中心,窗口大小为(2n+1)*(2n+1),其局部均值和方差为:
m x ( i , j ) = 1 ( 2 n + 1 ) 2 k = i n i + n l = j n j + n x ( k , l ) m_x(i, j) = \frac{1}{(2n+1)^2}\sum_{k=i-n}^{i+n}\sum_{l=j-n}^{j+n}x(k, l) mx(i,j)=(2n+1)21k=ini+nl=jnj+nx(k,l) σ x 2 ( i , j ) = 1 ( 2 n + 1 ) 2 k = i n i + n l = j n j + n [ x ( k , l ) m x ( i , j ) ] 2 \sigma_x^2(i, j)=\frac{1}{(2n+1)^2}\sum_{k=i-n}^{i+n}\sum_{l=j-n}^{j+n}[x(k, l)-m_x(i, j)]^2 σx2(i,j)=(2n+1)21k=ini+nl=jnj+n[x(k,l)mx(i,j)]2,均值 m x m_x mx可以近似认为是背景部分,那么 x m x-m xm就是高频部分,对高频部分有2种增益方法:
f ( i , j ) = m x ( i , j ) + C [ x ( i , j ) m x ( i , j ) ] f(i, j)=m_x(i, j)+C[x(i, j)-m_x(i, j)] f(i,j)=mx(i,j)+C[x(i,j)mx(i,j)],其中C是一个大于1的数,还有一种方法为 f ( i , j ) = m x ( i , j ) + D σ x ( i , j ) [ x ( i , j ) m x ( i , j ) ] f(i, j)=m_x(i, j)+\frac{D}{\sigma_x(i,j)}[x(i,j)-m_x(i,j)] f(i,j)=mx(i,j)+σx(i,j)D[x(i,j)mx(i,j)],在图像的高频区域,局部方差较大,此时增益值就较小,这样就不会出现过亮的情况。但是在图像平滑的区域,局部均方差很小,此时增益值较大,从而可能会方法噪声信号,所以需要对增益最大值做一定的限制,具体看代码。

代码

//自适应对比度增强算法,C表示对高频的直接增益系数,n表示滤波半径,maxCG表示对CG做最大值限制
Mat ACE(Mat src, int C = 3, int n = 3, float MaxCG = 7.5){
    int row = src.rows;
    int col = src.cols;
    Mat meanLocal; //图像局部均值
    Mat varLocal; //图像局部方差
    Mat meanGlobal; //全局均值
    Mat varGlobal; //全局标准差
    blur(src.clone(), meanLocal, Size(n, n));
    Mat highFreq = src - meanLocal;
    varLocal = highFreq.mul(highFreq);
    varLocal.convertTo(varLocal, CV_32F);
    for(int i = 0; i < row; i++){
        for(int j = 0; j < col; j++){
            varLocal.at<float>(i, j) = (float)sqrt(varLocal.at<float>(i, j));
        }
    }
    meanStdDev(src, meanGlobal, varGlobal);
    Mat gainArr = meanGlobal / varLocal; //增益系数矩阵
    for(int i = 0; i < row; i++){
        for(int j = 0; j < col; j++){
            if(gainArr.at<float>(i, j) > MaxCG){
                gainArr.at<float>(i, j) = MaxCG;
            }
        }
    }
    printf("%d %d\n", row, col);
    gainArr.convertTo(gainArr, CV_8U);
    gainArr = gainArr.mul(highFreq);
    Mat dst1 = meanLocal + gainArr;
    Mat dst2 = meanLocal + C * highFreq;
    return dst1;
}

int main(){
    Mat src = imread("../test.png");
    vector <Mat> now;
    split(src, now);
    int C = 150;
    int n = 5;
    float MaxCG = 3;
    Mat dst1 = ACE(now[0], C, n, MaxCG);
    Mat dst2 = ACE(now[1], C, n, MaxCG);
    Mat dst3 = ACE(now[2], C, n, MaxCG);
    now.clear();
    Mat dst;
    now.push_back(dst1);
    now.push_back(dst2);
    now.push_back(dst3);
    cv::merge(now, dst);
    imshow("origin", src);
    imshow("result", dst);
    imwrite("../result.jpg", dst);
    waitKey(0);
    return 0;
}

效果测试

int C = 150;
int n = 5;
float MaxCG = 3;
原图
结果
int C = 4;
int n = 50;
float MaxCG = 5;

原图
效果图效果图

参考博客

https://www.cnblogs.com/Imageshop/p/3324282.html