簡(jiǎn)介
有的時(shí)候我們需要將兩張圖片在alpha通道進(jìn)行混合,比如深度學(xué)習(xí)數(shù)據(jù)集增強(qiáng)方式MixUp。OpenCV的addWeighted
提供了相關(guān)操作,此篇博客將詳細(xì)介紹這個(gè)函數(shù),并給出代碼示例。????
函數(shù)原型
o u t p u t I m g = s a t u r a t e ( α ? i n p u t I m g 1 + β ? i n p u t I m g 2 + γ ) \rm outputImg=saturate( \alpha*inputImg1+ \beta*inputImg2 + \gamma) outputImg=saturate(α?inputImg1+β?inputImg2+γ)
cv.addWeighted( src1, alpha, src2, beta, gamma[, dst[, dtype]] ) -> dst
參數(shù) | 說明 |
---|---|
src1 | 圖片1 |
alpha | 圖片1的權(quán)重 |
src2 | 圖片2 |
beta | 圖片2的權(quán)重 |
gamma | 添加到每個(gè)總和的標(biāo)量。一般為0 |
dst | 輸出圖片,Python版本不需要指定?? |
dtype | 輸出數(shù)組的可選深度,默認(rèn)即可 |
代碼示例
文章來源:http://www.zghlxwxcb.cn/news/detail-737923.html
import cv2
import matplotlib.pyplot as plt
# 加載兩張圖片
img1 = cv2.imread(filename="Lenna.png")
img2 = cv2.imread(filename="horses.jpg")
# 將兩張圖片都調(diào)整到640*640
shape1 = img1.shape # HWC
shape2 = img2.shape # HWC
max1 = max(shape1[0], shape1[1])
max2 = max(shape2[0], shape2[1])
img1 = cv2.copyMakeBorder(
src=img1,
top=int((max1 - shape1[0])/2),
bottom=int((max1 - shape1[0])/2),
left=int((max1 - shape1[1])/2),
right=int((max1 - shape1[1])/2),
borderType=cv2.BORDER_REFLECT101,
)
img1 = cv2.resize(src=img1, dsize=(640, 640), interpolation=cv2.INTER_LINEAR)
img2 = cv2.copyMakeBorder(
src=img2,
top=int((max2 - shape2[0])/2),
bottom=int((max2 - shape2[0])/2),
left=int((max2 - shape2[1])/2),
right=int((max2 - shape2[1])/2),
borderType=cv2.BORDER_REFLECT101,
)
img2 = cv2.resize(src=img2, dsize=(640, 640), interpolation=cv2.INTER_LINEAR)
# 按照比例將兩張圖片進(jìn)行混合
alpha = 0.5
beta = 1.0 - alpha
img_blending = cv2.addWeighted(src1=img1, alpha=alpha, src2=img2, beta=beta, gamma=.0)
# 繪制圖片
fig = plt.figure(figsize=(9, 3))
fig.suptitle(t="Blend two images")
ax1 = fig.add_subplot(1, 3, 1)
ax2 = fig.add_subplot(1, 3, 2)
ax3 = fig.add_subplot(1, 3, 3)
ax1.set_title(label="image1")
ax1.spines["top"].set_visible(b=False)
ax1.spines["bottom"].set_visible(b=False)
ax1.spines["left"].set_visible(b=False)
ax1.spines["right"].set_visible(b=False)
ax1.axes.xaxis.set_visible(b=False)
ax1.axes.yaxis.set_visible(b=False)
ax1.imshow(X=cv2.cvtColor(src=img1, code=cv2.COLOR_BGR2RGB))
ax2.set_title(label="image2")
ax2.spines["top"].set_visible(b=False)
ax2.spines["bottom"].set_visible(b=False)
ax2.spines["left"].set_visible(b=False)
ax2.spines["right"].set_visible(b=False)
ax2.axes.xaxis.set_visible(b=False)
ax2.axes.yaxis.set_visible(b=False)
ax2.imshow(X=cv2.cvtColor(src=img2, code=cv2.COLOR_BGR2RGB))
ax3.set_title(label="blending image")
ax3.spines["top"].set_visible(b=False)
ax3.spines["bottom"].set_visible(b=False)
ax3.spines["left"].set_visible(b=False)
ax3.spines["right"].set_visible(b=False)
ax3.axes.xaxis.set_visible(b=False)
ax3.axes.yaxis.set_visible(b=False)
ax3.imshow(X=cv2.cvtColor(src=img_blending, code=cv2.COLOR_BGR2RGB))
plt.show()
參考資料
- Computer Vision: Algorithms and Applications
- OpenCV文檔:Adding (blending) two images using OpenCV??
- OpenCV文檔:addWeighted() ??
收集整理和創(chuàng)作不易, 若有幫助??, 請(qǐng)幫忙點(diǎn)贊
???收藏
??, 謝謝!??????文章來源地址http://www.zghlxwxcb.cn/news/detail-737923.html
到了這里,關(guān)于詳解cv2.addWeighted函數(shù)【使用 OpenCV 添加(混合)兩個(gè)圖像-Python版本】的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!