【发布时间】:2018-07-17 01:48:34
【问题描述】:
我正在尝试在 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)
我的结果:
【问题讨论】:
-
你是否因为某种原因与 opencv 结了婚?这看起来与基本的 numpy 操作无关
-
@en_Knight 我更喜欢使用 OpenCV,因为我在 Python 中进行原型设计,然后在 C++ 中实现。但是 numpy 也可以。
-
你能提供两个原始图像吗?标志和景观。其实你只需要将图片中logo中非黑色的所有内容都设置为0,然后将其与logo一起添加
-
你想要python中的
cv::Mat::copyTo函数,这在this post中得到了完美的解释。