图像旋转变换的推导

xiaoxiao2021-03-01  17

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

那么,对于旋转变换呢?我们可以同样将其想象成二维平面上矢量的旋转。如下图所示,矢量[x1,y1]逆时针旋转 θ θ 度到了[x2,y2]。 设定矢量的长度为s,根据坐标系定义,我们可以得到: x2=scosβx2=s•cos⁡β

y2=ssinβ y 2 = s • s i n ⁡ β 根据上面的图形,有: β=α+θ β = α + θ 因此: x2=scos(α+θ) x 2 = s • c o s ⁡ ( α + θ ) y2=ssin(α+θ) y 2 = s • s i n ⁡ ( α + θ ) 根据初中所学的三角函数公式: sin(α+θ)=sinαcosθ+cosαsinθ s i n ⁡ ( α + θ ) = s i n α • c o s θ + c o s α • s i n θ cos(α+θ)=cosαcosθsinαsinθ c o s ⁡ ( α + θ ) = c o s α • c o s θ − s i n α • s i n θ 于是: x2=scosαcosθssinαsinθ x 2 = s • c o s α • c o s θ − s • s i n α • s i n θ y2=ssinαcosθ+scosαsinθ y 2 = s • s i n α • c o s θ + s • c o s α • s i n θ 由于: x1=scosα x 1 = s • c o s ⁡ α y1=ssinα y 1 = s • s i n ⁡ α 因此: x2=x1cosθy1sinθ x 2 = x 1 • c o s θ − y 1 • s i n θ y2=x1sinθ+y1cosθ y 2 = x 1 • s i n θ + y 1 • c o s θ 于是,上式写成矩阵乘法的形式如下: [x2y2]=[cosθsinθsinθcosθ][x1y1](77) (77) [ x 2 y 2 ] = [ c o s θ − s i n θ s i n θ c o s θ ] [ x 1 y 1 ] 我们来看看一个图像逆时针旋转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://www.6miu.com/read-3350013.html

最新回复(0)