2011-06-17 23:57:00 +08:00
|
|
|
/**
|
|
|
|
* @file LinearTransforms.cpp
|
|
|
|
* @brief Simple program to change contrast and brightness
|
|
|
|
* @date Mon, June 6, 2011
|
|
|
|
* @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
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
|
|
|
|
/** Global Variables */
|
|
|
|
const int alpha_max = 5;
|
|
|
|
const int beta_max = 125;
|
|
|
|
int alpha; /**< Simple contrast control */
|
|
|
|
int beta; /**< Simple brightness control*/
|
|
|
|
|
|
|
|
/** Matrices to store images */
|
|
|
|
Mat image;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function on_trackbar
|
|
|
|
* @brief Called whenever any of alpha or beta changes
|
|
|
|
*/
|
2012-11-07 22:21:20 +08:00
|
|
|
static void on_trackbar( int, void* )
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
2013-10-30 20:34:27 +08:00
|
|
|
Mat new_image = Mat::zeros( image.size(), image.type() );
|
2011-06-17 23:57:00 +08:00
|
|
|
|
2013-10-30 20:34:27 +08:00
|
|
|
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 );
|
|
|
|
|
|
|
|
imshow("New Image", new_image);
|
2011-06-17 23:57:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function main
|
2012-10-17 07:18:30 +08:00
|
|
|
* @brief Main function
|
2011-06-17 23:57:00 +08:00
|
|
|
*/
|
2012-11-07 22:21:20 +08:00
|
|
|
int main( int, char** argv )
|
2011-06-17 23:57:00 +08:00
|
|
|
{
|
|
|
|
/// Read image given by user
|
|
|
|
image = imread( argv[1] );
|
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
/// Initialize values
|
2011-06-17 23:57:00 +08:00
|
|
|
alpha = 1;
|
|
|
|
beta = 0;
|
|
|
|
|
|
|
|
/// Create Windows
|
|
|
|
namedWindow("Original Image", 1);
|
|
|
|
namedWindow("New Image", 1);
|
|
|
|
|
|
|
|
/// Create Trackbars
|
|
|
|
createTrackbar( "Contrast Trackbar", "New Image", &alpha, alpha_max, on_trackbar );
|
2012-10-17 07:18:30 +08:00
|
|
|
createTrackbar( "Brightness Trackbar", "New Image", &beta, beta_max, on_trackbar );
|
2011-06-17 23:57:00 +08:00
|
|
|
|
|
|
|
/// Show some stuff
|
|
|
|
imshow("Original Image", image);
|
|
|
|
imshow("New Image", image);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2011-06-17 23:57:00 +08:00
|
|
|
/// Wait until user press some key
|
|
|
|
waitKey();
|
|
|
|
return 0;
|
|
|
|
}
|