【问题标题】:How to replace all pixels of a certain RGB value with another RGB value in OpenCV如何在OpenCV中用另一个RGB值替换某个RGB值的所有像素
【发布时间】:2020-05-18 00:13:33
【问题描述】:

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

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

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

【问题讨论】:

  • 尝试用您尝试过的内容更新问题。
  • 链接失效
  • 我知道,它大约在 15 分钟前工作。我认为它暂时下降了
  • 如果您有多个替换颜色,LUT 是一种有效的方法

标签: python image opencv colors pixel


【解决方案1】:

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

import numpy as np

pixels[np.all(pixels == (0, 255, 0), axis=-1)] = (255,255,255)

我在这里做了一些改变颜色的其他方法的例子。首先,我将使用此图像介绍您在问题中提出的确切、特定的 RGB 值。它在左边有三个大块,分别是红色、绿色和蓝色,右边是这些颜色之间的三个渐变:

这是上面的初步答案:

#!/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)


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

# 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


这一次我结合一个红色和蓝色像素的蒙版,这样你就可以看到蒙版的威力。最后一行是重点:

# 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


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

# 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


到目前为止,我们只将一些像素选择为一种新颜色。如果我们想在一次通过中使一些像素成为一种颜色,而让所有其他像素成为不同的颜色怎么办?最后一行是重点:

# 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)


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

关键字:图像处理、Python、prime、change colour、change color、prime。

【讨论】:

  • 谢谢!有没有办法用一系列颜色做到这一点?所以不是绿色,而是替换所有接近绿色的颜色?
  • 是的,当然。看看这里。它从 PIL 开始,但这并没有什么区别——技术是一样的。我会在接下来的一两天内让这个答案更全面。 stackoverflow.com/a/52183666/2836621
  • 谢谢!这不是我打算做的,但面具似乎是一个很好的方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-08
  • 2012-02-14
  • 1970-01-01
  • 2015-11-27
  • 2019-09-14
  • 2020-08-31
相关资源
最近更新 更多