我遇到了以下Python脚本:
import numpy
image = numpy.array([[1,2,3],[4,5,6],[7,8,9]])
image_padded = numpy.zeros((image.shape[0] + 2, image.shape[1] + 2))
image_padded[1:-1, 1:-1] = image
我知道最后一个语句将等于3x3图像数组。我不明白的部分是索引是如何建立的:[1:-1, 1:-1]
。我们如何解释这个索引在做什么?你知道吗
Tags:
从这个thread
a[start:end] # items start through end-1 a[start:] # items start through the rest of the array a[:end] # items from the beginning through end-1 a[:] # a copy of the whole array
1表示最后一个元素,所以从1到二维的最后一个元素。你知道吗
1:-1
是一个不包括外部2项的切片。它以1
开始,在最后一个-1
之前结束:同样适用于二维索引。你知道吗
相邻的差异用
image[1:] - image[:-1]
这样的表达式表示。你知道吗相关问题 更多 >
编程相关推荐