2

OpenCV中的图像平移

 2 years ago
source link: https://xugaoxiang.com/2022/07/09/opencv-translation/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

平移就是矩阵的移动,通常,我们需要定义一个 变换矩阵,这是一个2行3列的矩阵

ddaa3e2448581684.png

矩阵中的 txty 分别代表 x 方向和 y 方向上平移的距离

平移是使用放射变换函数 cv2.warpAffine 来实现的,它的函数原型是

cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])
  • src 输入图像
  • M2行3列变换矩阵
  • dsize 输出图像的大小
  • dst 可选参数,输出图像,也可以作为返回值
  • flags 可选参数,插值方法
  • borderMode 可选参数,边界模式
  • borderValue 可选参数,边界填充值,默认是0
import cv2
import numpy as np
from sqlalchemy import column

image = cv2.imread("lenna.png")
cv2.imshow("original image", image)

column, row, channel = image.shape

# 变换矩阵,x放心移动100,y方向移动50
M = np.float32([[1, 0, 100], [0, 1, 50]])
dst=cv2.warpAffine(image, M, (column, row))
cv2.imshow('dst', dst)

cv2.waitKey(0)
cv2.destroyAllWindows()
82d101eda775ece2.png

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK