Suppose I have two numpy image arrays, a
and b
, of the same dimensions, 8-bit color, RGB format. Now suppose I want to produce a new numpy array whose pixel values are that of the previous two combined using the "Overlay" blending mode.
Its definition, as taken from Wikipedia, is as follows, where b
is the top layer and a
is the bottom layer:
In the formula, I believe a
and b
are represented in terms of their "whiteness" in the sense that a completely white pixel is 1 and a completely black pixel is 0. I am not sure how hue plays into that.
I'm not sure if there's a faster way to do this other than by iterating over the two images pixel by pixel, which is REALLY slow for 1920x1080 images. I need to be able to do this as fast as possible.
For example, I managed to implement the Addition blending mode as follows:
import numpy as np
import cv2
a = cv2.imread("a.jpg", cv2.IMREAD_UNCHANGED)
b = cv2.imread("b.jpg", cv2.IMREAD_UNCHANGED)
a = a.astype(float)
b = b.astype(float)
ab = a
for i in range(len(ab)):
ab[i] = a[i] + b[i]
cv2.imwrite('Out.png', ab)
It seems to be pretty fast, and is certainly much faster than attempting to achieve the same thing by iterating pixel by pixel. But once again, this is just the Addition blending mode, and I need the Overlay blending mode.
If you know of any Python implementation of the Overlay blending mode between two RGB images in the form of numpy arrays that is very efficient, please help me find it. If not, can you implement it as efficiently as possible?