opencv 两图像叠加 创建滑动条

#include <cv.h>
#include <highgui.h>
#include <iostream>
using namespace cv;
int main( int argc, char** argv )
{
double alpha = 0.5; double beta; double input;
Mat src1, src2, dst;
/// Ask the user enter alpha
std::cout<<" Simple Linear Blender "<<std::endl;
std::cout<<"-----------------------"<<std::endl;
std::cout<<"* Enter alpha [0-1]: ";
std::cin>>input;
/// We use the alpha provided by the user if it is between 0 and 1
if( input >= 0.0 && input <= 1.0 )
{ alpha = input; }
/// Read image ( same size, same type )
src1 = imread("../../images/LinuxLogo.jpg");
src2 = imread("../../images/WindowsLogo.jpg");
if( !src1.data ) { printf("Error loading src1 \n"); return -1; }
if( !src2.data ) { printf("Error loading src2 \n"); return -1; }
/// Create Windows
namedWindow("Linear Blend", 1);
beta = ( 1.0 - alpha );
addWeighted( src1, alpha, src2, beta, 0.0, dst);
imshow( "Linear Blend", dst );
waitKey(0);
return 0;
}

有滑动条

#include <opencv.hpp>
#include <iostream>
#include <highgui.h>

using namespace std;
using namespace cv;

//  描述:定义一些辅助宏 
//------------------------------------------------------------------------------------------------ 
#define WINDOW_NAME "【滑动条的创建&线性混合示例】"        //为窗口标题定义的宏 


//		描述:全局变量声明
//-----------------------------------------------------------------------------------------------
const int		g_nMaxAlphaValue = 100;//Alpha值的最大值
int				g_nAlphaValueSlider = 70;//滑动条对应的变量
double		    g_dAlphaValue;
double			g_dBetaValue;

//声明存储图像的变量
Mat src1, src2;

//响应滑动条的回调函数
void on_Trackbar(int, void*)
{
	//求出当前alpha值相对于最大值的比例
	g_dAlphaValue = (double)g_nAlphaValueSlider / g_nMaxAlphaValue;
	//则beta值为1减去alpha值
	g_dBetaValue = (1.0 - g_dAlphaValue);

	//根据alpha和beta值进行线性混合
	Mat g_dstImage;
	addWeighted(src1, g_dAlphaValue, src2, g_dBetaValue, 0.0, g_dstImage);

	//显示效果图
	imshow(WINDOW_NAME, g_dstImage);
}



int main(int argc, char** argv)
{
	double alpha = 0.5; 
	double beta; 
	double input;

	/// Read image ( same size, same type )
	src1 = imread("color.jpg");
	src2 = imread("colors.jpg");
	if (!src1.data) { printf("Error loading src1 \n"); return -1; }
	if (!src2.data) { printf("Error loading src2 \n"); return -1; }

	/// Create Windows
	//创建窗体
	namedWindow(WINDOW_NAME, 1);

	//在创建的窗体中创建一个滑动条控件
	char TrackbarName[50];
	sprintf(TrackbarName, "透明值 %d", g_nMaxAlphaValue);

	createTrackbar(TrackbarName, WINDOW_NAME, &g_nAlphaValueSlider, g_nMaxAlphaValue, on_Trackbar);

	//结果在回调函数中显示
	on_Trackbar(g_nAlphaValueSlider, 0);

	waitKey(0);
	return 0;
}


color.jpg


colors.jpg

运行结果



Explanation
1. Since we are going to perform:
g(x) = (1 - α)f0(x) + αf1(x)
We need two source images (f0(x) and f1(x)). So, we load them in the usual way:
src1 = imread("../../images/LinuxLogo.jpg");
src2
= imread("../../images/WindowsLogo.jpg");
Warning: Since we are adding src1 and src2, they both have to be of the same size (width and height) and
type.
2. Now we need to generate the
g(x) image. For this, the function addWeighted comes quite handy:
beta = ( 1.0 - alpha );
addWeighted( src1, alpha, src2, beta,
0.0, dst);
since addWeighted produces:
dst = α · src1 + β · src2 + γ
In this case, γ is the argument 0:0 in the code above.
3. Create windows, show the images and wait for the user to end the program.


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值