首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在python中旋转图像最快的方法是什么?

在python中旋转图像最快的方法是什么?
EN

Stack Overflow用户
提问于 2021-11-08 15:59:21
回答 1查看 71关注 0票数 1

我需要知道在Python中旋转图像的最快方法是什么。

据我所知,在Stack Overflow中没有提出或回答这样的问题,所以我决定进行一个实验,找出答案。这就是为什么我想和大家分享结果的原因:

代码语言:javascript
复制
#we will compare which method is faster for rotating a 2D image 
#there are four major libraries for image handling and manipulation 
#PIL 
#OpenCV
#SciPy
#skimage 

import numpy as np
import PIL
import cv2
import matplotlib.pylab as plt
from PIL import Image
from scipy.ndimage import rotate
from scipy.ndimage import interpolation
import scipy
from skimage.transform import rotate




#get data :
PIL_image = Image.open('cat.jpg')
#image = cv2.imread('bambi.jpg') #read gray scale
array_image = np.array(PIL_image)

为每个模块定义一个函数:

代码语言:javascript
复制
def rotate_PIL (image, angel, interpolation):
    #PIL.Image.NEAREST (use nearest neighbour), PIL.Image.BILINEAR (linear interpolation in a 2×2 environment), or PIL.Image.BICUBIC 


    return image.rotate(angel,interpolation)
    
    
def rotate_CV(image, angel , interpolation):
    #in OpenCV we need to form the tranformation matrix and apply affine calculations
    #interpolation cv2.INTER_CUBIC (slow) & cv2.INTER_LINEAR
    h,w = image.shape[:2]
    cX,cY = (w//2,h//2)
    M = cv2.getRotationMatrix2D((cX,cY),angel,1)
    rotated = cv2.warpAffine(image,M , (w,h),flags=interpolation)
    return rotated

    

def rotate_scipy(image, angel , interpolation):
    return  scipy.ndimage.interpolation.rotate(image,angel,reshape=False,order=interpolation)

使用timeit找出哪个最快: PIL、Open CV或Scipy:

代码语言:javascript
复制
%timeit rotate_PIL(PIL_image,20,PIL.Image.NEAREST)
%timeit rotate_CV(array_image,20,cv2.INTER_LINEAR)
%timeit rotate_scipy(array_image,20,0)

结果是:

代码语言:javascript
复制
975 µs ± 203 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
3.5 ms ± 634 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
36.4 ms ± 11.6 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

不过,还有一个问题--这是针对一个jpeg文件的,而PIL刚刚压垮了另外两个库。但是,我主要处理TIFF格式的大型原始图像,因此我还决定测试TIFF文件:

代码语言:javascript
复制
#test for TIFF image 
#get data :
PIL_image = Image.open('TiffImage.tif')
#image = cv2.imread('bambi.jpg') #read gray scale
array_image = np.array(PIL_image)

%timeit rotate_PIL(PIL_image,20,PIL.Image.NEAREST)
%timeit rotate_CV(array_image,20,cv2.INTER_LINEAR)
%timeit rotate_scipy(array_image,20,0)

65.1 ms ± 9.35 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
38.6 ms ± 6.91 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
344 ms ± 43.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

这意味着OpenCV在TIFF文件格式方面比我们在JPEG文件方面的明显赢家做得更好。

如果有人碰巧知道原因,或者如果有人有类似的实验,请在这里分享。我认为这将是一个很好的比较的公开文档。

另外,如果你认为我的实验是无效的,或者因为任何原因可以改进,请告诉我。

EN

回答 1

Stack Overflow用户

发布于 2021-11-15 11:28:38

首先,感谢您的所有评论。它帮助我以更好的方式思考这个问题,并考虑到我在实验中所犯的错误。

我做了一个更好的可重复的实验,我使用不同大小的numpy数组生成图像,并使用Pillow、OpenCV和Scipy的三个库使用相同的插值函数旋转它们。答案可以在下面的图表中总结。要重复这个实验或看看我是如何得出这个结论的,请参考this link,如果您认为它可以改进,请发表评论。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69886416

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档