1.图像裁剪
import cv2
import numpy as np
img = cv2.imread('lena.jpg')
cv2.imshow('lena.jpg', img)
cv2.waitKey(0)
print(img.shape) # (200,200,3)
img_cut = img[30:200, 30:200]
cv2.imwrite('img_cut.jpg', img_cut)
结果:
2.图像尺寸变化(成倍扩大或缩小)
import cv2
import numpy as np
img = cv2.imread("lena.jpg")
cv2.imshow('lena.jpg', img)
cv2.waitKey(0)
print(img.shape) # (200,200,3)
new_img = cv2.resize(img, (40, 40), interpolation=cv2.INTER_AREA)
cv2.imwrite('new_img.jpg', new_img)
print(new_img.shape)
结果: