我有一个 Numpy 数组中的图像。
我将用黑色替换特定颜色,用白色替换所有其他颜色。
For 循环速度很慢,而且我的 numpy 条件不起作用。
与数组匹配的所有像素 --> [121, 112, 131] 必须完全替换为另一个数组 --> [0, 0, 0]
所有其他 --> [255, 255, 255]
我的结果最终得到与第一个通道匹配的监督像素 [True、False、False]
抱歉拼写错误,我希望我的代码更容易理解。
我的示例结果:
import numpy as np
from scipy import misc
file = misc.face()
img = np.array(file)
target_color = [121, 112, 131]
true_color = [0, 0, 0]
false_color = [255, 255, 255]
true_mask = np.all(img == target_color, axis=2)
false_mask = np.all(img != target_color, axis=2)
img[true_mask] = true_color
img[false_mask] = false_color
print(img)
Output:
array([[[ 0, 0, 0],
[255, 255, 255],
[255, 255, 255],
...,
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]],
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
...,
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]],
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
...,
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]],
...,
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
...,
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]],
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
...,
[121, 157, 96],
[255, 255, 255],
[255, 255, 255]],
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
...,
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]]], dtype=uint8)
Problem:
当第一个通道等于目标颜色时,还剩下像素。
Target Pixel: [121, 112, 131]
Overseen Pixel: [121, 157, 96]