コード例 #1
0
    public void setUserColor(int id, float r, float g, float b)
    {
        foreach (ThresholdSample ts in samples)
        {
            if (ts.id == id)
            {
                ts.userColor = new Color(r, g, b);
                return;
            }
        }

        ThresholdSample ts2 = new ThresholdSample();

        ts2.id        = id;
        ts2.userColor = new Color(r, g, b);
        samples.Add(ts2);
    }
コード例 #2
0
    public void setPoint(int id, float x, float y)
    {
        foreach (ThresholdSample ts in samples)
        {
            if (ts.id == id)
            {
                ts.point = new Vector3(x, y, 0);
                ts.color = getColor(ts.point);
                Debug.Log("Point " + " " + id + " " + x + " " + y + " " + ts.color);
                return;
            }
        }

        ThresholdSample ts2 = new ThresholdSample();

        ts2.id    = id;
        ts2.point = new Vector3(x, y, 0);
        ts2.color = getColor(ts2.point);
        Debug.Log("Add point " + " " + id + " " + x + " " + y + " " + ts2.color);
        samples.Add(ts2);
    }
コード例 #3
0
    public void UpdateThreshold(float sc)
    {
        Scale = sc;
        // Update samples with the point in the vector
        if (ThresholdPoints.Length > 0)
        {
            for (int i = 0; i < ThresholdPoints.Length; i++)
            {
                samples[i].point = new Vector3(
                    ThresholdPoints[i].localPosition.x / ThresholdSpace.rect.width,
                    ThresholdPoints[i].localPosition.y / ThresholdSpace.rect.height,
                    0);

                samples[i].active    = true;
                samples[i].userColor = getColor(samples[i].point);
            }
            for (int i = ThresholdPoints.Length; i < samples.Count; i++)
            {
                samples[i].active = false;
            }
        }

        InitTexture();

        int width  = (int)(originalImage.width * Scale);
        int height = (int)(originalImage.height * Scale);

        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                Color c = originalImage.GetPixel((int)(x / Scale), (int)(y / Scale));

                // Compute distance to each color
                float           d     = float.MaxValue;
                ThresholdSample tsmin = null;
                foreach (ThresholdSample ts in samples)
                {
                    float dref = (new Vector3(c.r, c.g, c.b) - new Vector3(ts.color.r, ts.color.g, ts.color.b)).sqrMagnitude;
                    if (dref < d)
                    {
                        tsmin = ts;
                        d     = dref;
                    }
                }

                if (tsmin != null)
                {
                    result.SetPixel(x, y, tsmin.userColor);
                }
                else
                {
                    result.SetPixel(x, y, c);
                }
            }
        }

        result.Apply();

        GetComponent <UnityEngine.UI.RawImage>().texture = result;
    }