2011-06-17 23:57:00 +08:00
|
|
|
/**
|
|
|
|
* @file BasicLinearTransforms.cpp
|
|
|
|
* @brief Simple program to change contrast and brightness
|
|
|
|
* @author OpenCV team
|
|
|
|
*/
|
|
|
|
|
2014-07-04 22:48:15 +08:00
|
|
|
#include "opencv2/imgcodecs.hpp"
|
2016-02-15 21:37:29 +08:00
|
|
|
#include "opencv2/highgui.hpp"
|
2011-06-17 23:57:00 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
2018-03-14 12:58:45 +08:00
|
|
|
// we're NOT "using namespace std;" here, to avoid collisions between the beta variable and std::beta in c++17
|
|
|
|
using std::cin;
|
|
|
|
using std::cout;
|
|
|
|
using std::endl;
|
2011-06-17 23:57:00 +08:00
|
|
|
using namespace cv;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function main
|
2012-10-17 07:18:30 +08:00
|
|
|
* @brief Main function
|
2011-06-17 23:57:00 +08:00
|
|
|
*/
|
2017-07-26 13:39:53 +08:00
|
|
|
int main( int argc, char** argv )
|
2011-06-17 23:57:00 +08:00
|
|
|
{
|
2017-01-05 23:05:53 +08:00
|
|
|
/// Read image given by user
|
|
|
|
//! [basic-linear-transform-load]
|
2018-07-12 01:38:16 +08:00
|
|
|
CommandLineParser parser( argc, argv, "{@input | ../data/lena.jpg | input image}" );
|
|
|
|
Mat image = imread( parser.get<String>( "@input" ) );
|
|
|
|
if( image.empty() )
|
2017-07-26 13:39:53 +08:00
|
|
|
{
|
2018-07-12 01:38:16 +08:00
|
|
|
cout << "Could not open or find the image!\n" << endl;
|
|
|
|
cout << "Usage: " << argv[0] << " <Input image>" << endl;
|
|
|
|
return -1;
|
2017-07-26 13:39:53 +08:00
|
|
|
}
|
2017-01-05 23:05:53 +08:00
|
|
|
//! [basic-linear-transform-load]
|
2018-07-12 01:38:16 +08:00
|
|
|
|
2017-01-05 23:05:53 +08:00
|
|
|
//! [basic-linear-transform-output]
|
|
|
|
Mat new_image = Mat::zeros( image.size(), image.type() );
|
|
|
|
//! [basic-linear-transform-output]
|
|
|
|
|
2018-07-12 01:38:16 +08:00
|
|
|
//! [basic-linear-transform-parameters]
|
|
|
|
double alpha = 1.0; /*< Simple contrast control */
|
|
|
|
int beta = 0; /*< Simple brightness control */
|
|
|
|
|
2017-01-05 23:05:53 +08:00
|
|
|
/// Initialize values
|
|
|
|
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;
|
2018-07-12 01:38:16 +08:00
|
|
|
//! [basic-linear-transform-parameters]
|
2017-01-05 23:05:53 +08:00
|
|
|
|
|
|
|
/// 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 :)
|
|
|
|
//! [basic-linear-transform-operation]
|
|
|
|
for( int y = 0; y < image.rows; y++ ) {
|
|
|
|
for( int x = 0; x < image.cols; x++ ) {
|
2018-07-12 01:38:16 +08:00
|
|
|
for( int c = 0; c < image.channels(); c++ ) {
|
2017-01-05 23:05:53 +08:00
|
|
|
new_image.at<Vec3b>(y,x)[c] =
|
2018-07-12 01:38:16 +08:00
|
|
|
saturate_cast<uchar>( alpha*image.at<Vec3b>(y,x)[c] + beta );
|
2017-01-05 23:05:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//! [basic-linear-transform-operation]
|
|
|
|
|
|
|
|
//! [basic-linear-transform-display]
|
|
|
|
/// Show stuff
|
|
|
|
imshow("Original Image", image);
|
|
|
|
imshow("New Image", new_image);
|
|
|
|
|
|
|
|
/// Wait until user press some key
|
|
|
|
waitKey();
|
|
|
|
//! [basic-linear-transform-display]
|
|
|
|
return 0;
|
2011-06-17 23:57:00 +08:00
|
|
|
}
|