欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python实现图像绕中心旋转

程序员文章站 2022-07-14 23:25:19
...

公式摘自《数字图像处理及Matlab实现》杨杰版
设初始图像上的点为A0(x0,y0),旋转β角后的点为A(x,y),为方便表示采用极坐标,初始角度为α,如下图:
Python实现图像绕中心旋转
旋转前A0用极坐标表示为:
Python实现图像绕中心旋转
旋转后A坐标为:
Python实现图像绕中心旋转
进行化简得:
Python实现图像绕中心旋转
现在我们知道了旋转后的点与初始点和旋转角度的关系,接下来按下列步骤来旋转图片:

  1. 对坐标进行平移转换
  2. 确定旋转后的图幅大小
  3. 对目标区域的点(x,y)进行反向旋转,求出目标点在原图上的位置(x0,y0),之后对进行过插值处理的原图灰度值赋值给目标点(x,y)

坐标平移转换

因为要绕中心点旋转,所以要将坐标进行平移。
Python实现图像绕中心旋转
图像大小为n*m,中心点坐标为center=(n/2,m/2),所以新的计算公式为
x = (x0 -center[0] ) cosβ - (y0 - center[1]) sinβ + center[0];
y = (x0 -center[0] ) sinβ + (y0 - center[1]) cosβ + center[1];

确定旋转后的图幅大小

如果是被裁剪过的效果,那么目标边界大小就为原始图像大小。
Python实现图像绕中心旋转
如果是未被裁剪过的效果,则要对边界进行重新计算,红色外圈为旋转后图像的边界。
Python实现图像绕中心旋转
用原始图像的四个顶点,在旋转后的坐标最大值减最小值即可得出,代码如下,nn和mm为旋转后的边界

# 确定旋转后图像的边界
xx = []
yy = []
for x0, y0 in ((0, 0), (n, 0), (n, m), (0, m)):
    x = (x0 - center[0]) * math.cos(b) + (y0 - center[1]) * math.sin(b)
    y = -1*(x0 - center[0]) * math.sin(b) + (y0 - center[1]) * math.cos(b)
    xx.append(x)
    yy.append(y)
nn = int(math.ceil(max(xx)) - math.floor(min(xx)))
nm = int(math.ceil(max(yy)) - math.floor(min(yy)))

反向旋转+插值处理

如果直接由原图来计算出目标图像,则会因为坐标点在旋转过程中出现小数,使有些点的没有被映射到,产生的图片有很多“黑点”。
所以我这里采用反向旋转,计算出目标点在原图上的位置,此时原图坐标也会有小数情况,所以对其进行了双线性内插值
插值算法参考了这篇文章
图像几何变换(缩放、旋转)中的常用的插值算法


整体代码如下:

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import pylab
from skimage import data,color,transform
import math

def imrotate(image, b, crop):
    b = -math.radians(b % 360)  # 将角度化为弧度
    n = np.size(image, 0)
    m = np.size(image, 1)
    center = (n/2.0, m/2.0)
    img = np.zeros((n, m))

    # 不裁剪
    if not crop:
        # 计算图幅
        xx = []
        yy = []
        for x0, y0 in ((0, 0), (n, 0), (n, m), (0, m)):
            x = (x0 - center[0]) * math.cos(b) + (y0 - center[1]) * math.sin(b)
            y = -1*(x0 - center[0]) * math.sin(b) + (y0 - center[1]) * math.cos(b)
            xx.append(x)
            yy.append(y)
        nn = int(math.ceil(max(xx)) - math.floor(min(xx)))
        nm = int(math.ceil(max(yy)) - math.floor(min(yy)))
        img = np.zeros((nn, nm))
        center = (nn/2, nm/2)

    # 裁剪
    if crop:
        nn = n
        nm = m

    def inmap(x, y):
       return True if x >= 0 and x < n and y >= 0 and y < m else False


    # 反向推
    for x in range(nn):
        for y in range(nm):
            x0 = (x-center[0])*math.cos(-b)+(y-center[1])*math.sin(-b)+center[0]
            y0 = -1*(x-center[0])*math.sin(-b)+(y-center[1])*math.cos(-b)+center[1]
            # 将坐标对齐
            x0 = x0-(nn-n)/2
            y0 = y0-(nm-m)/2
            # 双线性内插值
            i = int(x0)
            j = int(y0)
            u = x0 - i
            v = y0 - j
            if inmap(i, j):
                f1 = (1-u)*(1-v)*image[i, j]
                img[x, y] += f1
                if inmap(i, j+1):
                    f2 = (1-u)*v*image[i, j+1]
                    img[x, y] += f2
                if inmap(i+1, j):
                    f3 = u*(1-v)*image[i+1, j]
                    img[x, y] += f3
                if inmap(i+1, j+1):
                    f4 = u*v*image[i+1, j+1]
                    img[x, y] += f4

    plt.imshow(Image.fromarray(img), 'gray')
    pylab.show()

def main():
    #img = Image.open("week3//pout.tif")
    img = data.camera()
    img = np.array(img)
    b = 60
    imrotate(img, b, crop=False)
    imrotate(img, b, crop=True)

if __name__ == '__main__':
    main()

最后运行效果:
Python实现图像绕中心旋转