首页 > 解决方案 > Python 等价于 IDL median() 和 rot()

问题描述

我正在处理适合图像并尝试将 IDL 代码转换为 python,但我无法理解如何转换 IDLmedian()rot()函数。我有一个数组im_darksub(ny, nx, n_im)其中nx,ny是每个图像的形状,是图像n_im的数量(总共使用 12 个图像)。我试图翻译 IDL 代码im_median = median(im_darksub,dimension=1)im_medrot = rot(im_median,85,cubic=-0.5). 我以为我可以把它翻译成np.median()and spicy.misc.imrotate,但是当我使用时我不断收到错误imrotate

    Traceback (innermost last):
      File "<console>", line 1, in <module>
      File "/Users/courtneywatson1/Ureka/python/lib/python2.7/site-packages/scipy/misc/pilutil.py", line 345, in imrotate
        im = toimage(arr)
      File "/Users/courtneywatson1/Ureka/python/lib/python2.7/site-packages/scipy/misc/pilutil.py", line 234, in toimage
        raise ValueError("'arr' does not have a suitable array shape for "
    ValueError: 'arr' does not have a suitable array shape for any mode.

我猜这是因为我的图像阵列超过 2 或 3D?是否有 IDL 的 python 等效项median()rot()我使用它们的方式?

标签: pythonrotationmedianidl-programming-language

解决方案


这对我有用:

import numpy as np
x = np.arange(100)
x = np.reshape(x, (5, 5, 4))
im_median = np.median(x, axis=2)

import scipy.misc
im_rotate = scipy.misc.imrotate(y, 85, 'cubic')

我假设你看到它imrotate已被弃用:

/Users/mgalloy/anaconda3/bin/ipython:1: DeprecationWarning: `imrotate` is deprecated!
`imrotate` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``skimage.transform.rotate`` instead.

这似乎工作得很好:

import skimage.transform
im_rotate = skimage.transform.rotate(im_median, 85, order=3, preserve_range=True)

推荐阅读