addweighted()

import cv2
import numpy as np

img = cv2.imread('img.jpg')
cv2.namedWindow('Image', cv2.WINDOW_AUTOSIZE)
cv2.imshow('Image', img)
# 生成一个黑色的图像 大小和类型与源图像一样
img1 = np.zeros(img.shape, dtype=img.dtype)
cv2.imshow('Image1', img1)
# 数组后面直接加上255 则变为全白图像
img_white = img1 + 255
cv2.imshow('Image_white', img_white)
# 这个函数就相当于
src = cv2.addWeighted(img, 0.2, img1, 0.8, 0)
cv2.imshow('src', src)
src2 = cv2.addWeighted(img, 0.2, img_white, 0.8, 0)
cv2.imshow('src2', src2)
# 第五个参数是,按照权重加和之后 再给图片添加的数值,如果超过255 则全白
src3 = cv2.addWeighted(img, 0.2, img1, 0.8, 100)
cv2.imshow('src3', src3)
cv2.waitKey(0)
cv2.destroyAllWindows()

函数原型

def addWeighted(src1, alpha, src2, beta, gamma, dst=None, dtype=None): # real signature unknown; restored from __doc__
    """ addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]]) -> dst . @brief Calculates the weighted sum of two arrays. . . The function addWeighted calculates the weighted sum of two arrays as follows: . \f[\texttt{dst} (I)= \texttt{saturate} ( \texttt{src1} (I)* \texttt{alpha} + \texttt{src2} (I)* \texttt{beta} + \texttt{gamma} )\f] . where I is a multi-dimensional index of array elements. In case of multi-channel arrays, each . channel is processed independently. . The function can be replaced with a matrix expression: . @code{.cpp} . dst = src1*alpha + src2*beta + gamma; . @endcode . @note Saturation is not applied when the output array has the depth CV_32S. You may even get . result of an incorrect sign in the case of overflow. . @param src1 first input array. . @param alpha weight of the first array elements. . @param src2 second input array of the same size and channel number as src1. . @param beta weight of the second array elements. . @param gamma scalar added to each sum. . @param dst output array that has the same size and number of channels as the input arrays. . @param dtype optional depth of the output array; when both input arrays have the same depth, dtype . can be set to -1, which will be equivalent to src1.depth(). . @sa add, subtract, scaleAdd, Mat::convertTo """

结果展示