opencv学习(4)——图像亮度、对比度调整

来源:互联网 发布:scratch趣味编程100例 编辑:程序博客网 时间:2024/05/22 12:00
#include "opencv2/imgcodecs.hpp"#include "opencv2/highgui/highgui.hpp"#include <iostream>using namespace cv;double alpha; /**< Simple contrast control */int beta;  /**< Simple brightness control *//** * @function main * @brief Main function */int main( int, char** argv ){   /// Read image given by user    Mat image = imread("D:\\software_anz\\opencv\\opencv\\sources\\samples\\data\\HappyFish.jpg");   Mat new_image = Mat::zeros( image.size(), image.type() );   /// Initialize values   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;   /// Do the operation new_image(i,j) = alpha*image(i,j) + beta   /// Instead of these 'for' loops we could have used simply:   /// image.convertTo(new_image, -1, alpha, beta);   /// but we wanted to show you how to access the pixels :)   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 );                }       }      }   /// Create Windows   namedWindow("Original Image", 1);   namedWindow("New Image", 1);   /// Show stuff   imshow("Original Image", image);   imshow("New Image", new_image);   /// Wait until user press some key   waitKey();   return 0;}

1.亮度和对比度调整实验
两种常用的点过程(即点算子),是用常数对点进行 乘法 和 加法 运算:
g(x) = A*f(x) + B
两个参数 A和 B 一般称作 增益 和 偏置 参数。我们往往用这两个参数来分别控制 对比度 和 亮度 。
你可以把 f(x) 看成源图像像素,把 g(x) 看成输出图像像素。这样一来,上面的式子就能写得更清楚些:
g(i,j) = A*f(i,j) + B
其中, i 和 j 表示像素位于 第i行 和 第j列 。
2.关于saturate_cast防止数据溢出
参考http://blog.csdn.net/mjlsuccess/article/details/12401839

0 0