To create a bounding box in a texture in Unity

£可爱£侵袭症+ 提交于 2019-12-12 18:20:06

问题


I want to make a bounding box in texture.

This texture is the result of image Segmentation

I make Segmentation results into textures every time and I try to do a bounding box for this process

I have pixel-specific values for texture, but this changes randomly every time.

so, I tried to find pixel values with bfs algorithm.

public Queue<Node> SegNode = new Queue<Node>();

private bool[,] visit = new bool[256, 256];

private int[] dx = new int[4] { 0, 1, -1, 0 };
private int[] dy = new int[4] { 1, 0, 0, -1 };

public struct Node
{
    public int x, y;
    public float color;

    public Node(int x, int y, float color)
    {
        this.x = x;
        this.y = y;
        this.color = color;
    }
}

void bfs(int r, int c, float color, float[,,,] pixel)
{
    Queue<Node> q = new Queue<Node>();
    q.Enqueue(new Node(r, c, color));

    while (q.Count > 0)
    {
        Node curNode = q.Dequeue();
        SegNode.Enqueue(curNode);

        for (int i = 0; i < 4; i++)
        {
            int tr = curNode.x + dx[i];
            int tc = curNode.y + dy[i];

            if (tr >= 0 && tr < segmentationImageSize && tc >= 0 && tc < segmentationImageSize)
            {
                if (!visit[tr, tc] && pixel[0, tc, tr, 0] == color)
                {
                    visit[tr, tc] = true;
                    q.Enqueue(new Node(tr, tc, color));
                }
            }
        }
    }

And I thought about how to find the top and bottom.

But this seems to be too slow.

How can I get a bounding box easily?

I am using the result values for the segmentation to create a texture

        Texture2D SegResTexture = new Texture2D(widht, height, );
        for (int y = 0; y < SegResTexture.height; y++)
        {
            for (int x = 0; x < SegResTexture.width; x++)
            {
                SegResTexture.SetPixel(x, y, pixel[0, y, x, 0] < 0.1 ? maskTransparent : maskColor);

            }
        }
        SegResTexture.Apply();

        SegmentationRawIamge.GetComponent<RawImage>().texture = SegResTexture;

What I want to do is similar to the picture below.

Can you tell me how to make it or what sites to refer to?

来源:https://stackoverflow.com/questions/55273916/to-create-a-bounding-box-in-a-texture-in-unity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!