图像的二维旋转

Ttp*_*ala 3 python numpy image-processing linear-algebra transformation-matrix

尝试旋转 90 度时的图像。 左边一张是原图。我正在尝试以任何给定的角度旋转图像。我以图像的中心为原点旋转。

但是代码没有按预期进行旋转。我附上下面的代码。

import math
import numpy as np
import cv2

im = cv2.imread("Samples\\baboon.jpg", cv2.IMREAD_GRAYSCALE)
new = np.zeros(im.shape,np.uint8)

new_x = im.shape[0] // 2
new_y = im.shape[1] // 2

x = int(input("Enter the angle : "))

trans_mat = np.array([[math.cos(x), math.sin(x), 0],[-math.sin(x), math.cos(x), 0],[0, 0, 1]])

for i in range(-new_x, im.shape[0] - new_x):
    for j in range(-new_y, im.shape[1] - new_y):
        vec = np.matmul([i, j, 1], trans_mat)
        if round(vec[0] + new_x) < 512 and round(vec[1] + new_y) < 512:
            new[round(vec[0]+new_x), round(vec[1]+new_y)] = im[i+new_x,j+new_y]

cv2.imshow("rot",new)
cv2.imshow("1",im)
cv2.waitKey(0)
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)

Mad*_*ist 6

看起来您正在尝试实现最近邻重采样器。您正在做的是遍历图像并将每个输入像素映射到输出图像中的新位置。这可能会导致诸如像素错误地相互覆盖、输出像素留空等问题。

我建议(根据经验)您正在向后看问题。与其查看输入像素在输出中的位置,不如考虑每个输出像素在输入中的位置。这样,您就不会对最近的邻居产生歧义,并且整个图像数组将被填充。

您想围绕中心旋转。您正在使用的当前旋转矩阵围绕 旋转(0, 0)。为了弥补这一点,您需要将图像的中心平移到(0, 0)、旋转,然后再平移回来。我不会开发完整的仿射矩阵,而是向您展示如何手动执行各个操作,然后如何将它们组合到变换矩阵中。

手动计算

首先得到一个输入和输出图像:

im = cv2.imread("Samples\\baboon.jpg", cv2.IMREAD_GRAYSCALE)
new = np.zeros_like(im)
Run Code Online (Sandbox Code Playgroud)

然后确定旋转中心。清楚你的尺寸x通常是列 (dim 1),而不是行 (dim 0):

center_row = im.shape[0] // 2
center_col = im.shape[1] // 2
Run Code Online (Sandbox Code Playgroud)

计算图像中每个像素的径向坐标,形状为相应的维度:

row_coord = np.arange(im.shape[0])[:, None] - center_row
col_coord = np.arange(im.shape[1]) - center_col
Run Code Online (Sandbox Code Playgroud)

row_coordcol_coord是与输出图像中心的距离。现在计算它们在input 中的位置。请注意,我们可以使用广播来避免循环的需要。我在这里遵循您对角度定义的原始约定,并找到反向旋转以确定源位置。这里最大的区别是度数的输入被转换为弧度,因为这是三角函数所期望的:

angle = float(input('Enter Angle in Degrees: ')) * np.pi / 180.0 
source_row = row_coord * np.cos(angle) - col_coord * np.sin(angle) + center_row
source_col = row_coord * np.sin(angle) + col_coord * np.cos(angle) + center_col
Run Code Online (Sandbox Code Playgroud)

如果保证所有索引都在输入图像内,您甚至不需要预先分配输出。你可以从字面上做new = im[source_row, source_col]。但是,您需要屏蔽索引:

mask = source_row >= 0 & source_row < im.shape[0] & source_col >= 0 & source_col < im.shape[1]
new[mask] = im[source_row[mask].round().astype(int), source_col[mask].round().astype(int)]
Run Code Online (Sandbox Code Playgroud)

仿射变换

现在让我们来看看如何使用仿射变换。首先,您要从坐标中减去中心。假设您有一个列向量[[r], [c], [1]]。转换为零将是矩阵

[[r']    [[1  0 -rc]  [[r]
 [c']  =  [0  1 -cc] . [c]
 [1 ]]    [0  0  1 ]]  [1]]
Run Code Online (Sandbox Code Playgroud)

然后应用(向后)旋转:

[[r'']    [[cos(a) -sin(a) 0]  [[r']
 [c'']  =  [sin(a)  cos(a) 0] . [c']
 [ 1 ]]    [  0       0    1]]  [1 ]]
Run Code Online (Sandbox Code Playgroud)

最后,您需要转换回中心:

[[r''']    [[1  0 rc]  [[r'']
 [c''']  =  [0  1 cc] . [c'']
 [ 1  ]]    [0  0  1]]  [ 1 ]]
Run Code Online (Sandbox Code Playgroud)

如果将这三个矩阵按从右到左的顺序相乘,得到

   [[cos(a)   -sin(a)    cc * sin(a) - rc * cos(a) + rc]
M = [sin(a)    cos(a)   -cc * cos(a) - rc * sin(a) + cc]
    [  0         0                      1              ]]
Run Code Online (Sandbox Code Playgroud)

如果您构建一个完整的输出坐标矩阵而不是我们开始使用的子集数组,您可以使用np.matmul,也就是@运算符来为您进行乘法运算。但是,对于这样一个简单的情况,不需要这种复杂程度:

matrix = np.array([[np.cos(angle), -np.sin(angle),  col_center * np.sin(angle) - row_center * np.cos(angle) + row_center],
                   [np.sin(angle),  np.cos(angle), -col_center * np.cos(angle) - row_center * np.sin(angle) + col_center],
                   [0, 0, 1]])

coord = np.ones((*im.shape, 3, 1))
coord[..., 0, :] = np.arange(im.shape[0]).reshape(-1, 1, 1, 1)
coord[..., 1, :] = np.arange(im.shape[1]).reshape(-1, 1, 1)

source = (matrix @ coord)[..., :2, 0]
Run Code Online (Sandbox Code Playgroud)

其余的处理与手动计算非常相似:

mask = (source >= 0 & source_row < im.shape).all(axis=-1)
new[mask] = im[source[0, mask].round().astype(int), source_col[1, mask].round().astype(int)]
Run Code Online (Sandbox Code Playgroud)