opencv/samples/cpp/tutorial_code/HighGUI/AddingImagesTrackbar.cpp

74 lines
1.6 KiB
C++
Raw Normal View History

2011-06-17 23:57:00 +08:00
/**
* @file LinearBlend.cpp
* @brief Simple linear blender ( dst = alpha*src1 + beta*src2 )
* @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"
#include <iostream>
2011-06-17 23:57:00 +08:00
using namespace cv;
using std::cout;
2011-06-17 23:57:00 +08:00
/** Global Variables */
const int alpha_slider_max = 100;
2012-10-17 07:18:30 +08:00
int alpha_slider;
2011-06-17 23:57:00 +08:00
double alpha;
2012-10-17 07:18:30 +08:00
double beta;
2011-06-17 23:57:00 +08:00
/** Matrices to store images */
Mat src1;
Mat src2;
Mat dst;
2016-07-18 21:32:05 +08:00
//![on_trackbar]
2011-06-17 23:57:00 +08:00
/**
* @function on_trackbar
* @brief Callback for trackbar
*/
static void on_trackbar( int, void* )
2012-10-17 07:18:30 +08:00
{
2011-06-17 23:57:00 +08:00
alpha = (double) alpha_slider/alpha_slider_max ;
beta = ( 1.0 - alpha );
addWeighted( src1, alpha, src2, beta, 0.0, dst);
imshow( "Linear Blend", dst );
}
2016-07-18 21:32:05 +08:00
//![on_trackbar]
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
*/
int main( void )
2011-06-17 23:57:00 +08:00
{
2016-07-18 21:32:05 +08:00
//![load]
/// Read images ( both have to be of the same size and type )
2014-09-15 00:16:45 +08:00
src1 = imread("../data/LinuxLogo.jpg");
src2 = imread("../data/WindowsLogo.jpg");
2016-07-18 21:32:05 +08:00
//![load]
2011-06-17 23:57:00 +08:00
if( src1.empty() ) { cout << "Error loading src1 \n"; return -1; }
if( src2.empty() ) { cout << "Error loading src2 \n"; return -1; }
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_slider = 0;
2016-07-18 21:32:05 +08:00
//![window]
namedWindow("Linear Blend", WINDOW_AUTOSIZE); // Create Window
//![window]
2011-06-17 23:57:00 +08:00
2016-07-18 21:32:05 +08:00
//![create_trackbar]
2011-06-17 23:57:00 +08:00
char TrackbarName[50];
2012-10-17 07:18:30 +08:00
sprintf( TrackbarName, "Alpha x %d", alpha_slider_max );
2011-06-17 23:57:00 +08:00
createTrackbar( TrackbarName, "Linear Blend", &alpha_slider, alpha_slider_max, on_trackbar );
2016-07-18 21:32:05 +08:00
//![create_trackbar]
2011-06-17 23:57:00 +08:00
/// Show some stuff
on_trackbar( alpha_slider, 0 );
2012-10-17 07:18:30 +08:00
2011-06-17 23:57:00 +08:00
/// Wait until user press some key
waitKey(0);
return 0;
}