Talking about performance to complement Ying Xiong summary and based on arrays as Numpy.array
whose datatype is either int
or float
, OpenCV is much faster:
import numpy as np
import cv2
from timeit import Timer
from scipy.ndimage import zoom
def speedtest(cmd, N):
timer = Timer(cmd, globals=globals())
times = np.array(timer.repeat(repeat=N, number=1))
print(f'Over {N} attempts, execution took:\n'
f'{1e3 * times.min():.2f}ms at min\n'
f'{1e3 * times.max():.2f}ms at max\n'
f'{1e3 * times.mean():.2f}ms on average')
# My image is 2000x2000, let's try to resize it to 300x300
image_int = np.array(image, dtype= 'uint8')
image_float = np.array(image, dtype= 'float')
N_attempts = 100 # We run the speed test 100 times
speedtest("zoom(image_int, 300/2000)", N_attempts)
# Over 100 attempts, execution took:
# 120.84ms at min
# 135.09ms at max
# 124.50ms on average
speedtest("zoom(image_float, 300/2000)", N_attempts)
# Over 100 attempts, execution took
# 165.34ms at min
# 180.82ms at max
# 169.77ms on average
speedtest("cv2.resize(image_int, (300, 300))", N_attempts)
# Over 100 attempts, execution took
# 0.11ms at min
# 0.26ms at max
# 0.13ms on average
speedtest("cv2.resize(image_float, (300, 300))", N_attempts)
# Over 100 attempts, execution took
# 0.56ms at min
# 0.86ms at max
# 0.62ms on average