在VisualStudio2008+openCV下实现的代码如下:
View Code
// alphaBlending.cpp : 定义控制台应用程序的入口点。
//
#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("左图片");
cvNamedWindow("右图片");
cvNamedWindow("混合图片");
cvShowImage("左图片",src1);
cvShowImage("右图片",src2);
cvShowImage("混合图片",dst);
cvWaitKey(0);
return 0;
}