首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用二进制掩码查找边界体素(像素),从而只获得段面

如何使用二进制掩码查找边界体素(像素),从而只获得段面
EN

Stack Overflow用户
提问于 2021-02-19 11:14:22
回答 1查看 68关注 0票数 0

他是我在这里的第一个问题,所以请告诉我。我简要地描述了情况:有一个大小为512x512xN的体素集合(N是断层切片的数目)(新的BitArray (512x512xN))。作为一种分割算法的结果,得到了该集中填充体素的索引,使用BitArray数组表示所需的体素,其中值为true的索引为分段,false为背景。因此,我想知道如何使用二进制掩码来找到边界体素(像素),它存储它们的坐标(x,y,z)和背景信息(真-假),只得到段信封。下面是定义边界指数的代码,但这是不正确的:

代码语言:javascript
复制
private static async Task<BitArray> EdgeArray(Vector3Int size, BitArray data)
    {
        return await Task.Run(() =>
        {
            var edges = new BitArray(data.Length);
            var mask3d = new Dictionary<Vector3Int,bool>();

            Parallel.For(0, data.Count, i =>
            {
                lock (mask3d)
                {
                    mask3d.Add(size.GetIndex3D(i),data[i]);
                }
            });

            var voxels = mask3d.Keys.ToList();

            Parallel.For(0, voxels.Count, i =>
            {
                if (voxels[i].x == 0 || voxels[i].x == size.x - 1 || voxels[i].y == 0 ||
                    voxels[i].y == size.y - 1 || voxels[i].z == 0 || voxels[i].z == size.z - 1)
                {
                    if (mask3d[voxels[i]])
                    {
                        var index = voxels[i].ToIndex3D(size.x, size.y, size.z);
                        edges[index] = true;
                        Debug.Log(index);
                    }
                }
                else
                {
                    if (mask3d[voxels[i]] && (!mask3d[voxels[i-1]] || !mask3d[voxels[i+1]]))
                    {
                        var index = voxels[i].ToIndex3D(size.x, size.y, size.z);
                        edges[index] = true;
                        Debug.Log(index);
                    }
                }
            });
            return edges;
        });
    }

次要职能:

代码语言:javascript
复制
/// <summary>
    /// returns the index in the 3d representation
    /// </summary>
    /// <param name="vector3Int"> array size in 3d representation </param>
    /// <param name="index"> 1d index </param>
    /// <returns>index in the 3d representation</returns>
    public static Vector3Int GetIndex3D(this Vector3Int vector3Int, int index)
    {
        return new Vector3Int
        {
            z = index / (vector3Int.x * vector3Int.y),
            y = index % (vector3Int.x * vector3Int.y) / vector3Int.x,
            x = index % (vector3Int.x * vector3Int.y) % vector3Int.x
        };
    }
    
public static int ToIndex3D(this Vector3Int vector3Int, int width, int height, int depth)
    {
        return vector3Int.z * width * height + vector3Int.y * width + vector3Int.x*depth;
    }
EN

回答 1

Stack Overflow用户

发布于 2021-02-19 11:48:38

我认为GetIndex3D()和ToIndex3D()函数可能是错误的。ToIndex3D()应该是:

代码语言:javascript
复制
return vector3Int.z * width * height + vector3Int.y * width + vector3Int.x;

GetIndex3D()应该是:

代码语言:javascript
复制
z = index / (vector3Int.x * vector3Int.y),
y = (index / vector3Int.x) % vector3Int.y
x = index % vector3Int.x

(这是内存中未经测试的猜测,但我认为这是将3D数组转换为一维数组的一种好方法,反之亦然)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66276532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档