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.


猜你喜欢

转载自blog.csdn.net/moonlightpeng/article/details/80877157