Skip to content

Instantly share code, notes, and snippets.

@garybradski
Created September 23, 2017 05:29
Show Gist options
  • Save garybradski/2610942085d16655909b465d3f0e92b5 to your computer and use it in GitHub Desktop.
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
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