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

一、引言

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

二、数学原理及公式

学过线性代数的童鞋都知道如下的矩阵表示旋转矩阵:
在这里插入图片描述
如果再加上平移,则可以用齐次坐标的表示形式:
Python彩色图像旋转+平移变换数学原理及实现_第1张图片
假设旋转之前的坐标为(x,y),绕坐标原点旋转之后的坐标为(x*, y*),则旋转+平移变换公式的矩阵形式为:
Python彩色图像旋转+平移变换数学原理及实现_第2张图片
对于图像旋转平移变换而言,其实就是像素的坐标旋转,像素跟着坐标走而已。

三、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.1415926
    for i in range(m):
        for j in range(n):
            ii = i - halfM
            jj = j - halfN
            i1 = 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.1415926
    for i in range(m):
        for j in range(n):
            ii = i - halfM
            jj = j - halfN
            i1 = 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 imRT
        
def main():
    im = np.array( Image.open('dog.jpg', 'r') )
    theta = -45
    imRT = 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彩色图像旋转+平移变换数学原理及实现_第3张图片
Python彩色图像旋转+平移变换数学原理及实现_第4张图片

四、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彩色图像旋转+平移变换数学原理及实现_第5张图片

你可能感兴趣的:(图像处理,Python,python,图像处理)