Python Opencv实践 - 图像平移

import numpy as np
import matplotlib.pyplot as plt

img = 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()

 

你可能感兴趣的:(OpenCV实践-python,python,opencv,开发语言,图像处理,计算机视觉)