本章实现两种对比度增加方法
1. 线性方法:x = αx + β
α变量控制对比度,β变量控制亮度
例如原始图片像素范围为[1, 10];当α=2时,像素范围变为[2,20],像素值的分布范围明显增大,对比度变强。
2. 非线性方法:x = (x / 255)gamma * 255
当gamma<1时,增强图像的整体亮度,会对低亮度的像素有明显的增强效果;
当gamma>1时,降低图像的整体亮度,会对高亮度的像素有明显的降低效果;
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using namespace cv;
int main(int argc, char* argv[])
{
Mat image = imread("lena.jpg");
Mat new_image = Mat::zeros(image.size(), image.type());
// linear method
double alpha = 2.2; // contrast control
int beta = 50; // brightness control
for(int y=0; y<image.rows; y++){
for(int x=0; x<image.cols; x++){
for(int c=0; c<image.channels(); c++){
new_image.at<Vec3b>(y,x)[c] = saturate_cast<uchar>(alpha*image.at<Vec3b>(y,x)[c] + beta);
}
}
}
imshow("original image", image);
imshow("new image", new_image);
// non linear method
double gamma=0.4;
Mat lookUpTable(1, 256, CV_8U);
uchar* p = lookUpTable.ptr();
for(int i=0; i<256; ++i)
p[i] = saturate_cast<uchar>(pow(i/255.0, gamma)*255.0);
Mat res = image.clone();
LUT(image, lookUpTable, res);
imshow("gamma correction", res);
waitKey();
return 0;
}
参考链接:
https://docs.opencv.org/3.4/d3/dc1/tutorial_basic_linear_transform.html