import numpy as np
import matplotlib.pyplot as pltimg = cv.imread("../SampleImages/pomeranian.png", cv.IMREAD_COLOR)#图像平移
#cv.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])
# M是仿射变换矩阵,对于平移来说M是一个2x3平移矩阵:
# [ 1 0 tx ]
# 0 1 ty
# dsize是输出图像的大小,(宽度,高度),宽度是列数,高度是行数
#参考资料:https://blog.csdn.net/Gaowang_1/article/details/103128590
rows,cols = img.shape[:2]
M = np.float32(([1,0,100],[0,1,20])) #构造平移矩阵
img_translated = cv.warpAffine(img, M, (cols,rows))fig,axes = plt.subplots(nrows=1, ncols=2, figsize=(10,10),dpi=100)
axes[0].imshow(img[:,:,::-1])
axes[0].set_title("Original")
axes[1].imshow(img_translated[:,:,::-1])
axes[1].set_title("Translated")
plt.show()