Skip to content

Instantly share code, notes, and snippets.

@robsears
Created December 11, 2012 16:40
Show Gist options
  • Save robsears/4260157 to your computer and use it in GitHub Desktop.
Save robsears/4260157 to your computer and use it in GitHub Desktop.
Overlay an image in OpenCV using C++
# OverlayImage function reproduced from: http://www.aishack.in/2010/07/transparent-image-overlays-in-opencv/
void OverlayImage(IplImage* src, IplImage* overlay, CvPoint location, CvScalar S, CvScalar D) {
for(int x=0;xwidth;x++)
{
if(x+location.x >= src->width) continue;
for(int y=0;yheight;y++)
{
if(y+location.y>=src->height) continue;
CvScalar source = cvGet2D(src, y+location.y, x+location.x);
CvScalar over = cvGet2D(overlay, y, x);
CvScalar merged;
for(int i=0;i<4;i++)
merged.val[i] = (S.val[i]*source.val[i]+D.val[i]*over.val[i])
cvSet2D(src, y+location.y, x+location.x, merged);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment