OpenCV implements image mixing

principle

This is actually addition, but the difference is that the weights of the two images are different, which will give people a mixed or transparent feeling.

The calculation formula for image blending is as follows:

g(x)=(1-a)f0(x) + af1(x)

By modifying the value of α (0→1), very cool mixing can be achieved.

Now we blend the two images together.

The weight of the first image is 0.2 and the weight of the second image is 0.3. function

cv2.addWeighted()

You can blend images according to the following formula.

dst = aimg1 + Bimg2+y

Here y is taken to be zero.

Code

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['SimHei']

#读取图像
img1 = cv.imread("lena.png")
img2 = cv.imread('lena_saved.png')

#图像的混合
img3 = cv.addWeighted(img1,0.2,img2,0.3,0)

#图像的显示
plt.figure(figsize=(4,4))
plt.imshow(img3[:,:,::-1])
plt.title("混合图像")
plt.show()

Show results

Insert image description here

Guess you like

Origin blog.csdn.net/qq_53545309/article/details/132790905