我正在尝试在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)
我的结果:
cv::Mat::copyTo
函数,这个函数在这篇帖子中有完美的解释。 - zindarod