📜  使用 OpenCV 进行图像翻译 | Python

📅  最后修改于: 2022-05-13 01:55:14.213000             🧑  作者: Mango

使用 OpenCV 进行图像翻译 | Python

平移是指对象的直线移动,即图像从一个位置到另一个位置。如果我们知道水平和垂直方向的偏移量,比如 (tx, ty),那么我们可以制作一个变换矩阵,例如  \begin{bmatrix}  1 & 0 & tx \\ 0 & 1 & ty \end{bmatrix}
其中 tx 表示沿 x 轴的偏移,ty 表示沿 y 轴的偏移,即我们需要在该方向上偏移的像素数。
现在,我们可以使用cv2.wrapAffine()函数来实现这些翻译。此函数需要一个 2×3 数组。 numpy 数组应该是浮点类型。

以下是图像翻译的Python代码:

import cv2
import numpy as np
  
image = cv2.imread('C:\\gfg\\tomatoes.jpg')
  
# Store height and width of the image
height, width = image.shape[:2]
  
quarter_height, quarter_width = height / 4, width / 4
  
T = np.float32([[1, 0, quarter_width], [0, 1, quarter_height]])
  
# We use warpAffine to transform
# the image using the matrix, T
img_translation = cv2.warpAffine(image, T, (width, height))
  
cv2.imshow("Originalimage", image)
cv2.imshow('Translation', img_translation)
cv2.waitKey()
  
cv2.destroyAllWindows()

输出:

图像翻译的优点/应用是:

  • 隐藏图像的一部分
  • 裁剪图像
  • 移动图像
  • 使用循环中的图像翻译动画图像。