I have an Image in an Numpy Array. I will replace an specific Color with Black and all others in White. For loops are to slow and my numpy condition ist not working.
All pixels that matches an array --> [121, 112, 131] must complete replace with another array --> [0, 0, 0]
All other with --> [255, 255, 255]
My results ends up with overseen pixels that matches with the First Channel [True, False, False]
Sorry for bad spelling, i hope my code ist better to understand.
My example result:
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:
There are Pixels left when the First Channel is equal to the target Color.
Target Pixel: [121, 112, 131]
Overseen Pixel: [121, 157, 96]