opencv图像处理:二、图像几何变换

一、简介

这里主要介绍如何应用图像的几何变换,如平移,旋转,放射变换等。opencv提供2个转换函数分别是cv.warpAffinecv.warpPerspecive,可以使用这2个函数实现各种转换。

二、缩放

opencv使用cv.resize()函数实现缩放,对于该函数的插值参数比较合适的设置是cv.INTER_AREA用于缩小,cv.INTER_CUBIC(较耗时),cv.INTER_LINEAR用于放大。默认情况下插值使用cv.INTER_LINEAR
以下是示例代码:

import numpy as np
import cv2 as cv

img = cv.imread('./img/messi.jpg')
res = cv.resize(img,None,fx=0.5, fy=0.5, interpolation = cv.INTER_CUBIC)
#OR
#height, width = img.shape[:2]
#res = cv.resize(img,(0.5*width, 0.5*height), interpolation = cv.INTER_CUBIC)

cv.imshow("img", img)
cv.imshow("res", res)
cv.waitKey(0)
cv.destroyAllWindows()

请添加图片描述

三、平移

图像转换是移动目标位置,如果知道移动方向(x,y)以及需要移动到( t x , t y t_x, t_y tx,ty),那么需要创建的矩阵M如下:
M = [ 1 0 t x 0 1 t y ] M=[\begin{matrix} 1 & 0 & t_x \\ 0 & 1 & t_y \end{matrix} ] M=[1001txty]
以下为使用cv.warpAffine()函数实现示例:

cv.warpAffine() 计算原理:dst(x,y)=src(M11x+M12y+M13,M21x+M22y+M23)

import numpy as np
import cv2 as cv

img = cv.imread('./img/messi.jpg', 0)

rows, cols = img.shape
M   = np.float32([[1,0,100], [0,1,50]])
dst = cv.warpAffine(img, M, (cols, rows))

cv.imshow('img', img)
cv.imshow('dst', dst)
cv.waitKey(0)
cv.destroyAllWindows()

请添加图片描述

四、旋转

旋转依然使用cv.warpAffine()函数,图片θ角度需要创建M矩阵如下:
M = [ α β ( 1 − α ) ⋅ c e n t e r . x − β ⋅ c e n t e r . y − β α β ⋅ c e n t e r . x + ( 1 − α ) ⋅ c e n t e r . y ] M=[\begin{matrix} α & β & (1−α)⋅center.x−β⋅center.y \\ −β & α & β⋅center.x+(1−α)⋅center.y \end{matrix} ] M=[αββα(1α)center.xβcenter.yβcenter.x+(1α)center.y]
其中:
α = s c a l e ⋅ c o s θ β = s c a l e ⋅ s i n θ α=scale⋅cosθ \\ β=scale⋅sinθ α=scalecosθβ=scalesinθ
示例代码如下:

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

img = cv.imread('./img/messi.jpg', 0)
rows, cols = img.shape

# cols-1 and rows-1 are the coordinate limits.
M   = cv.getRotationMatrix2D(((cols - 1) / 2.0, (rows - 1) / 2.0), 90, 1)
dst = cv.warpAffine(img, M, (cols,rows))

cv.imshow('img', img)
cv.imshow('dst', dst)

cv.waitKey(0)
cv.destroyAllWindows()

请添加图片描述

五、仿射

仿射变换要点:
1、变换前是直线,变换后依然是直线。
2、直线比例保持不变。
示例代码如下:

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

img = cv.imread('./img/messi.jpg', 0)
rows, cols = img.shape


pts1 = np.float32([[50,50],  [200,50], [50,200]])
pts2 = np.float32([[10,100], [200,50], [100,250]])
M    = cv.getAffineTransform(pts1, pts2)
dst  = cv.warpAffine(img, M, (cols, rows))

cv.imshow('img', img)
cv.imshow('dst', dst)

cv.waitKey(0)
cv.destroyAllWindows()

请添加图片描述

六、透视

透视变换(Perspective Transformation)是指利用透视中心、像点、目标点三点共线的条件,按透视旋转定律使承影面(透视面)绕迹线(透视轴)旋转某一角度,破坏原有的投影光线束,仍能保持承影面上投影几何图形不变的变换。简而言之,就是将一个平面通过一个投影矩阵投影到指定平面上。

原理如下图所示:
在这里插入图片描述
透视需要4个图像输入点,使用cv.warpPerspective函数,示例代码如下:

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

img = cv.imread('./img/sudoku.png', 0)
rows, cols = img.shape

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

M = cv.getPerspectiveTransform(pts1,pts2)
dst = cv.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()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

icodekang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值