如何在OpenCV中用另一个RGB值替换某个RGB值的所有像素
How to replace all pixels of a certain RGB value with another RGB value in OpenCV
我需要能够用 OpenCV 中的另一种颜色替换具有特定 RGB 值的所有像素。
我已经尝试了一些解决方案,但 none 对我有用。
实现此目标的最佳方法是什么?
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 值,请查看 here and here.
关键字:图像处理,Python,素数,变色,变色,素数。
假设您需要更改的 'certain' 个像素具有以下 RGB 值:
[r,g,b] 即像素具有 R=r、G=g 和 B=b 作为颜色值。
首先,您需要创建一个与您的图像大小相同的二维蒙版。让大小为(X,Y)。面具应该:
- 如果图像中的相应像素具有 RGB 通道,则在索引 (x1,y1) 处具有值 1 或 True==[r,g,b]
- 如果图像中的相应像素具有 RGB 通道,则索引 (x2,y2) 的值为 0 或 False!=[r,g,b]
要创建此蒙版:
old_color = [r,g,b]
new_color = [r2,g2,b2]
height, width, channels = numpy.shape(image)
mask = numpy.zeros((height,width))
# iterate over all pixels in the image and assign 0 to the mask(x,y) if image(x,y) has channels==old_color
mask= [[1 if np.all(channels==[old_color]) else 0 for channels in row ] for row in image ]
然后找到mask中所有1的坐标,这些就是你需要在图像中分配新颜色的坐标。只需使用 np.where() 即可找到坐标。
mask = numpy.array(mask) # make sure that mask is a numpy array not a list of lists
# numpy.where would not work otherwise
coords_x, coord_y = np.where(mask>0)
最后用新的RGB值更改图像中这些坐标上的RGB值:
img_cp = image.copy()
img_cp[coords_x,coord_y,:]=new_color
您在图像中选择的像素现在有了新的颜色。您可以使用 matplotlib.pyplot.imshow(img_cp)
查看
我需要能够用 OpenCV 中的另一种颜色替换具有特定 RGB 值的所有像素。
我已经尝试了一些解决方案,但 none 对我有用。
实现此目标的最佳方法是什么?
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 值,请查看 here and here.
关键字:图像处理,Python,素数,变色,变色,素数。
假设您需要更改的 'certain' 个像素具有以下 RGB 值: [r,g,b] 即像素具有 R=r、G=g 和 B=b 作为颜色值。
首先,您需要创建一个与您的图像大小相同的二维蒙版。让大小为(X,Y)。面具应该:
- 如果图像中的相应像素具有 RGB 通道,则在索引 (x1,y1) 处具有值 1 或 True==[r,g,b]
- 如果图像中的相应像素具有 RGB 通道,则索引 (x2,y2) 的值为 0 或 False!=[r,g,b]
要创建此蒙版:
old_color = [r,g,b]
new_color = [r2,g2,b2]
height, width, channels = numpy.shape(image)
mask = numpy.zeros((height,width))
# iterate over all pixels in the image and assign 0 to the mask(x,y) if image(x,y) has channels==old_color
mask= [[1 if np.all(channels==[old_color]) else 0 for channels in row ] for row in image ]
然后找到mask中所有1的坐标,这些就是你需要在图像中分配新颜色的坐标。只需使用 np.where() 即可找到坐标。
mask = numpy.array(mask) # make sure that mask is a numpy array not a list of lists
# numpy.where would not work otherwise
coords_x, coord_y = np.where(mask>0)
最后用新的RGB值更改图像中这些坐标上的RGB值:
img_cp = image.copy()
img_cp[coords_x,coord_y,:]=new_color
您在图像中选择的像素现在有了新的颜色。您可以使用 matplotlib.pyplot.imshow(img_cp)