个性化阅读
专注于IT技术分析

使用OpenCV进行图像翻译|Python

点击下载

翻译术语”物体”是指物体即图像从一个位置到另一位置的直线移动。如果我们知道水平和垂直方向的位移量, 例如(tx, ty), 则可以制作一个转换矩阵, 例如

\ begin {bmatrix} 1&0&tx \\ 0&1&ty \ end {bmatrix}

其中tx表示沿x轴的位移, ty表示沿y轴的位移, 即我们需要在该方向上移动的像素数。

现在, 我们可以使用cv2.wrapAffine()实现这些翻译的功能。此功能需要2×3阵列。 numpy数组应为float类型。

以下是用于图像翻译的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()

输出如下:

使用OpenCV进行图像翻译|Python1

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

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

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。


赞(0)
未经允许不得转载:srcmini » 使用OpenCV进行图像翻译|Python

评论 抢沙发

评论前必须登录!