openCV学习笔记(四)--图像的几何变换Geometric Transformations of Images

图像的几何变换包括移位,旋转,仿射变换(affine transformation :不是特别懂这个是什么意思,根据例子的效果,自己也不知道该用怎样的语言来表述这个变换的具体含义),放大缩小等。

1)translation:改变object的位置

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('dog.png',0)
rows,cols=img.shape

# translation:改变object的位置
M=np.float32([[1,0,100],[0,1,50]])
dst=cv2.warpAffine(img,M,(cols,rows))


2)scale:放大或缩小

# scale:放大
res = cv2.resize(img,None,fx=1.5, fy=1.5, interpolation = cv2.INTER_CUBIC)
# or
# height, width = img.shape[:2]
# res = cv2.resize(img,(2*width, 2*height), interpolation = cv2.INTER_CUBIC)

3)旋转:rotation

#rotation:旋转
M = cv2.getRotationMatrix2D((cols/2,rows/2),180,1)
rota = cv2.warpAffine(img,M,(cols,rows))


旋转180


旋转90

4)  Affine Transformation:仿射变换
# Affine Transformation:仿射变换,保持直线性以及一条直线上点间距离的比值。
#                        不一定保留角度或者长度,但的确有平行线集合变换后彼此之间仍然平行的属性。
img1=cv2.imread('dog.png')
rows,cols,ch=img1.shape

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

M1 = cv2.getAffineTransform(pts1,pts2)

dst1 = cv2.warpAffine(img1,M1,(cols,rows))

plt.subplot(121),plt.imshow(img1),plt.title('Input')
plt.subplot(122),plt.imshow(dst1),plt.title('Output')
plt.show()


5) Perspective Transformation:透视变换

# Perspective Transformation:透视变换,我觉得就跟那放大镜看东西一样,尺寸不变,里面的内容变大,就好像离我们更近了。
img2 = cv2.imread('dog.png')
rows,cols,ch=img2.shape

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

M2 = cv2.getPerspectiveTransform(pts1,pts2)

dst2 = cv2.warpPerspective(img2,M2,(300,500))

plt.subplot(121),plt.imshow(img2),plt.title('Input')
plt.subplot(122),plt.imshow(dst2),plt.title('Output')
plt.show()


几何变换这节看着,比上一节要清晰那么一点点,可能是看懂了变换后的效果吧。

加油,继续学习。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值