【问题标题】:Set particular channel of Image to certain value according to other image根据其他图像将图像的特定通道设置为某个值
【发布时间】:2021-10-09 07:15:00
【问题描述】:

我有 this 图像,它有 4 个通道。我想要做的是,减少这张图片的不透明度。我希望图像保持透明,但是,我只想降低大众徽标部分的不透明度。 (使用 opencv/numpy 和 python)

这是我尝试过的:

logo = cv2.imread(FILE_PATH, cv2.IMREAD_UNCHANGED)
logo[:, :, 3] = 50

但这会将值 50 分配给整个图像,给我this 结果。请注意图像的其余部分不再透明(我希望它保持与原始图像相同。)

我想到了类似的东西:

#This is my logic NOT ANY CODE, I want to do something like this:

if (any of) other 3 channels are non zero, make alpha channel of that pixel = 50.
else, keep that pixel as it is. (This pixel would be part of logo)

有没有办法通过在 python 中使用 opencv / numpy 来实现这个结果?我的最后一个选择是遍历所有像素并查找上述条件,但我觉得这样效率低下。

This answer 正是我不想要的。我只想将徽标部分(彩色像素)的 alpha 通道设置为 50。

【问题讨论】:

  • logo[(logo[..., :3]!=0).any(2), 3] = 50。有关更多信息,请参阅this post
  • 我的回答解决了你的问题吗?如果是这样,请考虑接受它作为您的答案 - 通过单击计票旁边的空心对勾/复选标记。如果没有,请说出什么不起作用,以便我或其他人可以进一步帮助您。谢谢。 meta.stackexchange.com/questions/5234/…

标签: python image numpy opencv


【解决方案1】:

只是充实@yann-ziselman 的评论...

这是并排的图像的 RGBA 通道,带有红色边框,因此您可以在 Stack Overflow 的背景上看到范围:

按照您的要求操作如下:

import cv2

# Load image including alpha channel
im = cv2.imread('logo.png', cv2.IMREAD_UNCHANGED)

# Set alpha channel to 50 anywhere none of the BGR channels is non-zero
im[(im[..., :3]!=0).any(2), 3] = 50

# Save result
cv2.imwrite('result.png', im)

结果

结果并排分成 RGBA 通道

【讨论】:

    猜你喜欢
    • 2022-05-21
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 2020-05-07
    相关资源
    最近更新 更多