OpenCV-Python Development Guide (2) --- Image weighting and making ghosts

What is image weighted sum

The so-called image weighted sum is to consider the weight of the two images when calculating the sum of the pixel values ​​of the two images. The data formula is expressed as follows:

dst=saturate(src1a+src2b+y)

OpenCV provides the cv2.addWeighted() function to realize the weighted sum of images. The function is defined as:

addWeighted(src1, alpha, src2, beta, gamma, dst=None, dtype=None)

Among them, the parameters alpha and beta are the coefficients corresponding to src1 and src2, and their sum may be equal to 1, or not equal to 1. Corresponding to the mathematical formulas a and b respectively. And gamma corresponds to the math company y. It should be noted that the value of gamma can be 0, but it cannot be omitted. It is a required parameter.

The simple understanding is "image 1 coefficient 1 + image 2 system 2 + brightness adjustment parameters".

Human heads appear on glass

On the various supernatural websites, we will see various ghost images appearing in various captured images. Of course, the blogger does not want to say that this is completely non-existent. As for the existence or non-existence, it is not in the scope of discussion here, but we can embed a certain image into a human head through image weighting and make it look like a ghost.
Background image
Human head illustration
Above are two original images, corresponding to src1 and src2 above. The specific code for superimposing to achieve the ghost effect is as follows:

import cv2

img = cv2.imread("2_2.png", 1)
head = cv2.imread("2_1.png", 1)
print(img.shape, head.shape)
head = cv2.addWeighted(img, 1, head, 0.3, 0)
cv2.imshow("123", head)
cv2.waitKey()
cv2.destroyAllWindows()

After running, the effect is as follows:
Embedded in human head
Of course, the outline of Founder can still be seen here. When the matrix operation is introduced in detail later, it will be more perfect. Now I mainly talk about some basic things to make readers more interested in OpenCV.

Guess you like

Origin blog.csdn.net/liyuanjinglyj/article/details/113760987