I'm not sure if this is the best way to do it, but this will maybe do the job.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdio.h>
using namespace cv;
void addAlpha(Mat src, Mat input);
void test(){
namedWindow("window", WINDOW_AUTOSIZE);
Mat output(Size(300, 300), CV_8UC4, Scalar(0));
Mat m1(Size(300, 300), CV_8UC4, Scalar(0));
Mat m2(Size(300, 300), CV_8UC4, Scalar(0));
Mat m3(Size(300, 300), CV_8UC4, Scalar(0));
circle(m1, Point(130, 130), 75, Scalar(0, 0, 0xFF, 0xFF), -1);
circle(m2, Point(150, 150), 75, Scalar(0, 0xFF, 0, 0xFF), -1);
rectangle(m3, Rect(100, 100, 60, 60), Scalar(0xFF, 0, 0, 0xFF), -1);
rectangle(m3, Rect(115, 115, 30, 30), Scalar(0), -1);
m1.copyTo(output);
addAlpha(output,m2);
addAlpha(output,m3);
imshow("window", output);
cvWaitKey(0);
destroyAllWindows();
}
void addAlpha(Mat src, Mat input){
if(src.rows != input.rows || src.cols != input.cols){
perror("Not same size");
}
for(int i = 0; i < src.rows; i++){
for(int j = 0; j < src.cols; j++){
src.at<cv::Vec4b>(i,j)[0] = src.at<cv::Vec4b>(i,j)[0] * (1 - input.at<cv::Vec4b>(i,j)[3]/255.0) + input.at<cv::Vec4b>(i,j)[0] * (input.at<cv::Vec4b>(i,j)[3]/255.0);
src.at<cv::Vec4b>(i,j)[1] = src.at<cv::Vec4b>(i,j)[1] * (1 - input.at<cv::Vec4b>(i,j)[3]/255.0) + input.at<cv::Vec4b>(i,j)[1] * (input.at<cv::Vec4b>(i,j)[3]/255.0);
src.at<cv::Vec4b>(i,j)[2] = src.at<cv::Vec4b>(i,j)[2] * (1 - input.at<cv::Vec4b>(i,j)[3]/255.0) + input.at<cv::Vec4b>(i,j)[2] * (input.at<cv::Vec4b>(i,j)[3]/255.0);
}
}
}