在Python中围绕指定原点旋转2D图像

在Python中围绕指定原点旋转2D图像

本文介绍了在Python中围绕指定原点旋转2D图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个512x512像素的2D图像,我想在某个原点(旋转中心)以一定角度旋转。这一次,我使用Scipy用旋转方法旋转图像。但是,由于旋转始终围绕图像中心进行,因此我受到了挫折。对于512x512像素,旋转中心应该在点(x,y)128,128附近。如何使用自定义旋转中心旋转图像,让我们说围绕(x,y)20,128?

I have a 2D image of 512x512 pixels that I would like to rotate with a certain angle at a certain origin (rotation center). All this time, I uses Scipy to rotate images with its rotate method. But, I got stumbled because the rotation always done around the center of the image. With 512x512 pixels the rotation center should be around point (x,y) 128,128. How can I rotate the image with a custom rotation center, let's say around (x,y) 20,128?

推荐答案

如果OpenCV不是一个选项,你可以使用NumPy围绕一个所谓的枢轴点进行图像旋转(以nip 的方式导入numpy和sciPy(来自scipy import ndimage 的),方法如下:

If OpenCV is not an option, you can do image rotation around a so called pivot point with NumPy (import numpy as np) and SciPy (from scipy import ndimage) the following way:


  1. 填充图像 img ,使得轴心点位于图像中心,图像尺寸加倍:

  1. Pad the image img such that the pivot point is in the image center and the image size is doubled:

padX = [img.shape[1] - pivot[0], pivot[0]]
padY = [img.shape[0] - pivot[1], pivot[1]]
imgP = np.pad(img, [padY, padX], 'constant')

(图像形状按行列顺序, pivot 在XY或列行中您可能希望以不同的方式定义它。)

(While the image shape is in row-column order, pivot is in X-Y or column-row order here. You might want to define it differently.)

围绕中心旋转图像(此处旋转角度为45度):

Rotate the image around its center (here the rotation angle is 45 degrees):

imgR = ndimage.rotate(imgP, 45, reshape=False)

请注意,我们不允许重塑图像,因为我们将自己裁剪图像。

Note that we disallow reshaping the image, since we'll crop the image ourselves.

裁剪图像,使枢轴点位于其原始位置。因此,我们只需从第1步反转填充:

Crop the image such that the pivot point is at its original position. Therefore, we simply reverse the padding from step 1:

imgC = imgR[padY[0] : -padY[1], padX[0] : -padX[1]]


您可以在下图中看到不同的步骤(原始图像,填充,旋转,裁剪; 45度左右(100,300))。

You can see the different steps in the following plot (original image, padded, rotated, cropped; 45 degrees around (100, 300)).

将其包装在一个方便的函数中:

Wrapping it up in a handy function yields:

def rotateImage(img, angle, pivot):
    padX = [img.shape[1] - pivot[0], pivot[0]]
    padY = [img.shape[0] - pivot[1], pivot[1]]
    imgP = np.pad(img, [padY, padX], 'constant')
    imgR = ndimage.rotate(imgP, angle, reshape=False)
    return imgR[padY[0] : -padY[1], padX[0] : -padX[1]]






更新

对于彩色图像,您必须避免在填充时添加更多通道(零填充)在第三维):

For colored images you'd have to avoid adding more channels while padding (zero padding in 3rd dimension):

imgP = np.pad(img, [padY, padX, [0, 0]], 'constant')

别忘了使用 0 用于之前和之后填充。否则你得到 ValueError

Don't forget to use a 0 for both "before" and "after" padding. Otherwise you get a ValueError.

这篇关于在Python中围绕指定原点旋转2D图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 07:48