Python uses OpenCV module to achieve image fusion

Three fusions

Note that when merging, the two images are generally the same size. If the sizes are different, you need to cut out a part of the large image first, merge it with the small image, and then replace the original large image as a whole Cut out the small image part.

"""
# @Time    :  2020/4/3
# @Author  :  JMChen
"""
import cv2 as cv

img1 = cv.imread('logo.png')
img2 = cv.imread('lena.png')
# 在lena.png获取和logo.png大小相同的ROI
rows, cols, channels = img1.shape
img_ROI1 = img2[0:rows, 0:cols]

img_ROI2 = cv.addWeighted(img1, 0.7, img_ROI1, 0.3, 0)
img2[0:rows, 0:cols] = img_ROI2

# 显示混合后的图片
cv.imshow('img2', img2)
cv.waitKey(0)

# 将两幅图像(lena.png)+ (logo.png)进行融合
img2 = cv.imread('lena.png')
# 1,在lena.png获取和logo.png大小相同的ROI
img_ROI1 = img2[0:rows, 0:cols]

# 2,基于logo.png的灰度图,利用简单的阈值分割创建一个掩模
img1_gray = cv.cvtColor(img1, cv.COLOR_BGR2GRAY)
ret, mask = cv.threshold(img1_gray, 10, 255, cv.THRESH_BINARY)
mask_inv = cv.bitwise_not(mask)

# 3,与掩模进行按位与操作,去掉logo中非0部分,得到新的图
new_img2 = cv.bitwise_and(img_ROI1, img_ROI1, mask=mask_inv)

# 4,将新图与logo相加,然后将这一部分替换掉原始图像的img_ROI1部分
dst = cv.add(img1, new_img2)
img2[0:rows, 0:cols] = dst

cv.imshow('res', img2)
cv.waitKey(0)
cv.destroyAllWindows()

# 实现另一种融合
img2 = cv.imread('lena.png')
img_ROI1 = img2[0:rows, 0:cols]

dst_1 = cv.addWeighted(img_ROI1, 0.55, dst, 0.45, 0)
img2[0:rows, 0:cols] = dst_1

cv.imshow('res_2', img2)
cv.waitKey(0)
cv.destroyAllWindows()

The effect is as follows:
caNsu
Insert picture description here

Related proportional parameters can be adjusted as needed

YES
Published 7 original articles · Like 8 · Visits 133

Guess you like

Origin blog.csdn.net/weixin_44983848/article/details/105411528