我需要在OpenCV中将所有具有特定RGB值的像素替换为另一种颜色。
我尝试了一些解决方案,但都没有奏效。
如何以最佳方式实现这一目标?
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)
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 ]
然后找到掩模中所有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)
img_cp = image.copy()
img_cp[coords_x,coord_y,:]=new_color
您选择的图像像素现在具有新的颜色。 您可以使用matplotlib.pyplot.imshow(img_cp)
进行检查。