Created
July 14, 2015 17:49
-
-
Save maximus5684/082f8939edb6aed7ba0a to your computer and use it in GitHub Desktop.
OpenCV Image Overlay w/Transparency
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 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