如何使用二进制掩码查找边界体素(像素),以获得仅分段表面



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

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

二次函数:

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

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

return vector3Int.z * width * height + vector3Int.y * width + vector3Int.x;

和GetIndex3D()应该是:

z = index / (vector3Int.x * vector3Int.y),
y = (index / vector3Int.x) % vector3Int.y
x = index % vector3Int.x

(这是一个未经测试的猜测从记忆,但我认为这是一个OK的方式来转换3D数组到1D数组,反之亦然)

最新更新