gpt4 book ai didi

python - 如何在OpenCV中将某个RGB值的所有像素替换为另一个RGB值

转载 作者:行者123 更新时间:2023-12-01 06:28:27 27 4
gpt4 key购买 nike

我需要能够在 OpenCV 中将具有特定 RGB 值的所有像素替换为另一种颜色。

我尝试了一些解决方案,但没有一个对我有用。

实现这一目标的最佳方法是什么?

最佳答案

TLDR;使用 Numpy 将所有绿色像素设为白色:

import numpy as np

pixels[np.all(pixels == (0, 255, 0), axis=-1)] = (255,255,255)
<小时/>

我在这里举了一些改变颜色的其他方法的例子。首先,我将使用此图像介绍精确、具体的 RGB 值,就像您在问题中提出的那样。它的左侧有三个大块,分别是正红色、正绿色和正蓝色,右侧是这些颜色之间的三个渐变过渡:

enter image description here

这又是上面的最初答案:

#!/usr/bin/env python3

import cv2
import numpy as np

# Load image
im = cv2.imread('image.png')

# Make all perfectly green pixels white
im[np.all(im == (0, 255, 0), axis=-1)] = (255,255,255)

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

enter image description here

<小时/>

这次我定义了颜色名称以提高可读性和可维护性。最后一行是重点:

# Define some colours for readability - these are in OpenCV **BGR** order - reverse them for PIL
red = [0,0,255]
green = [0,255,0]
blue = [255,0,0]
white = [255,255,255]
black = [0,0,0]

# Make all perfectly green pixels white
im[np.all(im == green, axis=-1)] = white

相同的结果。

<小时/>

这次我制作了一个可重复使用的红色像素蒙版,可以在后续操作中使用。带有赋值 im[Rmask] = black 的最后一行现在特别容易阅读:

# Define some colours for readability - these are in OpenCV **BGR** order - reverse them for PIL
red = [0,0,255]
green = [0,255,0]
blue = [255,0,0]
white = [255,255,255]
black = [0,0,0]

# Make mask of all perfectly red pixels
Rmask = np.all(im == red, axis=-1)

# Make all red pixels black
im[Rmask] = black

enter image description here

<小时/>

这次我组合红色和蓝色像素的蒙版,以便您可以看到蒙版的威力。最后一行是重点:

# Define some colours for readability - these are in OpenCV **BGR** order - reverse them for PIL
red = [0,0,255]
green = [0,255,0]
blue = [255,0,0]
white = [255,255,255]
black = [0,0,0]

# Make mask of all perfectly red pixels and all perfectly blue pixels
Rmask = np.all(im == red, axis=-1)
Bmask = np.all(im == blue, axis=-1)

# Make all red or blue pixels black
im[Rmask | Bmask] = black

enter image description here

<小时/>

这次我将所有非红色像素变成黑色 - 希望您现在能够欣赏蒙版的力量。最后一行是重点:

# Define some colours for readability - these are in OpenCV **BGR** order - reverse them for PIL
red = [0,0,255]
green = [0,255,0]
blue = [255,0,0]
white = [255,255,255]
black = [0,0,0]

# Make mask of all perfectly red pixels
Rmask = np.all(im == red, axis=-1)

# Make all non-red pixels black
im[~Rmask] = black

enter image description here

<小时/>

到目前为止,我们只将一些选择的像素转换为单一的新颜色。如果我们想在一次传递中将某些像素设置为一种颜色,而将所有其他像素设置为不同颜色,该怎么办?最后一行是重点:

# Define some colours for readability - these are in OpenCV **BGR** order - reverse them for PIL
red = [0,0,255]
green = [0,255,0]
blue = [255,0,0]
white = [255,255,255]
black = [0,0,0]

# Make mask of all perfectly red pixels
Rmask = np.all(im == red, axis=-1)

# Make all red pixels white AND at same time everything else black
im = np.where(np.all(im == red, axis=-1, keepdims=True), white, black)

enter image description here

<小时/>

如果您想影响整个范围颜色,而不是特定的 RGB 值,请查看 herehere .

关键字:图像处理、Python、prime、改变颜色、改变颜色、prime。

关于python - 如何在OpenCV中将某个RGB值的所有像素替换为另一个RGB值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60018903/

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