OpenCV学习笔记-\doc\tutorials\core\basic_linear_transform
为了尊重原作,我没有修改代码,为了在我之前博文中配置好的MSVS2010中跑这个程序,我做了一些小修改,注意,算法是不变的。
代码如下:
#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream> using namespace cv; using namespace std; double alpha; /**< Simple contrast control */ int beta; /**< Simple brightness control */ int main( int argc, char** argv ) { /// Read image given by user Mat image = imread("lena.jpg"); /// matlab风格的初始化0值Mat对象 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 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(0); return 0; }