作者:采石工
链接:https://www.zhihu.com/question/49571479/answer/116840869
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
在NumPy中可以利用如下的函数进行数组或矩阵的连接:
hstack : Stack arrays in sequence horizontally (column wise).
vstack : Stack arrays in sequence vertically (row wise).
dstack : Stack arrays in sequence depth wise (along third axis).
concatenate : Join a sequence of arrays together.
r_ : Translates slice objects to concatenation along the first axis.
c_ : Translates slice objects to concatenation along the second axis.
题主需要的是在第三维(或轴)上的连接,见如下的代码:
<pre><code>
#coding=utf-8importnumpyasnp# 因为是生成随机数做测试,设置固定随机数种子,可以保证每次结果一致
np.random.seed(0)
RGB=np.random.randint(0,255,(5,5,3))
alpha=np.random.randint(0,255,(5,5))
RGBA=np.dstack((RGB,alpha))
print('RGB = \n {}'.format(RGB))
print('alpha = \n {}'.format(alpha))
print('RGBA = \n {}'.format(RGBA))
print('RGBA[:, :, 3] = \n {}'.format(RGBA[:,:,3]))#coding=utf-8importnumpyasnp# 因为是生成随机数做测试,设置固定随机数种子,可以保证每次结果一致
np.random.seed(0)
RGB=np.random.randint(0,255,(5,5,3))
alpha=np.random.randint(0,255,(5,5))
RGBA=np.dstack((RGB,alpha))
print('RGB = \n {}'.format(RGB))
print('alpha = \n {}'.format(alpha))
print('RGBA = \n {}'.format(RGBA))
print('RGBA[:, :, 3] = \n {}'.format(RGBA[:,:,3]))
</code></pre>
这是一个代码区块。
网友评论