gpt4 book ai didi

python - 将 2 张图像与蒙版结合起来

转载 作者:太空宇宙 更新时间:2023-11-03 22:43:11 24 4
gpt4 key购买 nike

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

我已经尝试了 cv2.add()cv2.bitwise_and() 但这些都没有复制 Logo 。

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

最佳答案

我想以下是您的想法。

代码:

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

下面是在logo的蓝色 channel 上应用Otsu阈值得到的mask:

enter image description here

关于python - 将 2 张图像与蒙版结合起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51365126/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com