我正在将一些基于PIL的代码转换为NumPy,但我发现skimage.transform.rotate函数比PIL的Image.rotate慢得多。
作为一个粗略的比较,使用skimage's旋转在~1000x1000像素图像上大约需要2.2秒,而Image.rotate则需要大约0.1秒:
import time
from PIL import Image
import numpy as np
from skimage.transform import rotate
im = Image.open("some_big_image.png").convert("L")
print "Image size: %s" %(im.size, )
s = time.time()
im.rotate(10, Image.BICUBIC, expand=True)
print "Image.rotate: %0.04f" %(time.time() - s, )
ima = np.array(im) / 255.0
s = time.time()
rotate(ima, 10, order=3) # order=3 --> bi-cubic filtering
print "skimage.transform.rotate: %0.04f" %(time.time() - s, )以及产出:
$ py rotate.py
Image size: (1275, 1650)
Image.rotate: 0.1154
skimage.transform.rotate: 2.2310(在多次运行中,这些数字或多或少是一致的;我不认为这是没有运行足够的测试的产物)
所以!这是怎么回事?有没有办法加快skimage的rotate
版本信息:
同样值得注意的是:
BICUBIC筛选,则im.rotate操作仅需0.01秒,而将order=0设置为使用最近邻筛选时,skimage.rotate则需要0.6秒。发布于 2013-11-11 21:57:54
从https://github.com/scikit-image/scikit-image安装最新版本。就在几天前,我修复了一个与这个慢速相关的bug (参见https://github.com/scikit-image/scikit-image/commit/d5776656a8217e58cb28d5760439a54e96d15316)。
我的数字与当前的dev版本如下:
from PIL import Image
import numpy as np
from skimage.transform import rotate
a = np.zeros((1000, 1000), dtype=np.uint8)
im = Image.fromarray(a)
%timeit im.rotate(10, Image.BICUBIC, expand=True)
ima = a / 255.0
%timeit rotate(ima, 10, order=1)
%timeit rotate(ima, 10, order=3)
## -- Output --
10 loops, best of 3: 41.3 ms per loop
10 loops, best of 3: 43.6 ms per loop
10 loops, best of 3: 101 ms per loop发布于 2013-11-06 20:58:02
由于只读取了warp()的Python代码,而没有读取的Cython代码,因此猜测由于skimage使用的是通用的扭曲代码,它的代码路径比专门为平面内旋转而编写的代码路径效率更低。
https://stackoverflow.com/questions/19801978
复制相似问题