簡體   English   中英

如何將OpenCV中某個RGB值的所有像素替換為另一個RGB值

[英]How to replace all pixels of a certain RGB value with another RGB value in OpenCV

我需要能夠用 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)

在此處輸入圖片說明


如果您想影響整個顏色范圍,而不是特定的 RGB 值,請查看此處此處

關鍵詞:圖像處理,Python,素數,改變顏色,改變顏色,素數。

假設您需要更改的“某些”像素具有以下 RGB 值:[r,g,b] 即像素具有 R=r、G=g 和 B=b 作為顏色值。

首先,您需要創建一個與圖像大小相同的二維蒙版。 讓大小為(X,Y)。 面具應該:

  1. 如果圖像中的相應像素具有 RGB 通道==[r,g,b],則在索引 (x1,y1) 處具有值 1 或 True
  2. 如果圖像中的相應像素具有 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 ] 


然后找到蒙版中所有 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

您在圖像中選擇的像素現在有新的 colors。 您可以查看matplotlib.pyplot.imshow(img_cp)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM