OpenCV学习:改变图像的对比度和亮度
本实例演示简单地改变图像的对比度和亮度,使用了如下线性变换来实现像素值的遍历操作:
The parameters α > 0 and β often called the gain and bias parameters;
sometimes these parameters are said to control contrast and brightness respectively.
代码如下:
// 改变图像的对比度和亮度 #include <opencv2/opencv.hpp> using namespace cv; using namespace std; double alpha; /** < Simple contrast control */ int beta; /** < Simple brightness control */ int main( int argc, char** argv ) { /// 加载图像 Mat image = imread( "./Res/James Harden.jpg" );
/// 目标图像空间预分配 Mat new_image = Mat::zeros( image.size(), image.type() ); /// 输入初始化值 cout <<" Basic Linear Transforms "<<endl; cout <<"-------------------------"<<endl; cout <<" *Enter the alpha value [1.0-3.0]: "; cin >> alpha; cout <<" *Enter the beta value [0-100]: "; cin >> beta; /// 执行变换 new_image(i,j) = alpha * image(i,j) + 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(); cin.get(); return 0; }
运行结果:
希望大家能把自己的所学和他人一起分享,不要去鄙视别人索取时的贪婪,因为最应该被鄙视的是不肯分享时的吝啬。