将 2 个图像与蒙版合并

saz*_*azr 5 python opencv

我试图在 OpenCV python 中的风景图像顶部绘制徽标。我找到了可以“混合”/水印图像的答案,但我不想使徽标透明,我只想在横向图像的顶部显示徽标并保持徽标不透明度(或不透明度)。

我已经尝试了这两种方法cv2.add()cv2.bitwise_and()但这些都没有复制徽标。

src = cv2.imread('../images/pan1.jpg')
logo = cv2.imread('../images/logo.png')  # the background is black which is good because I dont want the black parts

# Ensure the logo image is the same size as the landscape image
logo_resized = np.zeros(src.shape, dtype="uint8")
# Place the logo at the bottom right
logo_resized[ src.shape[0] - logo.shape[0] : src.shape[0], src.shape[1] - logo.shape[1] : src.shape[1]] = logo

# Convert the logo to single channel
logo_gray = cv2.cvtColor(logo_resized, cv2.COLOR_BGR2GRAY)
# Create a mask of the logo image
ret, mask = cv2.threshold(logo_gray, 1, 255, cv2.THRESH_BINARY)

# Combine the landscape and the logo images but use a mask so the black parts of logo don't get copied across
# combined = cv2.bitwise_and(logo_resized, logo_resized, mask=mask)
combined = cv2.add(src, logo_resized, mask=mask)
Run Code Online (Sandbox Code Playgroud)

我的结果:

在此处输入图片说明 在此处输入图片说明 在此处输入图片说明

Jer*_*uke 8

我想以下是您的想法。

代码:

room = cv2.imread('room.JPG' )
logo = cv2.imread('logo.JPG' )

#--- Resizing the logo to the shape of room image ---
logo = cv2.resize(logo, (room.shape[1], room.shape[0]))

#--- Apply Otsu threshold to blue channel of the logo image ---
ret, logo_mask = cv2.threshold(logo[:,:,0], 0, 255, cv2.THRESH_BINARY|cv2.THRESH_OTSU)
cv2.imshow('logo_mask', logo_mask)

room2 = room.copy() 

#--- Copy pixel values of logo image to room image wherever the mask is white ---
room2[np.where(logo_mask == 255)] = logo[np.where(logo_mask == 255)]

cv2.imshow('room_result.JPG', room2)
cv2.waitKey()
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)

结果:

最后结果:

在此处输入图片说明

下面是在logo的蓝色通道上应用Otsu阈值得到的掩码:

在此处输入图片说明