Skip to content

Instantly share code, notes, and snippets.

@osolmaz
Forked from Brick85/gist:5009046
Created January 4, 2014 18:40
Show Gist options
  • Save osolmaz/8258869 to your computer and use it in GitHub Desktop.
Save osolmaz/8258869 to your computer and use it in GitHub Desktop.
void blendWithMask(cv::Mat &base, cv::Mat &src, cv::Mat &mask, cv::Mat &out){
char ch = base.channels();
double alpha = 0;
for( int y = 0; y < base.rows; y++ ){
uchar* pBS = base.ptr<uchar>(y);
uchar* pSR = src.ptr<uchar>(y);
uchar* pMK = mask.ptr<uchar>(y);
uchar* pOU = out.ptr<uchar>(y);
for( int x = 0; x < base.cols*ch; x++ ){
int ix = x / ch;
if(pMK[ix] == 255){
pOU[x] = pBS[x];
} else if(pMK[ix] == 0){
pOU[x] = pSR[x];
} else {
alpha = pMK[ix] / 255.0;
pOU[x] = pSR[x] * (1-alpha) + pBS[x] * alpha;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment