图像的2D旋转

如何解决图像的2D旋转

Image while trying to do a 90 degrees rotation. Left one is original image.

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

但是代码没有按预期进行轮换。 我在下面附上代码。

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

解决方法

您似乎正在尝试实现最近邻居重采样器。您正在做的是浏览图像并将每个输入像素映射到输出图像中的新位置。这会导致诸如像素相互错误覆盖,输出像素为空等类似问题。

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

您要绕中心旋转。您正在使用的当前旋转矩阵绕(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] // 2
center_col = im.shape[1] // 2

计算图像中每个像素的径向坐标,并调整为相应的尺寸:

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

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

如果确保所有索引都落在输入图像之内,则您甚至不需要预先分配输出。您实际上可以只做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']    [[1  0 -rc]  [[r]
 [c']  =  [0  1 -cc] . [c]
 [1 ]]    [0  0  1 ]]  [1]]

然后(向后)旋转:

[[r'']    [[cos(a) -sin(a) 0]  [[r']
 [c'']  =  [sin(a)  cos(a) 0] . [c']
 [ 1 ]]    [  0       0    1]]  [1 ]]

最后,您需要翻译回中心:

[[r''']    [[1  0 rc]  [[r'']
 [c''']  =  [0  1 cc] . [c'']
 [ 1  ]]    [0  0  1]]  [ 1 ]]

如果将这三个矩阵从右到左依次相乘,则会得到

   [[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              ]]

如果您构建输出坐标的完整矩阵而不是我们开始使用的子集数组,则可以使用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,1]])

coord = np.ones((*im.shape,3,1))
coord[...,:] = np.arange(im.shape[0]).reshape(-1,1,1)
coord[...,:] = np.arange(im.shape[1]).reshape(-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)]
,

我认为这就是您要寻找的 Properly rotate image in OpenCV?

这是代码

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_image


new_image = rotimage(im)
cv2.imshow("1",new_image)
cv2.waitKey(0)
,

我尝试实现 Madphysicist 的矩阵乘法方法。这是实施,对于那些关心的人:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt
from pathlib import Path

path = Path(".")
img = plt.imread(path.resolve().parent / "img_align" / "faces_imgs" / "4.jpg")
angle = 15


def _transform(rot_mat,x,y):
    """
    conveninece method for matrix multiplication
    """
    return np.matmul(rot_mat,np.array([x,y,1]))


def rotate(img,angle):
    angle %= 360
    angle = np.radians(angle)
    new = np.zeros_like(img)
    cx,cy = tuple(x / 2 for x in img.shape[:2])

    # Angles are reverse as we are interpolating from destination to source
    rot_mat = np.array(
        [
            [np.cos(-angle),-np.sin(-angle),0],[np.sin(-angle),np.cos(-angle),1],]
    )

    rot_mat[0,2],rot_mat[1,_ = _transform(rot_mat,-cx,-cy)

    # build combined affine transformation matrrix
    rot_mat[0,2] += cx
    rot_mat[1,2] += cy

    coord = np.ones((*img.shape,1))  # [576x336x3x3x1]
    coord[...,:] = np.arange(img.shape[0]).reshape(-1,1)
    coord[...,:] = np.arange(img.shape[1]).reshape(-1,1)

    source = (rot_mat @ coord)[...,0]
    x_mask = source[...,0]
    y_mask = source[...,1]
    mask = (
        (x_mask >= 0)
        & (x_mask < img.shape[0])
        & (y_mask >= 0)
        & (y_mask < img.shape[1])
    ).all(axis=-1)

    # Clipping values to avoid IndexError
    new[mask] = img[
        x_mask[...,0][mask].round().astype(int).clip(None,img.shape[0] - 1),y_mask[...,1][mask].round().astype(int).clip(None,img.shape[1] - 1),]
    plt.imsave("test.jpg",new)


if __name__ == "__main__":
    rotate(img,angle)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-