使用scipy处理图片——旋转任意角度

本文介绍了如何使用scipy库中的ndimage模块进行图片的非90度旋转,包括指定角度旋转并保持原始尺寸,以及调整大小后旋转。示例代码展示了左旋转30度、右旋转30度、135度旋转以及保持原大小的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

《使用numpy处理图片——90度旋转》中,我们使用numpy提供的方法,可以将矩阵旋转90度。而如果我们需要旋转任意角度,则需要自己撸很多代码。如果我们使用scipy库提供的方法,则会容易很多。
需要注意的是,旋转导致原始的图片会“撑开”修改后的图片大小。当然我们也可以通过参数设置,让图片大小不变,但是会让部分图片显示不出来。

载入图片

import numpy as np
import PIL.Image as Image
import scipy.ndimage as ndimage

data = np.array(Image.open('the_starry_night.jpg'))

在这里插入图片描述

左旋转30度,且重新调整图片大小

left30 = ndimage.rotate(data, 30)

Image.fromarray(left30).save('left30.png')

在这里插入图片描述

右旋转30度,且重新调整图片大小

right30 = ndimage.rotate(data, -30)

Image.fromarray(right30).save('right30.png')

在这里插入图片描述

左旋转135度,保持图片大小不变

注意我们给reshape参数传递了False,即不调整图片大小

left135 = ndimage.rotate(data, 135, reshape=False)

Image.fromarray(left135).save('left135.png')

在这里插入图片描述

右旋转135度,保持图片大小不变

right135 = ndimage.rotate(data, -135, reshape=False)

Image.fromarray(right135).save('right135.png')

在这里插入图片描述

代码地址

https://github.com/f304646673/scipy-ndimage-example/tree/main/rot

### 计算SFR的方法及其旋转算法实现 空间频率响应 (Spatial Frequency Response, SFR) 是衡量成像系统分辨率能力的重要指标之一。它描述了图像边缘锐度随空间频率变化的情况。以下是关于如何计算SFR以及其旋转算法的具体实现。 #### 1. SFR 的基本概念 SFR 可通过测量图像中的斜边(edge)来获取。通常情况下,这一过程涉及以下几个方面: - **Edge Profile**: 测量沿图像中一条线上的像素强度分布。 - **Line Spread Function (LSF)**: 对 Edge Profile 进行数值微分得到 LSF。 - **Modulation Transfer Function (MTF)**: 使用傅里叶变换将 LSF 转换到频域以获得 MTF[^1]。 #### 2. 斜边检测与方向调整 为了准确评估不同角度下的 SFR 性能,在实际应用中可能需要考虑图像内的斜边并非总是水平或垂直排列的情形。因此引入了针对任意方向上 SFR 的处理方式——即所谓的 “SFR Rotation”。 ##### (a)确定最佳拟合直线的角度 对于给定的一组数据点代表了一条理想化的边界位置,则可以通过最小二乘法找到最接近这些样本的整体趋势线,并由此得出该倾斜角θ作为后续分析的基础参数。 ```python import numpy as np from scipy.optimize import leastsq def fit_line_angle(points): """ Fit a line to the given points and calculate its angle. Args: points (list of tuples): List containing coordinates [(x0,y0),...,(xn,yn)] Returns: float: Angle in radians between fitted line and positive x-axis direction. """ def error(params, pts): m, c = params return [y-(m*x+c) for x, y in pts] initial_guess=[0.,np.mean([p[1]for p in points])] optimized_params=leastsq(error,initial_guess,args=(points))[0] slope=optimized_params[0] intercept=optimized_params[1] theta=np.arctan(slope) return theta ``` ##### (b)重新采样至标准坐标系下 一旦知道了具体的偏转程度之后就可以利用几何映射技术把原始资料转换成为新的参照框架之中去进一步执行标准化流程以便于统一比较各个方位的表现特性。 ```python def resample_to_standard(image_data, angle): """ Resamples image data so that edges align with cardinal axes. Args: image_data (numpy.ndarray): Input grayscale or binary edge map array. angle (float): The rotation angle required to straighten out slanted lines. Returns: numpy.ndarray: Rotated version of input where all significant transitions occur along horizontal/vertical directions only now. """ from scipy.ndimage.interpolation import rotate rotated_img = rotate(image_data,-angle*180./np.pi,reshape=True) return rotated_img ``` #### 3. 结果验证 最后一步是对整个程序链路的有效性和精确度加以检验确认无误后再投入正式运用阶段当中去。这包括但不限于对比理论预期值同实测所得之间的差异大小是否处于可接受范围内等问题探讨。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

breaksoftware

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值