函數說明
cv2.addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]]) → dst
- 1
參數說明
- src1 – first input array.
- alpha – weight of the first array elements.
- src2 – second input array of the
same size and channel
number assrc1
. - beta – weight of the second array elements.
- dst – output array that has the
same size and number of channels
as the input arrays. - gamma – scalar added to each sum.
- 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 tosrc1.depth()
.
此函數可以用一下矩陣表達式來代替:
dst = src1 * alpha + src2 * beta + gamma;
注意:由參數說明可以看出,被疊加的兩幅圖像必須是尺寸相同、類型相同的;並且,當輸出圖像array的深度為CV_32S時,這個函數就不適用了,這時候就會內存溢出或者算出的結果壓根不對。
CV_32S is a signed 32bit integer value for each pixel
python代碼實現:
# coding=utf-8 import cv2 # 底板圖案 bottom_pic = 'girl1.jpg' # 上層圖案 top_pic = 'girl2.jpg' bottom = cv2.imread(bottom_pic) top = cv2.imread(top_pic) h, w, _ = bottom.shape img2 = cv2.resize(top, (w,h), interpolation=cv2.INTER_AREA) #alpha,beta,gamma可調 alpha = 0.7 beta = 1-alpha gamma = 0 # 權重越大,透明度越低 overlapping = cv2.addWeighted(bottom, alpha, img2, beta, gamma) #cv2.addWeighted(bottom, 0.5, img2, 0.5, 0) #overlapping = cv2.addWeighted(bottom,0.8,top,0.2,0) # 保存疊加后的圖片 cv2.imwrite('overlap(8:2).jpg', overlapping) cv2.namedWindow('newImage') cv2.imshow('newImage',overlapping) cv2.waitKey() cv2.destroyAllWindows()