1 #include<opencv2opencv.hpp> 2 #include<iostream> 3 #include<vector> 4 using namespace cv; 5 using namespace std; 6 7 #define WINDOW_NAME "【线性混合示例】" 8 const int g_nMaxAlphaValue = 100; //Alpha的最大值 9 int g_nAlphaValueSlider; 10 double g_dAlphaValue; 11 double g_dBetaValue; 12 Mat g_srcImage1; 13 Mat g_srcImage2; 14 Mat g_dstImage; 15 16 void on_Trackbar(int, void*) { 17 //求出当前alpha值相对于最大值的比例 18 g_dAlphaValue = (double)g_nAlphaValueSlider / g_nMaxAlphaValue; 19 //则beta值为1减去alpha值 20 g_dBetaValue = (1.0 - g_dAlphaValue); 21 22 //根据alpha和beta值进行线性混合 23 addWeighted(g_srcImage1,g_dAlphaValue,g_srcImage2,g_dBetaValue,0.0,g_dstImage); 24 25 //显示效果图 26 imshow(WINDOW_NAME,g_dstImage); 27 } 28 int main(int argc,char** argv) { 29 g_srcImage1 = imread("C:\Users\Nelsoner\Desktop\Camera Roll\05.jpg"); 30 g_srcImage2 = imread("C:\Users\Nelsoner\Desktop\Camera Roll\08.jpg"); 31 32 if (!g_srcImage1.data) { 33 cout << "读取第一幅图片错误!" << endl; 34 return -1; 35 } 36 if (!g_srcImage2.data) { 37 cout << "读取第二幅图片错误!" << endl; 38 return -1; 39 } 40 //设置滑动条初值为70 41 g_nAlphaValueSlider = 70; 42 43 namedWindow(WINDOW_NAME); 44 45 //在创建的窗体中创建一个滑动条控件 46 char TrackbarName[50]; 47 sprintf(TrackbarName,"透明值 %d",g_nMaxAlphaValue); 48 49 createTrackbar(TrackbarName,WINDOW_NAME, &g_nAlphaValueSlider,g_nMaxAlphaValue,on_Trackbar); 50 51 on_Trackbar(g_nAlphaValueSlider,0); 52 53 54 waitKey(); 55 return 0; 56 }
效果图:
获取当前轨迹条的位置:getTrackbarPos();