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