python:skimage.transform.AffineTransform旋转中心

2024-03-29 09:43:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在python中使用skimage来旋转图像,在opencv中,it seems I can do

cv.GetRotationMatrix2D(center, angle, scale, mapMatrix) 

其中center是源图像中旋转的中心。

在skliage中,相应的转换似乎是skimage.transform.AffineTransform

skimage.transform.AffineTransform(matrix=None, scale=None, rotation=None, shear=None, translation=None)

但我不能确定旋转的中心。。。。是否有定义旋转中心的方法(或者可能有另一种撇渣方法?)

我查了网页和手册,到目前为止什么也没有发现。。。


Tags: 方法图像nonetransformit中心doopencv
2条回答

目前可以通过组合以下转换来实现这一点:

  1. 移动图像使中心围绕原点
  2. 旋转N度
  3. 将图像移回

然而,一个单一的参数将使这更容易!请您在GitHub上提交一个问题,以便我们实现它,好吗?

同时,密码:

from skimage import data
from skimage import transform
import numpy as np
import matplotlib.pyplot as plt

image = data.chelsea()

shift_y, shift_x = np.array(image.shape[:2]) / 2.
tf_rotate = transform.SimilarityTransform(rotation=np.deg2rad(30))
tf_shift = transform.SimilarityTransform(translation=[-shift_x, -shift_y])
tf_shift_inv = transform.SimilarityTransform(translation=[shift_x, shift_y])

image_rotated = transform.warp(image, (tf_shift + (tf_rotate + tf_shift_inv)).inverse)

plt.imshow(image_rotated)
plt.show()

Stefan答案中的图像中心似乎不正确,下面是他的代码的正确版本

from skimage import transform
import numpy as np
import matplotlib.pyplot as plt

image = np.zeros([21, 21])
image[10,:] = 1
image[10,10] = 5
image[7, 10] = 1

shift_y, shift_x = (np.array(image.shape)-1) / 2.
tf_rotate = transform.SimilarityTransform(rotation=np.deg2rad(60))
tf_shift = transform.SimilarityTransform(translation=[-shift_x, -shift_y])
tf_shift_inv = transform.SimilarityTransform(translation=[shift_x, shift_y])
image_rotated = transform.warp(image, (tf_shift + (tf_rotate + tf_shift_inv)).inverse, order = 3)


plt.subplot(121)
plt.imshow(image)
plt.subplot(122)
plt.imshow(image_rotated)
plt.show()

print "original image maximum at: ", np.unravel_index(np.argmax(image), image.shape)
print "rotated image maximum at : ", np.unravel_index(np.argmax(image_rotated), image_rotated.shape)

原始图像最大值为:(10,10) 旋转图像的最大值为:(10,10)

相关问题 更多 >