亮度和对比度的调整原理
公式原理: new_image = a*image + beta 即
g(x)=αf(x)+β
其中,
其中
一下代码执行的公式为:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
double alpha; /*< Simple contrast control */
int beta; /*< Simple brightness control */
int main( int argc, char** argv )
{
Mat image = imread( argv[1] );
Mat new_image = Mat::zeros( image.size(), image.type() );
std::cout<<" Basic Linear Transforms "<<std::endl;
std::cout<<"-------------------------"<<std::endl;
std::cout<<"* Enter the alpha value [1.0-3.0]: ";std::cin>>alpha;
std::cout<<"* Enter the beta value [0-100]: "; std::cin>>beta;
for( int y = 0; y < image.rows; y++ )
{
for( int x = 0; x < image.cols; x++ )
{
for( int c = 0; c < 3; c++ )
{
new_image.at<Vec3b>(y,x)[c] = saturate_cast<uchar>( alpha*( image.at<Vec3b>(y,x)[c] ) + beta );
}
}
}
namedWindow("Original Image", 1);
namedWindow("New Image", 1);
imshow("Original Image", image);
imshow("New Image", new_image);
waitKey();
return 0;
}
结果
- 运行代码的参数:
α=2.2 ,β=50 $ ./BasicLinearTransforms lena.jpgBasic Linear Transforms-------------------------* Enter the alpha value [1.0-3.0]: 2.2* Enter the beta value [0-100]: 50 -
结果如下: