数字图像处理--图像旋转变换的推导

前面我们提到了图像的缩放变换,可以用矩阵乘法的形式来表达变换后的像素位置映射关系。

那么,对于旋转变换呢?我们可以同样将其想象成二维平面上矢量的旋转。如下图所示,矢量 [ x 1 , y 1 ] [x_1,y_1] [x1,y1]逆时针旋转 θ θ θ度到了 [ x 2 , y 2 ] [x_2,y_2] [x2,y2]

在这里插入图片描述
设定矢量的长度为s,根据坐标系定义,我们可以得到:
x 2 = s • c o s ⁡ β y 2 = s • s i n ⁡ β \begin{aligned} x2=s• cos⁡β\\ y2=s• sin⁡β \end{aligned} x2=scosβy2=ssinβ

根据上面的图形,有:
β = α + θ β=α+θ β=α+θ

因此:
x 2 = s • c o s ⁡ ( α + θ ) y 2 = s • s i n ⁡ ( α + θ ) x2=s• cos⁡(α+θ) \\ y2=s• sin⁡(α+θ) x2=scos(α+θ)y2=ssin(α+θ)

根据初中所学的三角函数公式:
s i n ⁡ ( α + θ ) = s i n α • c o s θ + c o s α • s i n θ c o s ⁡ ( α + θ ) = c o s α • c o s θ − s i n α • s i n θ sin⁡(α+θ)=sinα•cosθ+cosα•sinθ \\ cos⁡(α+θ)=cosα•cosθ-sinα•sinθ sin(α+θ)=sinαcosθ+cosαsinθcos(α+θ)=cosαcosθsinαsinθ

于是:
x 2 = s • c o s α • c o s θ − s • s i n α • s i n θ y 2 = s • s i n α • c o s θ + s • c o s α • s i n θ x2=s•cosα•cosθ-s•sinα•sinθ\\ y2=s•sinα•cosθ+s•cosα•sinθ x2=scosαcosθssinαsinθy2=ssinαcosθ+scosαsinθ

由于:
x 1 = s • c o s ⁡ α y 1 = s • s i n ⁡ α x1=s• cos⁡α\\ y1=s• sin⁡α x1=scosαy1=ssinα

因此:
x 2 = x 1 • c o s θ − y 1 • s i n θ y 2 = x 1 • s i n θ + y 1 • c o s θ x2=x1•cosθ-y1•sinθ\\ y2=x1•sinθ+y1•cosθ x2=x1cosθy1sinθy2=x1sinθ+y1cosθ

于是,上式写成矩阵乘法的形式如下:
[ x 2 y 2 ] = [ c o s θ − s i n θ s i n θ c o s θ ] [ x 1 y 1 ] \begin{aligned} { \left[ \begin{array}{ccc} x2\\ y2\\ \end{array} \right ]}={ \left[ \begin{array}{ccc} cosθ& -sinθ\\ sinθ & cosθ\\ \end{array} \right ]}{ \left[ \begin{array}{ccc} x1\\ y1\\ \end{array} \right ]} \end{aligned} [x2y2]=[cosθsinθsinθcosθ][x1y1]

我们来看看一个图像逆时针旋转180度的情况。

import cv2

import numpy as np
import math

lenna = cv2.imread("lenna256.png", 0)
row, col = lenna.shape

lenna_rotation = np.zeros_like(lenna)

A = np.mat([[math.cos(math.pi), -math.sin(math.pi)], [math.sin(math.pi), math.cos(math.pi)]])

for r in range(row):
    for l in range(col):
        v = np.dot(A.I, np.array([r, l]).T)

        lenna_rotation[r, l] = lenna[int(v[0, 0]), int(v[0, 1])]

cv2.imshow("lenna", lenna)
cv2.imshow("rotation", lenna_rotation)
cv2.waitKey()

在这里插入图片描述
上面的图像宽度和高度是一样的,而且旋转角度是180度,比较特殊。在一般情况下,我们需要注意的是2点:一是旋转图像一般要将旋转中心设置在图像的中心点位置;二是图像旋转后,可能越过了原来的图像边界范围。这些都比较麻烦,好在opencv已经做好了这一切。

lenna = cv2.imread("lenna256.png", 0)
row, col = lenna.shape
M = cv2.getRotationMatrix2D((col // 2, row // 2), 70, 0.5)
dst = cv2.warpAffine(lenna, M, (col, row))
cv2.imshow("rotation", dst)
cv2.waitKey()

在这里插入图片描述

转载自:https://blog.csdn.net/saltriver/article/details/79680189

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值