加快简单的图像预处理功能

时间:2020-02-16 14:07:03

标签: python performance image-processing optimization keras

我基于scikit图像function创建了这个简单的函数:

from skimage import exposure
import numpy as np

def hist_contr_func(image):
    '''Function from skimage that corrects the histogram to enhance contrast - "Adaptive equalization" '''
    # Line below is needed, because equalize_adapthist works in pixel value range (0,1)
    image = image / 255.

    h, w = image.shape[:2]
    # Line below is required, because equalize_adapthist works on arrays with shape (h,w)
    image = np.resize(image, (h,w))
    img_adapteq = exposure.equalize_adapthist(image, clip_limit=0.03)

    # I back-resize, because keras works with arrays with shape (h,w,channels)
    img_adapteq = np.resize(img_adapteq, (h,w,1))

    return img_adapteq

如何加快该功能?我认为瓶颈在exposure.equalize_adapthist中,但对调整大小计算的改进也表示赞赏

更多,但不是必不可少的信息:我正在使用该函数在keras中预处理图像:

from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(
    rotation_range=30,
    width_shift_range=0.15,
    preprocessing_function=hist_contr_func)

在没有preprocessing_function=hist_contr_func的情况下,我的keras脚本运行速度提高了5倍。我还要感谢其他对比增强的直方图校正程序包的建议,这些程序包的工作速度更快。谢谢!

0 个答案:

没有答案