取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)
Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐