opencv 滚动条

本文详细介绍了如何使用OpenCV库在窗口中创建可调整参数的滑动条,以实时修改图像合成比例,实现图像混合效果的动态调整。通过设置滑动条的取值范围和回调函数,用户可以直观地控制图像融合的权重,达到增强视觉体验的目的。


参数介绍:
 createTrackbar( trackbarname , "hello" , &alpha_slider ,alpha_max ,  on_trackbar )  ;
在标签中显示的文字(提示滑动条的用途) TrackbarName
创建的滑动条要放置窗体的名字 “hello”
滑动条的取值范围从 0 到 alpha_max (最小值只能为 zero).
滑动后的值存放在 alpha_slider 变量里
每当滑动条的值改变, 就会调用 on_trackbar 回调函数



#include <iostream>
#include <string>
#include <string.h> 

#include <cv.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/imgproc/imgproc.hpp>

using namespace std ;
using namespace cv  ;

const  int  alpha_max = 100 ;
Mat mtbaidu  ,  mtgoogle  , ans  ;

double  alpha , bate ;
int     alpha_slider ;

void   on_trackbar(int ,  void *){
	   alpha = (double)alpha_slider / alpha_max ; 
	   bate = 1.0 - alpha ; 
	   addWeighted(mtbaidu , alpha , mtgoogle , bate , 0.0 , ans) ;
	   imshow("hello" , ans ) ;
}

int  main(){

	 mtbaidu = imread("baidu.jpg" , 1) ;
	 mtgoogle = imread("google.jpg" , 1) ;
	 if(mtbaidu.empty() || mtgoogle.empty()){
		 puts("open error!") ; 
		 return -1 ; 
	 }
	 namedWindow("hello") ;
     char  trackbarname[50] ;
	 sprintf( trackbarname, "%d", alpha_max );

	 createTrackbar( trackbarname , "hello" , &alpha_slider ,alpha_max ,  on_trackbar )  ;
	 on_trackbar( alpha_slider, 0 );

	 waitKey(0) ; 

	 return  0 ; 
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值