图像的二维旋转

http://img1.mukewang.com/646328c6000194de19161078.jpg

我正在尝试将图像旋转任何给定角度。我以图像的中心为原点旋转。


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


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()


jeck猫
浏览 122回答 3
3回答

慕少森

看起来您正在尝试实施最近邻重采样器。您正在做的是遍历图像并将每个输入像素映射到输出图像中的新位置。这可能会导致像素错误地相互覆盖、输出像素留空等问题。我会建议(根据经验)你正在向后看问题。您不应查看输入像素在输出中的最终位置,而应考虑每个输出像素在输入中的起始位置。这样,您就不会对最近的邻居产生歧义,并且整个图像数组都将被填充。你想绕中心旋转。您正在使用的当前旋转矩阵围绕 旋转(0, 0)。为了弥补这一点,您需要将图像的中心平移到(0, 0),旋转,然后再平移回来。我不会开发完整的仿射矩阵,而是会向您展示如何手动执行各个操作,然后如何将它们组合到变换矩阵中。人工计算首先得到一张输入输出图像:im = cv2.imread("Samples\\baboon.jpg", cv2.IMREAD_GRAYSCALE)new = np.zeros_like(im)然后确定旋转中心。清楚你的维度x通常是列 (dim 1),而不是行 (dim 0):center_row = im.shape[0] // 2center_col = im.shape[1] // 2计算图像中每个像素的径向坐标,整形为相应的维度:row_coord = np.arange(im.shape[0])[:, None] - center_rowcol_coord = np.arange(im.shape[1]) - center_colrow_coord是输出col_coord图像中距中心的距离。现在计算它们在输入中的位置。请注意,我们可以使用广播来避免循环的需要。我在这里遵循您最初的角度定义约定,并找到反向旋转以确定源位置。这里的最大区别在于度数的输入被转换为弧度,因为这是三角函数所期望的:angle = float(input('Enter Angle in Degrees: ')) * np.pi / 180.0&nbsp;source_row = row_coord * np.cos(angle) - col_coord * np.sin(angle) + center_rowsource_col = row_coord * np.sin(angle) + col_coord * np.cos(angle) + center_col如果保证所有索引都落在输入图像中,您甚至不需要预先分配输出。你可以从字面上做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)]仿射变换现在让我们看一下使用仿射变换。首先,您想从坐标中减去中心。假设您有一个列向量[[r], [c], [1]]。转换为零将是矩阵[[r']&nbsp; &nbsp; [[1&nbsp; 0 -rc]&nbsp; [[r]&nbsp;[c']&nbsp; =&nbsp; [0&nbsp; 1 -cc] . [c]&nbsp;[1 ]]&nbsp; &nbsp; [0&nbsp; 0&nbsp; 1 ]]&nbsp; [1]]然后应用(向后)旋转:[[r'']&nbsp; &nbsp; [[cos(a) -sin(a) 0]&nbsp; [[r']&nbsp;[c'']&nbsp; =&nbsp; [sin(a)&nbsp; cos(a) 0] . [c']&nbsp;[ 1 ]]&nbsp; &nbsp; [&nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; 1]]&nbsp; [1 ]]最后,你需要翻译回中心:[[r''']&nbsp; &nbsp; [[1&nbsp; 0 rc]&nbsp; [[r'']&nbsp;[c''']&nbsp; =&nbsp; [0&nbsp; 1 cc] . [c'']&nbsp;[ 1&nbsp; ]]&nbsp; &nbsp; [0&nbsp; 0&nbsp; 1]]&nbsp; [ 1 ]]如果你从右到左依次将这三个矩阵相乘,你会得到&nbsp; &nbsp;[[cos(a)&nbsp; &nbsp;-sin(a)&nbsp; &nbsp; cc * sin(a) - rc * cos(a) + rc]M = [sin(a)&nbsp; &nbsp; cos(a)&nbsp; &nbsp;-cc * cos(a) - rc * sin(a) + cc]&nbsp; &nbsp; [&nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]]如果您构建一个完整的输出坐标矩阵而不是我们开始的子集数组,您可以使用np.matmul,也就是@运算符为您进行乘法运算。对于这样一个简单的案例,不需要这种复杂程度:matrix = np.array([[np.cos(angle), -np.sin(angle),&nbsp; col_center * np.sin(angle) - row_center * np.cos(angle) + row_center],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[np.sin(angle),&nbsp; np.cos(angle), -col_center * np.cos(angle) - row_center * np.sin(angle) + col_center],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[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]处理的其余部分与手动计算非常相似: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)]

跃然一笑

我试图实现 Madphysicist 的矩阵乘法方法。这是实现,对于那些关心的人:#!/usr/bin/env python# -*- coding: utf-8 -*-import numpy as npimport matplotlib.pyplot as pltfrom pathlib import Pathpath = Path(".")img = plt.imread(path.resolve().parent / "img_align" / "faces_imgs" / "4.jpg")angle = 15def _transform(rot_mat, x, y):&nbsp; &nbsp; """&nbsp; &nbsp; conveninece method for matrix multiplication&nbsp; &nbsp; """&nbsp; &nbsp; return np.matmul(rot_mat, np.array([x, y, 1]))def rotate(img, angle):&nbsp; &nbsp; angle %= 360&nbsp; &nbsp; angle = np.radians(angle)&nbsp; &nbsp; new = np.zeros_like(img)&nbsp; &nbsp; cx, cy = tuple(x / 2 for x in img.shape[:2])&nbsp; &nbsp; # Angles are reverse as we are interpolating from destination to source&nbsp; &nbsp; rot_mat = np.array(&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [np.cos(-angle), -np.sin(-angle), 0],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [np.sin(-angle), np.cos(-angle), 0],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0, 0, 1],&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; )&nbsp; &nbsp; rot_mat[0, 2], rot_mat[1, 2], _ = _transform(rot_mat, -cx, -cy)&nbsp; &nbsp; # build combined affine transformation matrrix&nbsp; &nbsp; rot_mat[0, 2] += cx&nbsp; &nbsp; rot_mat[1, 2] += cy&nbsp; &nbsp; coord = np.ones((*img.shape, 3, 1))&nbsp; # [576x336x3x3x1]&nbsp; &nbsp; coord[..., 0, :] = np.arange(img.shape[0]).reshape(-1, 1, 1, 1)&nbsp; &nbsp; coord[..., 1, :] = np.arange(img.shape[1]).reshape(-1, 1, 1)&nbsp; &nbsp; source = (rot_mat @ coord)[..., :2, 0]&nbsp; &nbsp; x_mask = source[..., 0]&nbsp; &nbsp; y_mask = source[..., 1]&nbsp; &nbsp; mask = (&nbsp; &nbsp; &nbsp; &nbsp; (x_mask >= 0)&nbsp; &nbsp; &nbsp; &nbsp; & (x_mask < img.shape[0])&nbsp; &nbsp; &nbsp; &nbsp; & (y_mask >= 0)&nbsp; &nbsp; &nbsp; &nbsp; & (y_mask < img.shape[1])&nbsp; &nbsp; ).all(axis=-1)&nbsp; &nbsp; # Clipping values to avoid IndexError&nbsp; &nbsp; new[mask] = img[&nbsp; &nbsp; &nbsp; &nbsp; x_mask[..., 0][mask].round().astype(int).clip(None, img.shape[0] - 1),&nbsp; &nbsp; &nbsp; &nbsp; y_mask[..., 1][mask].round().astype(int).clip(None, img.shape[1] - 1),&nbsp; &nbsp; ]&nbsp; &nbsp; plt.imsave("test.jpg", new)if __name__ == "__main__":&nbsp; &nbsp; rotate(img, angle)

噜噜哒

我认为这就是您要寻找的 这是代码ang = int(input("Enter the angle : "))im = cv2.imread("Samples\\baboon.jpg", cv2.IMREAD_GRAYSCALE)def rotimage(image):    row,col = image.shape[0:2]    center=tuple(np.array([col,row])/2)    rot_mat = cv2.getRotationMatrix2D(center,ang,1.0)    new_image = cv2.warpAffine(image, rot_mat, (col,row))    return new_imagenew_image = rotimage(im)cv2.imshow("1",new_image)cv2.waitKey(0)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python