Unity_检测颜色相似度

如何检测颜色相似度

先将颜色从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);

    }

    /// 
    /// 获取当前屏幕坐标上的颜色值
    /// 
    /// 
    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().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);
    }

    /// 
    /// 颜色深度检测
    /// 
    /// 
    /// 
	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";
    }

    /// 
    /// 获取当前屏幕坐标上的颜色值
    /// 
    /// 
    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().material.color = color;
        StopCoroutine(CaptureScreenShot());
    }
}

你可能感兴趣的:(unity,C#)