Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/AlphaBlending/alphaBlend.cpp
3118 views
1
//
2
// alphaBlend.cpp
3
//
4
//
5
// Created by Sunita Nayak on 3/14/17.
6
//
7
//
8
9
#include <opencv2/opencv.hpp>
10
11
using namespace cv;
12
using namespace std;
13
14
// Alpha blending using multiply and add functions
15
Mat& blend(Mat& alpha, Mat& foreground, Mat& background, Mat& outImage)
16
{
17
Mat fore, back;
18
multiply(alpha, foreground, fore);
19
multiply(Scalar::all(1.0)-alpha, background, back);
20
add(fore, back, outImage);
21
22
return outImage;
23
}
24
25
// Alpha Blending using direct pointer access
26
Mat& alphaBlendDirectAccess(Mat& alpha, Mat& foreground, Mat& background, Mat& outImage)
27
{
28
29
int numberOfPixels = foreground.rows * foreground.cols * foreground.channels();
30
31
float* fptr = reinterpret_cast<float*>(foreground.data);
32
float* bptr = reinterpret_cast<float*>(background.data);
33
float* aptr = reinterpret_cast<float*>(alpha.data);
34
float* outImagePtr = reinterpret_cast<float*>(outImage.data);
35
36
int i,j;
37
for ( j = 0; j < numberOfPixels; ++j, outImagePtr++, fptr++, aptr++, bptr++)
38
{
39
*outImagePtr = (*fptr)*(*aptr) + (*bptr)*(1 - *aptr);
40
}
41
42
return outImage;
43
}
44
45
46
int main(int argc, char** argv)
47
{
48
49
// Read in the png foreground asset file that contains both rgb and alpha information
50
Mat foreGroundImage = imread("foreGroundAssetLarge.png", -1);
51
Mat bgra[4];
52
split(foreGroundImage, bgra);//split png foreground
53
54
// Save the foregroung RGB content into a single Mat
55
vector<Mat> foregroundChannels;
56
foregroundChannels.push_back(bgra[0]);
57
foregroundChannels.push_back(bgra[1]);
58
foregroundChannels.push_back(bgra[2]);
59
Mat foreground = Mat::zeros(foreGroundImage.size(), CV_8UC3);
60
merge(foregroundChannels, foreground);
61
62
// Save the alpha information into a single Mat
63
vector<Mat> alphaChannels;
64
alphaChannels.push_back(bgra[3]);
65
alphaChannels.push_back(bgra[3]);
66
alphaChannels.push_back(bgra[3]);
67
Mat alpha = Mat::zeros(foreGroundImage.size(), CV_8UC3);
68
merge(alphaChannels, alpha);
69
70
// Read background image
71
Mat background = imread("backGroundLarge.jpg");
72
73
// Convert Mat to float data type
74
foreground.convertTo(foreground, CV_32FC3);
75
background.convertTo(background, CV_32FC3);
76
alpha.convertTo(alpha, CV_32FC3, 1.0/255); // keeps the alpha values betwen 0 and 1
77
78
// Number of iterations to average the performane over
79
int numOfIterations = 1; //1000;
80
81
// Alpha blending using functions multiply and add
82
Mat outImage= Mat::zeros(foreground.size(), foreground.type());
83
double t = (double)getTickCount();
84
for (int i=0; i<numOfIterations; i++) {
85
outImage = blend(alpha, foreground, background, outImage);
86
}
87
t = ((double)getTickCount() - t)/getTickFrequency();
88
cout << "Time for alpha blending using multiply & add function : " << t*1000/numOfIterations << " milliseconds" << endl;
89
90
// Alpha blending using direct Mat access with for loop
91
outImage = Mat::zeros(foreground.size(), foreground.type());
92
t = (double)getTickCount();
93
for (int i=0; i<numOfIterations; i++) {
94
outImage = alphaBlendDirectAccess(alpha, foreground, background, outImage);
95
}
96
t = ((double)getTickCount() - t)/getTickFrequency();
97
cout << "Time for alpha blending using alphaBlendDirectAccess : " << t*1000/numOfIterations << " milliseconds" << endl;
98
99
//imshow("alpha blended image", outImage/255);
100
//waitKey(0);
101
102
return 0;
103
}
104
105