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

74 lines
1.8 KiB
C++
Raw Normal View History

2011-06-17 23:57:00 +08:00
/**
2019-07-28 17:09:17 +08:00
* @file BasicLinearTransformsTrackbar.cpp
2011-06-17 23:57:00 +08:00
* @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
// we're NOT "using namespace std;" here, to avoid collisions between the beta variable and std::beta in c++17
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
*/
static void on_trackbar( int, void* )
2012-10-17 07:18:30 +08:00
{
Mat new_image = Mat::zeros( image.size(), image.type() );
2011-06-17 23:57:00 +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
*/
2017-07-26 13:39:53 +08:00
int main( int argc, char** argv )
2011-06-17 23:57:00 +08:00
{
/// Read image given by user
2019-07-28 17:09:17 +08:00
String imageName("lena.jpg"); // by default
2017-07-26 13:39:53 +08:00
if (argc > 1)
{
imageName = argv[1];
}
2019-07-28 17:09:17 +08:00
image = imread( samples::findFile( imageName ) );
2011-06-17 23:57:00 +08:00
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
2017-07-26 13:39:53 +08:00
createTrackbar( "Contrast", "New Image", &alpha, alpha_max, on_trackbar );
createTrackbar( "Brightness", "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;
}