700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python图像几何变换_python-opencv 图像几何变换--缩放 平移 旋转

python图像几何变换_python-opencv 图像几何变换--缩放 平移 旋转

时间:2022-04-12 21:36:26

相关推荐

python图像几何变换_python-opencv 图像几何变换--缩放 平移 旋转

缩放

缩放是调整图片的大小,可以指定输出图像尺寸大小,也可以指定缩放比例。

opencv函数原型

cv2.resize(InputArray src, OutputArray dst, Size, fx, fy, interpolation)

InputArray src

输入图像

OutputArrzy dst

输出图像

Size

输出图像尺寸

fx, fy

x轴,y轴的缩放系数

interpolation

插值方式

interpolation插值方式:

插值方式有INTER_NEAREST 最近邻插值、INTER_LINEAR 双线性插值、INTER_AREA 像素区域重采样、INTER_CUBIC 4*4像素邻域双三次插值、 INTER_LANCZOS4 8*8像素邻域Lanczos插值。其中INTER_LINEAR为默认的插值方法,首选的插值方法是INTER_AREA。我们可以使用以下方法调整图像大小:

import cv2 as cv

img = cv.imread(r'Lena.png', 1)

res1 = cv.resize(img, None, fx=0.5, fy=0.5, interpolation=cv.INTER_AREA)

# Or

height, width = img.shape[ : 2]

res2 = cv.resize(img, (int(0.5*width), int(0.5*height)), interpolation=cv.INTER_AREA)

平移

在对图像作平移操作时,需创建变换矩阵。2行3列矩阵,决定了何种变换。M矩阵则表示水平方向上平移为x而竖直方向上的平移距离为y。

import numpy as np

import cv2 as cv

img = cv.imread(r'Lena.png', 1)

rows, cols, channels = img.shape

M = np.float32([[1,0,100],[0,1,50]])

res = cv.warpAffine(img, M, (cols, rows))

# cv.warpAffine()第三个参数为输出的图像大小,值得注意的是该参数形式为(width, height)。

cv.imshow('img', res)

cv.waitKey(0)

cv.destroyAllWindows()

效果图

旋转

旋转需变换矩阵,而函数cv.getRotationMatrix2D()会找到此转换矩阵。

import cv2 as cv

img = cv.imread(r'Lena.png', 1)

rows, cols, channels = img.shape

rotate = cv.getRotationMatrix2D((rows*0.5, cols*0.5), 45, 1)

'''

第一个参数:旋转中心点

第二个参数:旋转角度

第三个参数:缩放比例

'''

res = cv.warpAffine(img, rotate, (cols, rows))

cv.imshow('img', img)

cv.imshow('res', res)

cv.waitKey(0)

cv.destroyAllWindows()

效果图

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。