Python彩色图像旋转+平移变换数学原理及实现

编程入门 行业动态 更新时间:2024-10-28 12:19:48

Python彩色<a href=https://www.elefans.com/category/jswz/34/1771430.html style=图像旋转+平移变换数学原理及实现"/>

Python彩色图像旋转+平移变换数学原理及实现

一、引言

图像的几何变换在图像处理中被经常使用,其中图像旋转又是使用频率很高的变换,不仅应用于普通的图像的处理中,也会用于机器学习中的图像数据增强。图像旋转的数学原理很简单,就是简单的矩阵乘法。
本文给出了图像旋转的Python详细实现过程(纯手工),此外也给出了python内嵌函数roate的用法。

二、数学原理及公式

学过线性代数的童鞋都知道如下的矩阵表示旋转矩阵:

如果再加上平移,则可以用齐次坐标的表示形式:

假设旋转之前的坐标为(x,y),绕坐标原点旋转之后的坐标为(x*, y*),则旋转+平移变换公式的矩阵形式为:

对于图像旋转平移变换而言,其实就是像素的坐标旋转,像素跟着坐标走而已。

三、Python手工实现图像旋转+平移

1.单通道图像旋转平移变换
图像旋转是以图像中心为旋转轴,因此图像在旋转之前需要把像素坐标进行中心化,然后再进行变换,代码如下:

#im为单通道图像像素矩阵
#theta为旋转角度(单位是:度°)
#deltaX和deltaY是沿着两个坐标轴方向的平移量
#返回旋转+平移变换结果图像imRT
def SingleChannelRatTrans( im, theta, deltaX, deltaY ):[m, n] = np.shape( im )halfM = np.int( np.floor(m / 2) )halfN = np.int( np.floor(n / 2) )imRT = np.zeros( [ m, n] )angle = theta / 180 * 3.1415926for i in range(m):for j in range(n):ii = i - halfMjj = j - halfNi1 = round( ii * math.cos( angle ) + jj * math.sin( angle ) + halfM + deltaX )j1 = round( -ii * math.sin( angle ) + jj * math.cos( angle ) + halfN + deltaY )if i1 >= 0 and  i1 < m and j1 >= 0 and j1 < n:imRT[i][j] = im[i1][j1]imRT = imRT.clip( 0, 255 )#限制灰度值在0~255之间imRT = np.rint(imRT).astype('uint8')#设置像素的数据类型 return imRT

2.灰度图像或彩色图像旋转平移变换
灰度图像直接调用前面的单通道图像变换函数即可。
彩色图像针对R、G、B分量分别调用单通道图变换函数即可。

def ImageRatationTranslation( im, theta, deltaX, deltaY ):dims = np.shape( im )#获取图像维数lens = len( dims )   #lens值为2则是灰度图像,为3则是彩色图像if lens == 2:#单通道图像imRT = SingleChannelRatTrans( im, theta, deltaX, deltaY )if lens == 3:#三通道图像imr = im[ :, :, 0 ]img = im[ :, :, 1 ]imb = im[ :, :, 2 ]imrRT = SingleChannelRatTrans( imr, theta, deltaX, deltaY )imgRT = SingleChannelRatTrans( img, theta, deltaX, deltaY )imbRT = SingleChannelRatTrans( imb, theta, deltaX, deltaY )imRT = np.stack( ( imrRT, imgRT, imbRT ), 2  )return imRT

3.完整的图像旋转平移变换代码

#图像旋转+平移变换
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import math
#单通道图像的旋转+平移变换
#im为单通道图像像素矩阵
#theta为旋转角度(单位是:度°)
#deltaX和deltaY是沿着两个坐标轴方向的平移量
#返回旋转+平移变换结果图像
def SingleChannelRatTrans( im, theta, deltaX, deltaY ):[m, n] = np.shape( im )halfM = np.int( np.floor(m / 2) )halfN = np.int( np.floor(n / 2) )imRT = np.zeros( [ m, n] )angle = theta / 180 * 3.1415926for i in range(m):for j in range(n):ii = i - halfMjj = j - halfNi1 = round( ii * math.cos( angle ) + jj * math.sin( angle ) + halfM + deltaX )j1 = round( -ii * math.sin( angle ) + jj * math.cos( angle ) + halfN + deltaY )if i1 >= 0 and  i1 < m and j1 >= 0 and j1 < n:imRT[i][j] = im[i1][j1]imRT = imRT.clip( 0, 255 )#限制灰度值在0~255之间imRT = np.rint(imRT).astype('uint8')#设置像素的数据类型 return imRT
#图像旋转+平移变换,可以是灰度图像,也可以是彩色图像
#画布大小为原图像的大小,因此旋转平移之后会有部分像素看不到
def ImageRatationTranslation( im, theta, deltaX, deltaY ):dims = np.shape( im )#获取图像维数lens = len( dims )   #lens值为2则是灰度图像,为3则是彩色图像if lens == 2:#单通道图像imRT = SingleChannelRatTrans( im, theta, deltaX, deltaY )if lens == 3:#三通道图像imr = im[ :, :, 0 ]img = im[ :, :, 1 ]imb = im[ :, :, 2 ]imrRT = SingleChannelRatTrans( imr, theta, deltaX, deltaY )imgRT = SingleChannelRatTrans( img, theta, deltaX, deltaY )imbRT = SingleChannelRatTrans( imb, theta, deltaX, deltaY )imRT = np.stack( ( imrRT, imgRT, imbRT ), 2  )return imRTdef main():im = np.array( Image.open('dog.jpg', 'r') )theta = -45imRT = ImageRatationTranslation( im, theta, 10, -80 )plt.figure()plt.imshow( im, cmap = 'gray' )plt.axis( 'off' )plt.figure()plt.imshow( imRT, cmap = 'gray' )plt.axis( 'off' )if __name__ == '__main__':main()

运行结果:

四、Python内嵌的命令rotate

语法规则:
output = im.rotate( angle, method )
其中im是图像矩阵,可以是单通道,也可以是多通道
angle是旋转角度,单位是度°,正值为逆时针,负值为顺时针
method是插值方法,可以是nearest、bilinear等
output是旋转之后的图像。
例如:

im = Image.open('dog.jpg', 'r')
imRT = im.rotate( 45, Image.NEAREST )
plt.figure()
plt.imshow( imRT, cmap = 'gray' )
plt.axis( 'off' )

运行结果如下:

更多推荐

Python彩色图像旋转+平移变换数学原理及实现

本文发布于:2024-02-12 08:15:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1686913.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   彩色   原理   数学   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!