import cv2
import numpy as np
1. 截取图像小片
# 截取一小片图像,行属于[100, 201),列属于[100, 251)
img = cv2.imread('monarch.png')
img_slice = img[100:201, 100:251, :]
# 显示img_slice
cv2.imshow('img_slice', img_slice)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出结果
2. 更新一片图像的值为纯绿色
# 设置一小片图像的值为纯绿色
# 行属于[100, 201),列属于[100, 251)
img_marked = cv2.imread('monarch.png')
img_marked[100:201, 100:251, :] = [0, 255, 0]
# 显示img_marked
cv2.imshow('img marked', img_marked)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出结果