Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 创建三维二值图像_Python_Numpy_Indexing_Binary_Ndimage - Fatal编程技术网

Python 创建三维二值图像

Python 创建三维二值图像,python,numpy,indexing,binary,ndimage,Python,Numpy,Indexing,Binary,Ndimage,我有一个二维数组,a,包含一组100 x,y,z坐标: [[ 0.81 0.23 0.52] [ 0.63 0.45 0.13] ... [ 0.51 0.41 0.65]] 我想创建一个三维二值图像,b,在x、y、z维度上各有101个像素,坐标范围在0.00和1.00之间。 由a定义的位置处的像素值应为1,所有其他像素值应为0 我可以用b=np.zeros((101101101))创建一个正确形状的零数组,但是我如何分配坐标并将其切片以使用a创建这些零?您可以这样做- #

我有一个二维数组,
a
,包含一组100 x,y,z坐标:

[[ 0.81  0.23  0.52]
 [ 0.63  0.45  0.13]
 ...
 [ 0.51  0.41  0.65]]
我想创建一个三维二值图像,
b
,在x、y、z维度上各有101个像素,坐标范围在0.00和1.00之间。 由
a
定义的位置处的像素值应为1,所有其他像素值应为0


我可以用
b=np.zeros((101101101))
创建一个正确形状的零数组,但是我如何分配坐标并将其切片以使用
a
创建这些零?

您可以这样做-

# Get the XYZ indices
idx = np.round(100 * a).astype(int)

# Initialize o/p array
b = np.zeros((101,101,101))

# Assign into o/p array based on linear index equivalents from indices array
np.put(b,np.ravel_multi_index(idx.T,b.shape),1)

分配部分的运行时-

让我们使用更大的网格来计时

In [82]: # Setup input and get indices array
    ...: a = np.random.randint(0,401,(100000,3))/400.0
    ...: idx = np.round(400 * a).astype(int)
    ...: 

In [83]: b = np.zeros((401,401,401))

In [84]: %timeit b[list(idx.T)] = 1 #@Praveen soln
The slowest run took 42.16 times longer than the fastest. This could mean that an intermediate result is being cached.
1 loop, best of 3: 6.28 ms per loop

In [85]: b = np.zeros((401,401,401))

In [86]: %timeit np.put(b,np.ravel_multi_index(idx.T,b.shape),1) # From this post
The slowest run took 45.34 times longer than the fastest. This could mean that an intermediate result is being cached.
1 loop, best of 3: 5.71 ms per loop

In [87]: b = np.zeros((401,401,401))

In [88]: %timeit b[idx[:,0],idx[:,1],idx[:,2]] = 1 #Subscripted indexing
The slowest run took 40.48 times longer than the fastest. This could mean that an intermediate result is being cached.
1 loop, best of 3: 6.38 ms per loop

首先,从安全地将浮点数舍入整数开始。在上下文中,请参见问题

a_indices = np.rint(a * 100).astype(int)
接下来,将
b
中的索引分配给1。但是要小心使用普通的
列表
而不是数组,否则会触发使用。这种方法的性能似乎与其他方法的性能相当(感谢@Divakar!:-)

我创建了一个小示例,大小为10而不是100,尺寸为2而不是3,以说明:

>>> a = np.array([[0.8, 0.2], [0.6, 0.4], [0.5, 0.6]])
>>> a_indices = np.rint(a * 10).astype(int)
>>> b = np.zeros((10, 10))
>>> b[list(a_indices.T)] = 1
>>> print(b) 
[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  1.  0.  0.  0.]
 [ 0.  0.  0.  0.  1.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  1.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]

(a*100)。aType(int)
非常危险!你可能会得到错误的索引!试试看:
a=np.arange(101)/100
b=(100*a).astype(int)
(b==np.arange(101,dtype=int)).all()
,你会得到
False
!例如,由于浮点精度问题,数字28在
b
中出现两次。@Praveen Damn,你说得对!四舍五入是唯一的办法。更新了帖子。现在,它和你开始时的一样。你想让我删除这篇文章吗?我不会要求你删除它。你的
np.put
方法很有趣。我对这两者之间的时间分析很感兴趣,只要使用
list(a_index.T)
@Praveen当然,让我补充一下。@Praveen补充道。性能数据似乎是可比的。
>>> a = np.array([[0.8, 0.2], [0.6, 0.4], [0.5, 0.6]])
>>> a_indices = np.rint(a * 10).astype(int)
>>> b = np.zeros((10, 10))
>>> b[list(a_indices.T)] = 1
>>> print(b) 
[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  1.  0.  0.  0.]
 [ 0.  0.  0.  0.  1.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  1.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]