取list中对应索引数据的格式为:
list_a [start : end : step]
表示取出 list_a 中下标从 start 开始,到 end 结束,步长为 step 的元素,包含 start 和 step。
(1)例子1:取出list中下标为偶数的元素:
import numpy as np
a = np.random.randint(low=1, high=10, size=7)
print(a) # [7 5 6 2 6 8 3]
print(a[0:len(a):2]) # [7 6 6 3]
(2)例子2:通过下标操作对图像进行2倍比例的上/下采样:
import cv2
import numpy as np
def downSample(img):
return img[0:img.shape[0]:2, 0:img.shape[1]:2, :]
def upSample(img):
upImg = np.zeros((2 * img.shape[0], 2 * img.shape[1], img.shape[2]))
upImg[0:upImg.shape[0]:2, 0:upImg.shape[1]:2, :] = img
return upImg
if __name__ == '__main__':
img = cv2.imread("test.jpg")
downImg = downSample(img)
upImg = upSample(img)
print(img.shape) # (520, 520, 3)
print(downImg.shape) # (260, 260, 3)
print(upImg.shape) # (1040, 1040, 3)