python用opencv实现图像旋转_Opencv-Python学习笔记五——图像翻转,平移,仿射及透视 warpAffine...

本文详细介绍了使用OpenCV-Python进行图像处理的各种变换,包括图像翻转(水平、垂直、水平垂直)、平移、旋转、仿射变换和透视变换。通过实例展示了如何使用cv2.flip、cv2.warpAffine、cv2.getRotationMatrix2D等函数,以及cv2.getPerspectiveTransform和cv2.warpPerspective进行图像变换。
摘要由CSDN通过智能技术生成

本篇笔记主要记录Opencv里的图像翻转,平移,旋转,仿射及透视功能,主要是下面几个API:

cv2.flip() # 图像翻转

cv2.warpAffine() #图像仿射

cv2.getRotationMatrix2D() #取得旋转角度的Matrix

cv2.GetAffineTransform(src, dst, mapMatrix) #取得图像仿射的matrix

cv2.getPerspectiveTransform(src, dst) #取得图像透视的4个点起止值

cv2.warpPerspective() #图像透视

图像翻转 cv2.flip()

cv2.flip(src, flipCode[, dst]) → dst

src: 原始图像矩阵;

dst: 变换后的矩阵;

flipMode: 翻转模式,有三种模式

0 --- 垂直方向翻转; 1----- 水平方向翻转; -1:水平、垂直方向同时翻转

flipCode==0垂直翻转(沿X轴翻转),flipCode>0水平翻转(沿Y轴翻转),flipCode<0水平垂直翻转(先沿X轴翻转,再沿Y轴翻转,等价于旋转180°)

%matplotlib inline

from matplotlib import pyplot as plt

import numpy as np

import cv2

image = cv2.imread("aier.jpg")

# Flipped Horizontally 水平翻转

h_flip = cv2.flip(image, 1)

# Flipped Vertically 垂直翻转

v_flip = cv2.flip(image, 0)

# Flipped Horizontally & Vertically 水平垂直翻转

hv_flip = cv2.flip(image, -1)

plt.figure(figsize=(8,8))

plt.subplot(221)

plt.imshow(image[:,:,::-1])

plt.title('original')

plt.subplot(222)

plt.imshow(h_flip[:,:,::-1])

plt.title('horizontal flip')

plt.subplot(223)

plt.imshow(v_flip[:,:,::-1])

plt.title(' vertical flip')

plt.subplot(224)

plt.imshow(hv_flip[:,:,::-1])

plt.title('h_v flip')

# 调整子图间距

# plt.subplots_adjust(wspace=0.5, hspace=0.1)

plt.subplots_adjust(top=0.8, bottom=0.08, left=0.10, right=0.95, hspace=0,

wspace=0.35)

# plt.tight_layout()

plt.show()

cv2.flip()

图像平移,旋转,仿射  cv2.warpAffine()

cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])

src input image.

dst output image that has the size dsize and the same type as src .

M 2×3 transformation matrix.

dsize size of the output image.

flags combination of interpolation methods (see InterpolationFlags) and the optional flag WARP_INVERSE_MAP that means that M is the inverse transformation ( dst→src ).

borderMode pixel extrapolation method (see BorderTypes); when borderMode=BORDER_TRANSPARENT, it means that the pixels in the destination image corresponding to the "outliers" in the source image are not modified by the function.

borderValue value used in case of a constant border; by default, it is 0.

cv2.getRotationMatrix2D(center, angle, scale)

函数有三个参数:

center:图片的旋转中心

angle:旋转角度

scale:缩放比例,该例中0.5表示我们缩小一半

%matplotlib inline

from matplotlib import pyplot as plt

import cv2

import numpy as np

img = cv2.imread('aier.jpg')

rows,cols = img.shape[:2]

# 定义平移矩阵,需要是numpy的float32类型

# x轴平移200,y轴平移100, 2*3矩阵

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

# 用仿射变换实现平移

img_s = cv2.warpAffine(img, M, (cols, rows), borderValue=(155, 150, 200))

# 第一个参数旋转中心,第二个参数旋转角度,第三个参数:缩放比例, 生成一2*3的矩阵

M = cv2.getRotationMatrix2D((cols/2,rows/2),90,1)

M1 = cv2.getRotationMatrix2D((cols/2,rows/2),180,1)

M2 = cv2.getRotationMatrix2D((cols/2,rows/2),60,1)

print(M)

'''

[[ 6.123234e-17 1.000000e+00 1.500000e+02]

[-1.000000e+00 6.123234e-17 6.500000e+02]]

'''

# 第三个参数:变换后的图像大小

img_tra = cv2.warpAffine(img,M,(cols,rows))

img_tra1 = cv2.warpAffine(img,M1,(cols,rows))

img_tra2 = cv2.warpAffine(img,M2,(cols,rows), borderValue=(155, 100, 155))

plt.figure(figsize=(8,8))

plt.subplot(221)

plt.imshow(img[:,:,::-1])

plt.subplot(222)

plt.imshow(img_s[:,:,::-1])

plt.subplot(223)

plt.imshow(img_tra[:,:,::-1])

plt.subplot(224)

plt.imshow(img_tra2[:,:,::-1])

plt.subplots_adjust(top=0.8, bottom=0.08, left=0.10, right=0.95, hspace=0,

wspace=0.35)

plt.show()

平移,旋转

图像仿射

图像的旋转加上拉升就是图像仿射变换,仿射变化也是需要一个M矩阵就可以,但是由于仿射变换比较复杂,一般直接找很难找到这个矩阵,opencv提供了根据变换前后三个点的对应关系来自动求解M。这个函数是

M=cv2.getAffineTransform(pos1,pos2),其中两个位置就是变换前后的对应位置关系。输出的就是仿射矩阵M。然后在使用函数cv2.warpAffine()。

cv.GetAffineTransform(src, dst, mapMatrix) → None

Parameters: 变换前的三个点与其对应的变换后的点.

src – Coordinates of triangle vertices in the source image.

dst – Coordinates of the corresponding triangle vertices in the destination image.

The function calculates the 2*3 matrix of an affine transform.

AffineMatrix = cv2.getAffineTransform(np.array(SrcPointsA),

np.array(CanvasPointsA))

图像仿射示例图

%matplotlib inline

from matplotlib import pyplot as plt

import cv2

import numpy as np

img = cv2.imread('aier.jpg')

rows,cols = img.shape[:2]

pts1 = np.float32([[50,50],[200,50],[50,200]])

pts2 = np.float32([[10,100],[200,20],[100,250]])

M = cv2.getAffineTransform(pts1,pts2)

#第三个参数:变换后的图像大小

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

plt.subplot(121)

plt.imshow(img[:,:,::-1])

plt.subplot(122)

plt.imshow(res[:,:,::-1])

plt.show()

透视 Perspective

视角变换,需要一个3*3变换矩阵。在变换前后要保证直线还是直线。

构建此矩阵需要在输入图像中找寻 4个点,以及在输出图像中对应的位置。这四个点中的任意三个点不能共线。

## pts1 ==> pts2

pts1=np.float32([[56,65],[368,52],[28,387],[389,390]])

pts2=np.float32([[0,0],[300,0],[0,300],[300,300]])

M=cv2.getPerspectiveTransform(pts1,pts2)

cv2.getPerspectiveTransform(np.array(SrcPointsA), np.array(CanvasPointsA))

cv2.getPerspectiveTransform(src, dst) → retval

cv2.warpPerspective(Img, PerspectiveMatrix, (300, 300))

cv2.warpPerspective(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]]) → dst

src – input image.

dst – output image that has the size dsize and the same type as src .

M – 3*3 transformation matrix.

dsize – size of the output image.

flags – combination of interpolation methods (INTER_LINEAR or INTER_NEAREST) and the optional flag WARP_INVERSE_MAP, that sets M as the inverse transformation ( dst ---> src ).

borderMode – pixel extrapolation method (BORDER_CONSTANT or BORDER_REPLICATE).

borderValue – value used in case of a constant border; by default, it equals 0.

4个点前后映射示例图

%matplotlib inline

from matplotlib import pyplot as plt

import cv2

import numpy as np

img=cv2.imread('aier.jpg')

rows,cols,ch=img.shape

pts1=np.float32([[56,5],[368,5],[28,387],[389,390]])

pts2=np.float32([[0,0],[300,0],[0,300],[300,300]])

M=cv2.getPerspectiveTransform(pts1,pts2)

print(M)

dst=cv2.warpPerspective(img,M,(300,300))

plt.subplot(121),plt.imshow(img),plt.title('Input')

plt.subplot(122),plt.imshow(dst),plt.title('Output')

plt.show()

图像透视

本文主要内容就是这些,其他更深入功能待后续继续完善.....

[参考]

Geometric Transformations of Images

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值