What is the image weighted sum

The so-called image weighted sum is to take the weights of the two images into account when calculating the sum of the pixel values of the two images. The data formula is expressed as follows:

dst=saturate(src1a+src2b+y)

The cv2.addweighted () function is provided in OpenCV to realize the weighted sum of the image. The function is defined as:

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

Where, the parameters alpha and beta are the coefficients corresponding to src1 and src2, and their sum may or may not be equal to 1. Corresponding to mathematical formulas A and B respectively. Gamma corresponds to the math firm Y. Note that the value of gamma can be 0, but cannot be omitted. It is mandatory.

The simple understanding is “image 1 coefficient 1+ image 2 system 2+ brightness adjustment parameter”.

Heads appeared in the glass

In each big paranormal website, we will see a variety of images in the appearance of a variety of ghost. Of course, the blogger is not saying that it doesn’t exist at all, which is beyond the scope of this discussion, but we can use image weighting and embedding a human head to create a ghosting appearance.

Above are the two original images, corresponding to srC1 and SRc2 above. Overlay to achieve the ghost effect of the specific code 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()
Copy the code

After running, the effect looks like this:

Of course, you can still see the outline of the square here, and it will be more complete later when we talk about matrix operations in more detail. Now I’m going to focus on the basics to make the reader more interested in OpenCV.