首页 > 解决方案 > 在 Unity 中的纹理中创建边界框

问题描述

我想在纹理中制作一个边界框。

这个纹理是图像分割的结果

我每次都将分割结果制作成纹理,并尝试为这个过程做一个边界框

我有纹理的像素特定值,但这每次都会随机变化。

所以,我试图用 bfs 算法找到像素值。

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));
                }
            }
        }
    }

我想着如何找到顶部和底部。

但这似乎太慢了。

如何轻松获得边界框?

我正在使用分割的结果值来创建纹理

        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;

我想做的类似于下图。

在此处输入图像描述

在此处输入图像描述 你能告诉我如何制作或参考哪些网站吗?

标签: unity3dtexturesbounding-box

解决方案


推荐阅读