用蒙版组合2张图像

时间:2018-07-16 15:18:49

标签: 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)

我的结果:

enter image description here enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

我想以下是您的构想。

代码:

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()

结果:

最终结果:

enter image description here

以下是在徽标的蓝色通道上应用Otsu阈值获得的蒙版:

enter image description here