opencv/samples/cpp/tutorial_code/HighGUI/BasicLinearTransformsTrackbar.cpp

67 lines
1.5 KiB
C++
Raw Normal View History

2011-06-17 17:57:00 +02:00
/**
* @file LinearTransforms.cpp
* @brief Simple program to change contrast and brightness
* @date Mon, June 6, 2011
* @author OpenCV team
*/
#include "opencv2/highgui/highgui.hpp"
2011-06-17 17:57:00 +02: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
*/
static void on_trackbar( int, void* )
2012-10-17 01:18:30 +02:00
{
Mat new_image = Mat::zeros( image.size(), image.type() );
2011-06-17 17:57:00 +02: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 17:57:00 +02:00
}
/**
* @function main
2012-10-17 01:18:30 +02:00
* @brief Main function
2011-06-17 17:57:00 +02:00
*/
int main( int, char** argv )
2011-06-17 17:57:00 +02:00
{
/// Read image given by user
image = imread( argv[1] );
2012-10-17 01:18:30 +02:00
/// Initialize values
2011-06-17 17:57:00 +02: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 01:18:30 +02:00
createTrackbar( "Brightness Trackbar", "New Image", &beta, beta_max, on_trackbar );
2011-06-17 17:57:00 +02:00
/// Show some stuff
imshow("Original Image", image);
imshow("New Image", image);
2012-10-17 01:18:30 +02:00
2011-06-17 17:57:00 +02:00
/// Wait until user press some key
waitKey();
return 0;
}