将绿色转换为红色 [英] Convert greenish to redish

查看:247
本文介绍了将绿色转换为红色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有52张标准游戏卡的图像.其中有些是黑色的,有些是红色的.经过训练的神经网络可以正确识别它们.现在事实证明,有时使用绿色代替红色.这就是为什么我要将所有绿色(红色)的图像转换为红色(红色)的原因.如果它们是黑色或红色,则应该对其进行太多或根本不做任何更改.

I have images of the stanrd 52 game cards. Some of them are black and some are red. A neural network has been trained on it to recognize them correctly. Now it turns out that sometimes green is used instead of red. That's why I want to convert all images that are green(ish) to red(ish). If they are blackish or redish they should not be changed much or at all if possible.

实现此目标的最佳方法是什么?

What's the best way to achieve this?

推荐答案

这是一种使用公差值来确定-ish因子并为其设置数学符号的方法-

Here's one approach using a tolerance value to decide on the -ish factor to set a mathematical notation to it -

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)

一种内存效率更高的方法是遍历这些选择性颜色,就像这样-

A more memory-efficient approach would be to loop through those selective colors, like so -

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")

输出-

这篇关于将绿色转换为红色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆