首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在opencv中合并一个RGBA和一个RGB图像

如何在opencv中合并一个RGBA和一个RGB图像
EN

Stack Overflow用户
提问于 2022-01-04 12:11:52
回答 1查看 1.2K关注 0票数 1

我有两张照片。在一幅图像中,通过以A图像格式编辑通道RGBA,白色像素被设置为透明(或无色)。假设这个图像名是image_rgba.png,另一个图像是image.jpg。现在,我希望使用下面的python代码将image_rgba.png映像放在image.jpg的特定位置

代码语言:javascript
运行
复制
import cv2
import numpy as np
import matplotlib.pyplot as plt

image = cv2.imread('image.jpg')
label = cv2.imread('image_rgba.png')
label = cv2.cvtColor(label, cv2.COLOR_BGR2RGBA)

new_image = image.copy()
new_image = cv2.cvtColor(new_image, cv2.COLOR_BGR2RGBA)

new_image[200:290, 300:384, :] = label

当我写这个图像时,image_rgba.png的边缘是无色的,在输出图像中被设置为白色(以下图像中绿色框的白色边缘)。

我希望绿色框标签的白色边缘不显示,而是背景图像显示在该像素。我发现了similar问题,但它无助于解决我的问题。如果有人能帮我,我会很感激的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-04 13:38:13

这是你怎么做的。我不是OpenCV专家,所以可能有更简单的方法。代码中的注释解释了它的功能。总的想法是:

  1. 从PNG
  2. 提取α通道,提取具有相同大小的
  3. α混合的背景片段,
  4. 将结果放回背景

代码语言:javascript
运行
复制
import cv2

def alphaMerge(small_foreground, background, top, left):
    """
    Puts a small BGRA picture in front of a larger BGR background.
    :param small_foreground: The overlay image. Must have 4 channels.
    :param background: The background. Must have 3 channels.
    :param top: Y position where to put the overlay.
    :param left: X position where to put the overlay.
    :return: a copy of the background with the overlay added.
    """
    result = background.copy()
    # From everything I read so far, it seems we need the alpha channel separately
    # so let's split the overlay image into its individual channels
    fg_b, fg_g, fg_r, fg_a = cv2.split(small_foreground)
    # Make the range 0...1 instead of 0...255
    fg_a = fg_a / 255.0
    # Multiply the RGB channels with the alpha channel
    label_rgb = cv2.merge([fg_b * fg_a, fg_g * fg_a, fg_r * fg_a])

    # Work on a part of the background only
    height, width = small_foreground.shape[0], small_foreground.shape[1]
    part_of_bg = result[top:top + height, left:left + width, :]
    # Same procedure as before: split the individual channels
    bg_b, bg_g, bg_r = cv2.split(part_of_bg)
    # Merge them back with opposite of the alpha channel
    part_of_bg = cv2.merge([bg_b * (1 - fg_a), bg_g * (1 - fg_a), bg_r * (1 - fg_a)])

    # Add the label and the part of the background
    cv2.add(label_rgb, part_of_bg, part_of_bg)
    # Replace a part of the background
    result[top:top + height, left:left + width, :] = part_of_bg
    return result

background = cv2.imread('image.jpg')
# Read the image "unchanged" to get the alpha channel as well
label = cv2.imread('image_rgba.png', cv2.IMREAD_UNCHANGED)
result = alphaMerge(label, background, 100, 200)
cv2.imshow("result", result)
cv2.waitKey()

我在以下背景下进行了测试:

这个前景是:

结果如Python代码所示:

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70578600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档