BasicLinearTransformsTrackbar.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * @file BasicLinearTransformsTrackbar.cpp
  3. * @brief Simple program to change contrast and brightness
  4. * @date Mon, June 6, 2011
  5. * @author OpenCV team
  6. */
  7. #include "opencv2/imgcodecs.hpp"
  8. #include "opencv2/highgui.hpp"
  9. // we're NOT "using namespace std;" here, to avoid collisions between the beta variable and std::beta in c++17
  10. using namespace cv;
  11. /** Global Variables */
  12. const int alpha_max = 5;
  13. const int beta_max = 125;
  14. int alpha; /**< Simple contrast control */
  15. int beta; /**< Simple brightness control*/
  16. /** Matrices to store images */
  17. Mat image;
  18. /**
  19. * @function on_trackbar
  20. * @brief Called whenever any of alpha or beta changes
  21. */
  22. static void on_trackbar( int, void* )
  23. {
  24. Mat new_image = Mat::zeros( image.size(), image.type() );
  25. for( int y = 0; y < image.rows; y++ )
  26. for( int x = 0; x < image.cols; x++ )
  27. for( int c = 0; c < 3; c++ )
  28. new_image.at<Vec3b>(y,x)[c] = saturate_cast<uchar>( alpha*( image.at<Vec3b>(y,x)[c] ) + beta );
  29. imshow("New Image", new_image);
  30. }
  31. /**
  32. * @function main
  33. * @brief Main function
  34. */
  35. int main( int argc, char** argv )
  36. {
  37. /// Read image given by user
  38. String imageName("lena.jpg"); // by default
  39. if (argc > 1)
  40. {
  41. imageName = argv[1];
  42. }
  43. image = imread( samples::findFile( imageName ) );
  44. /// Initialize values
  45. alpha = 1;
  46. beta = 0;
  47. /// Create Windows
  48. namedWindow("Original Image", 1);
  49. namedWindow("New Image", 1);
  50. /// Create Trackbars
  51. createTrackbar( "Contrast", "New Image", &alpha, alpha_max, on_trackbar );
  52. createTrackbar( "Brightness", "New Image", &beta, beta_max, on_trackbar );
  53. /// Show some stuff
  54. imshow("Original Image", image);
  55. imshow("New Image", image);
  56. /// Wait until user press some key
  57. waitKey();
  58. return 0;
  59. }