The function of OpenCV image mixing is:
cv2.addWeighted(src1, alpha, src2, beta, gamma, dst=..., dtype=...) -> dst
Required parameters:
- src1: numpy array of the first image.
- alpha: The weight of the first image.
- src2: numpy array of the second image.
- beta: The weight of the second image.
- gamma: A constant added after image blending.
Optional parameters:
- dst: numpy array of output images.
- dtype: The bit depth of the output image, the default is src1.depth().
The mixed formula can be expressed as follows:
Example
The following example sets alpha + beta = 1.0, and gamma = 0.0. This example is also on my github repo. Press ESC after executing the script to exit the script.
import cv2
def main():
img1 = cv2.imread("Dog_Strong.jpg")
img2 = cv2.imread("Dog_Weak.jpg")
window_name = "Blending"
def change_trackbar(val):
alpha = float(val / 255)
blend_img = cv2.addWeighted(img2, alpha, img1, 1.0 - alpha, 0.0)
cv2.imshow(window_name, blend_img)
cv2.namedWindow(window_name)
cv2.createTrackbar("trackbar", window_name, 0, 255, change_trackbar)
cv2.imshow(window_name, img1)
while True:
if cv2.waitKey(100) == 27: # ESC
cv2.destroyWindow(window_name)
break
if __name__ == "__main__":
main()
Demo:
Top comments (0)