将绿色转换为红色
问题描述:
我拥有stanrd 52游戏卡的图像。其中一些是黑色的,一些是红色的。已经训练了一个神经网络来正确识别它们。现在事实证明,有时使用绿色而不是红色。这就是为什么我要将所有绿色(ish)图像转换为红色(ish)的原因。如果它们变黑或变红,则应尽可能不要变化太大或根本不变。将绿色转换为红色
什么是实现这一目标的最佳方式是什么?
答
下面是使用一种方法的公差值对-ish
因素决定设置一个数学符号,以它 -
def set_image(a, tol=100): #tol - tolerance to decides on the "-ish" factor
# define colors to be worked upon
colors = np.array([[255,0,0],[0,255,0],[0,0,0],[255,255,255]])
# Mask of all elements that are closest to one of the colors
mask0 = np.isclose(a, colors[:,None,None,:], atol=tol).all(-1)
# Select the valid elements for edit. Sets all nearish colors to exact ones
out = np.where(mask0.any(0)[...,None], colors[mask0.argmax(0)], a)
# Finally set all green to red
out[(out == colors[1]).all(-1)] = colors[0]
return out.astype(np.uint8)
一个内存更有效的方法是依次通过选择性的那些颜色,像这样 -
def set_image_v2(a, tol=100): #tol - tolerance to decides on the "-ish" factor
# define colors to be worked upon
colors = np.array([[255,0,0],[0,255,0],[0,0,0],[255,255,255]])
out = a.copy()
for c in colors:
out[np.isclose(out, c, atol=tol).all(-1)] = c
# Finally set all green to red
out[(out == colors[1]).all(-1)] = colors[0]
return out
采样运行 -
输入图像:
from PIL import Image
img = Image.open('green.png').convert('RGB')
x = np.array(img)
y = set_image(x)
z = Image.fromarray(y, 'RGB')
z.save("tmp.png")
输出 -
同样,你需要定义什么是绿的,什么是瑞迪施,数学。 – Divakar
我会使用opencv库,在图像中读取为RGB(有三个红色,绿色蓝色通道),然后尝试切换R和G通道,看看是否能得到你想要的。 – flyingmeatball
我没有任何其他的定义,除了一切都是绿色的,比黑色或红色或白色更绿。只有那四种颜色是重要的。绿色,红色,白色和黑色。 – Nickpick