import time
import cv2
from skimage import transform
import numpy as np
from PIL import Image
from scipy import misc
import tensorflow as tf
# 测试开始
path = 'imgs/yahui_1.jpg'
frame = cv2.imread(path)
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# 第一种:opencv
start_time = time.time()
pic = cv2.resize(rgb_frame, (400, 300), interpolation=cv2.INTER_LINEAR)
print('使用opencv耗时:', time.time() - start_time)
# 第二种: scipy.misc
start_time = time.time()
pic = misc.imresize(rgb_frame, (400, 300), interp='bilinear')
print('scipy.misc', time.time() - start_time)
# 第三种:skimage.transform
start_time = time.time()
pic = transform.resize(rgb_frame, (400, 300))
print('skimage.transform', time.time() - start_time)
# 第四种: tensorflow
tf_img_in = tf.placeholder(dtype=tf.float32, shape=(None, None, 3))
tf_img_op1 = tf.image.resize_images(rgb_frame, [400, 300], method=tf.image.ResizeMethod.BILINEAR)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
start_time = time.time()
img_op1 = sess.run(tf_img_op1, feed_dict={tf_img_in: rgb_frame})
print('TensorFlow 缩放用时: ', time.time() - start_time)
img_op1[img_op1 < 0] = 0
img_op1[img_op1 > 1] = 1
img_op1 = np.asarray(img_op1 * 255, np.uint8)
# 第五种 pil
start_time = time.time()
img = Image.open(path)
img.resize((400, 300))
print('PIL Image', time.time() - start_time)
# 第六种:imutils
start_time = time.time()
pic = imutils.resize(frame,height=400, width=300)
print('使用imutil耗时: ', time.time() - start_time)
时间对比:
使用opencv耗时: 0.00099945068359375
scipy.misc 0.003000497817993164
skimage.transform 0.03397846221923828
TensorFlow 缩放用时: 0.049959659576416016
PIL Image 0.01897120475769043
使用imutil耗时: 0.0019981861114501953
用过对比可以看出 opencv的效率最高