Implemented in visualstudio2008 + opencvCodeAs follows:
View code
// Alphablending. cpp: defines console applications.Program.
//
# Include " Stdafx. h "
# Include < Cv. h >
# Include < Cxcore. h >
# Include < Highgui. h >
Iplimage * Alphablending (iplimage * Src1, iplimage * Src2, Double Winsize)
{
If (Src1 -> Height ! = Src2 -> Height | Src1 -> Width ! = Src2 -> Width)
{
// The height and weight of two images must agree to blend
Return 0 ;
}
Int W = Src1 -> Width, h = Src1 -> Height;
Int Winstart = ( Int ) (W * Winsize / 2 ), Winend = ( Int ) (W - Winstart );
Iplimage * DST = Cvcreateimage (cvgetsize (src1), ipl_depth_8u, 3 );
For ( Int I = 0 ; I < H; I ++ )
{
For ( Int J = 0 ; J < W; j ++ )
{
Cvscalar P1, P2, P;
Double A = 0 ;
If (J > Winend)
{
A = 1 ;
}
Else If (J > Winstart)
{
A = (J - Winstart) / ( Double ) (Winend - Winstart );
}
P1 = Cvget2d (src1, I, j );
P2 = Cvget2d (src2, I, j );
For ( Int K = 0 ; K < 3 ; K ++ )
{
P. Val [k] = A * P2.val [k] + ( 1 - A) * P1.val [k];
}
Cvset2d (DST, I, j, P );
}
}
Return DST;
}
Int _ Tmain ( Int Argc, _ tchar * Argv [])
{
Iplimage * Src1 = Cvloadimage ( " Apple.jpg " );
Iplimage * Src2 = Cvloadimage ( " Orange.jpg " );
Iplimage * DST = Alphablending (src1, src2, 0.9 );
Cvnamedwindow ( " Left Image " );
Cvnamedwindow ( " Right Image " );
Cvnamedwindow ( " Hybrid Image " );
Cvshowimage ( " Left Image " , Src1 );
Cvshowimage ( " Right Image " , Src2 );
Cvshowimage ( " Hybrid Image " , DST );
Cvwaitkey ( 0 );
Return 0 ;
}