AddingImagesTrackbar.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * @file AddingImagesTrackbar.cpp
  3. * @brief Simple linear blender ( dst = alpha*src1 + beta*src2 )
  4. * @author OpenCV team
  5. */
  6. #include "opencv2/imgcodecs.hpp"
  7. #include "opencv2/highgui.hpp"
  8. #include <iostream>
  9. using namespace cv;
  10. using std::cout;
  11. /** Global Variables */
  12. const int alpha_slider_max = 100;
  13. int alpha_slider;
  14. double alpha;
  15. double beta;
  16. /** Matrices to store images */
  17. Mat src1;
  18. Mat src2;
  19. Mat dst;
  20. //![on_trackbar]
  21. /**
  22. * @function on_trackbar
  23. * @brief Callback for trackbar
  24. */
  25. static void on_trackbar( int, void* )
  26. {
  27. alpha = (double) alpha_slider/alpha_slider_max ;
  28. beta = ( 1.0 - alpha );
  29. addWeighted( src1, alpha, src2, beta, 0.0, dst);
  30. imshow( "Linear Blend", dst );
  31. }
  32. //![on_trackbar]
  33. /**
  34. * @function main
  35. * @brief Main function
  36. */
  37. int main( void )
  38. {
  39. //![load]
  40. /// Read images ( both have to be of the same size and type )
  41. src1 = imread( samples::findFile("LinuxLogo.jpg") );
  42. src2 = imread( samples::findFile("WindowsLogo.jpg") );
  43. //![load]
  44. if( src1.empty() ) { cout << "Error loading src1 \n"; return -1; }
  45. if( src2.empty() ) { cout << "Error loading src2 \n"; return -1; }
  46. /// Initialize values
  47. alpha_slider = 0;
  48. //![window]
  49. namedWindow("Linear Blend", WINDOW_AUTOSIZE); // Create Window
  50. //![window]
  51. //![create_trackbar]
  52. char TrackbarName[50];
  53. sprintf( TrackbarName, "Alpha x %d", alpha_slider_max );
  54. createTrackbar( TrackbarName, "Linear Blend", &alpha_slider, alpha_slider_max, on_trackbar );
  55. //![create_trackbar]
  56. /// Show some stuff
  57. on_trackbar( alpha_slider, 0 );
  58. /// Wait until user press some key
  59. waitKey(0);
  60. return 0;
  61. }