Skip to content

Instantly share code, notes, and snippets.

@maximus5684
Created July 14, 2015 17:49
Show Gist options
  • Save maximus5684/082f8939edb6aed7ba0a to your computer and use it in GitHub Desktop.
Save maximus5684/082f8939edb6aed7ba0a to your computer and use it in GitHub Desktop.
OpenCV Image Overlay w/Transparency
void OverlayImage(Mat* src, Mat* overlay, const Point& location)
{
for (int y = max(location.y, 0); y < src->rows; ++y)
{
int fY = y - location.y;
if (fY >= overlay->rows)
break;
for (int x = max(location.x, 0); x < src->cols; ++x)
{
int fX = x - location.x;
if (fX >= overlay->cols)
break;
double opacity = ((double)overlay->data[fY * overlay->step + fX * overlay->channels() + 3]) / 255;
for (int c = 0; opacity > 0 && c < src->channels(); ++c)
{
unsigned char overlayPx = overlay->data[fY * overlay->step + fX * overlay->channels() + c];
unsigned char srcPx = src->data[y * src->step + x * src->channels() + c];
src->data[y * src->step + src->channels() * x + c] = srcPx * (1. - opacity) + overlayPx * opacity;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment