需要按照相邻顺序重新排列像素集

时间:2018-07-23 13:00:45

标签: c# unity3d

在Unity项目中,我处理了一张图像以获取队列中的边框像素。问题是,这些像素形成边界,但不是线性顺序。我无法使用它们来追踪边界。我需要对它们进行排序,以便从第一个像素开始,其余所有像素都应该线性地依次排列。

所以我做了什么:

  1. 用绿色填充图像中的对象,同时将变成绿色的像素存储在队列中。我在屏幕上看到的是白色背景上的绿色形状。

  2. 在此队列上使用以下代码来获取边框像素:

    private static Queue<Point> BorderFill(Texture2D tex1, Queue<Point> qu1)
    {
        var qt = new Queue<Point>();
    
        while (qu1.Count > 0) 
        {
            Point c1    = qu1.Dequeue ();
            Color right = tex1.GetPixel (c1.x + 1, c1.y);
            Color left  = tex1.GetPixel(c1.x-1, c1.y);
            Color up    = tex1.GetPixel(c1.x, c1.y+1);
            Color down  = tex1.GetPixel (c1.x, c1.y - 1);
    
            if (left != Color.green || up != Color.green || down != Color.green || right != Color.green)
                qt.Enqueue (new Point (c1.x, c1.y));
        }
         return qt;
    }
    
  3. 此功能为我提供了具有边框像素的队列。

  4. 我需要重新排序。

我正在用C#编码。

0 个答案:

没有答案