Created
September 23, 2017 05:29
-
-
Save garybradski/2610942085d16655909b465d3f0e92b5 to your computer and use it in GitHub Desktop.
blend one image with another using an alpha mask in c++ w/opencv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void alphaBlend(Mat& foreground, Mat& background, Mat& alpha, Mat& outImage) | |
{ | |
// Find number of pixels. | |
int numberOfPixels = foreground.rows * foreground.cols * foreground.channels(); | |
// Get floating point pointers to the data matrices | |
float* fptr = reinterpret_cast<float*>(foreground.data); | |
float* bptr = reinterpret_cast<float*>(background.data); | |
float* aptr = reinterpret_cast<float*>(alpha.data); | |
float* outImagePtr = reinterpret_cast<float*>(outImage.data); | |
// Loop over all pixesl ONCE | |
for( | |
int i = 0; | |
i < numberOfPixels; | |
i++, outImagePtr++, fptr++, aptr++, bptr++ | |
) | |
{ | |
*outImagePtr = (*fptr)*(*aptr) + (*bptr)*(1 - *aptr); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment