I have a RGB image stored as a numpy array. I have a color array and these colors I will search in the image and replace those RGB values with a same scalar value. The rest of the RGB values that do not match shall simply be replaced by 0.
The colors I'm searching could be like the following,
colors = []
colors.append((69, 0, 9, 17))
colors.append((196, 127, 128,1))
colors.append((199, 5, 126, 19))
colors.append((55, 127, 126, 4))
colors.append((0, 127, 29, 2))
colors.append((68, 6, 124, 18))
The 4th values on each color is the value that will replace the corresponding RGB values.
I tried using np.asin
but it doesn't search for arrays. It only searches for scalars. Right now I am using for loop but its extremely slow.
for i in range(image.shape[0]):
for j in range(image.shape[1]):
match = -1
for k in range(len(colors)):
match = k
for l in range(3):
if image[i,j,l] != colors[k][l]:
match=-1
break
if match >=0 :
break
val = [0,0,0]
if match >= 0:
val = [colors[match][3],colors[match][3],colors[match][3]]
for l in range(3):
image[i,j,l] = val[l]
Any efficient approach will be very appreciated.