Unity_检测颜色相似度

10 篇文章 0 订阅

如何检测颜色相似度

先将颜色从RGB颜色空间转至HSV颜色空间
Unity中已经包含一个从RGB转至HSV的方法——Color.RGBToHSV(Color, out h, out s, out v); 其中H,S,V分别代表颜色的色调,饱和度,值的输出量,输出后的值范围为0至1,为当前H,S,V值在HSV颜色空间中的百分比,如果需要获取到详细值则需要乘以H,S,V的最大值(H—360,S—255,V—255)

具体实现

  1. 首先需要先获取到屏幕坐标上的颜色值,由于实时获取可能会造成一定的卡顿,所以在Update中设置检测间隔时间
public class GetScreenColor : MonoBehaviour
{
    //当前获取到的屏幕颜色
    public Color CurrentColor;
    //屏幕坐标位置
    public Vector3 HitPoint;

    private float ScanTime;
    private float NextScanTime;

    //颜色检测标准
    public Color32 ColorStandard = new Color32(169, 58, 61, 255);

    // Use this for initialization
    void Start ()
    {
        ScanTime = 5.0f;
    }
	
	// Update is called once per frame
	void Update ()
    {
        //每隔五秒进行一次颜色检测
        NextScanTime += Time.deltaTime;
        if (NextScanTime >= 5.0f)
        {
            NextScanTime = 0;
            StartCoroutine(CaptureScreenShot());
        }
        HitPoint = Camera.main.WorldToScreenPoint(transform.position);

    }

    /// <summary>
    /// 获取当前屏幕坐标上的颜色值
    /// </summary>
    /// <returns></returns>
    IEnumerator CaptureScreenShot()
    {
        yield return new WaitForEndOfFrame();

        Texture2D m_texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
        m_texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
        m_texture.Apply();
        Color color = m_texture.GetPixel((int)HitPoint.x, (int)HitPoint.y);
        CurrentColor = color;
//        GetComponent<Renderer>().material.color = color;
        StopCoroutine(CaptureScreenShot());
    }
}

  1. 对于获取到的颜色和颜色检测标准进行相似度检测,此处H,S,V的差值可以自己设定
string ColorGap(Color32 _color)
    {
        Color32 currentColor = CurrentColor;
        float H, S, V;
        float h, s, v;
         //将检测颜色标准和当前颜色由RGB颜色空间转换到HSV颜色空间
            Color.RGBToHSV(_color, out H, out S, out V);
            Color.RGBToHSV(currentColor, out h, out s, out v);
            //获取当前颜色HSV值和颜色标准HSV值的差,并设置为绝对值
            float ClanpH = Mathf.Abs(h * 360 - H * 360);
            float ClanpS = Mathf.Abs(s * 255 - S * 255);
            float ClanpV = Mathf.Abs(v * 255 - V * 255);
            //向下取整数
            ClanpH = Mathf.Floor(ClanpH);
            ClanpS = Mathf.Floor(ClanpS);
            ClanpV = Mathf.Floor(ClanpV);
            //根据当前HSV值的差来判断当前颜色和颜色检测标准的相似度
            if (ClanpH <= 20 && ClanpS <= 50 && ClanpV < 30)
            {
                return "red";
            }
        return "none";
    }
完整脚本代码
public class GetScreenColor : MonoBehaviour
{
    //当前获取到的屏幕颜色
    public Color CurrentColor;
    //屏幕坐标位置
    public Vector3 HitPoint;

    private float ScanTime;
    private float NextScanTime;

    //颜色检测标准
    public Color32 ColorStandard = new Color32(169, 58, 61, 255);

    // Use this for initialization
    void Start ()
    {
        ScanTime = 5.0f;
    }
	
	// Update is called once per frame
	void Update ()
    {
        //每隔五秒进行一次颜色检测
        NextScanTime += Time.deltaTime;
        if (NextScanTime >= 5.0f)
        {
            NextScanTime = 0;
            StartCoroutine(CaptureScreenShot());
        }
        HitPoint = Camera.main.WorldToScreenPoint(transform.position);
		
		color = ColorGap(ColorStandard);
    }

    /// <summary>
    /// 颜色深度检测
    /// </summary>
    /// <param name="_color"></param>
    /// <returns></returns>
	string ColorGap(Color32 _color)
    {
        Color32 currentColor = CurrentColor;
        float H, S, V;
        float h, s, v;
         //将检测颜色标准和当前颜色由RGB颜色空间转换到HSV颜色空间
            Color.RGBToHSV(_color, out H, out S, out V);
            Color.RGBToHSV(currentColor, out h, out s, out v);
            //获取当前颜色HSV值和颜色标准HSV值的差,并设置为绝对值
            float ClanpH = Mathf.Abs(h * 360 - H * 360);
            float ClanpS = Mathf.Abs(s * 255 - S * 255);
            float ClanpV = Mathf.Abs(v * 255 - V * 255);
            //向下取整数
            ClanpH = Mathf.Floor(ClanpH);
            ClanpS = Mathf.Floor(ClanpS);
            ClanpV = Mathf.Floor(ClanpV);
            //根据当前HSV值的差来判断当前颜色和颜色检测标准的相似度
            if (ClanpH <= 20 && ClanpS <= 50 && ClanpV < 30)
            {
                return "red";
            }
        return "none";
    }

    /// <summary>
    /// 获取当前屏幕坐标上的颜色值
    /// </summary>
    /// <returns></returns>
    IEnumerator CaptureScreenShot()
    {
        yield return new WaitForEndOfFrame();

        Texture2D m_texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
        m_texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
        m_texture.Apply();
        Color color = m_texture.GetPixel((int)HitPoint.x, (int)HitPoint.y);
        CurrentColor = color;
//        GetComponent<Renderer>().material.color = color;
        StopCoroutine(CaptureScreenShot());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值