我需要一种多重处理方法来预处理图像,然后再将其输入神经网络。我发现这个页面叫做令人尴尬的并行 for 循环 https://pythonhosted.org/joblib/parallel.html其中对数组/列表中的元素并行运行数学任务。我想知道这是否可以扩展到图像(毕竟图像只不过是数组,大的 3D 数组!)
我决定执行添加加权 https://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html#addweighted从 OpenCV 到图像集合的操作。使用此操作,您可以对两个图像应用不同的权重并将它们相加。它用于尽可能混合图像see here https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_core/py_image_arithmetics/py_image_arithmetics.html#image-blending
我执行了这个功能with and withoutjoblib 在我的桌面上查找一组图像并比较它们的性能。最后我提到了图像的数量以及所使用的这些图像的集体大小。
Code:
import os
import time
#--- Importing the required library ---
from joblib import delayed
#--- Choosing all available image formats of images from my desktop ---
path = r'C:\Users\Jackson\Desktop'
img_formats = ['.png', '.jpg', '.jpeg']
#--- Defining the addWeighted function from OpenCV ---
def weight(im):
addweighted = cv2.addWeighted(im, 0.7, cv2.GaussianBlur(im, (15, 15), 0), 0.3, 0)
return addweighted
#--- Using joblib library-----
start_time = time.time()
new_dir = os.path.join(path, 'add_Weighted_4_joblib')
if not os.path.exists(new_dir):
os.makedirs(new_dir)
def joblib_loop():
for f in os.listdir(path):
if any(c in f for c in img_formats):
img = cv2.imread(os.path.join(path, f))
r = delayed(weight)(img)
cv2.imwrite(os.path.join(new_dir, f + '_add_weighted_.jpg'), r)
elapsed_time = time.time() - start_time
print('Using Joblib : ', elapsed_time)
#--- Without joblib ---
start_time = time.time()
#--- Check whether directory exists if not make one
new_dir = os.path.join(path, 'add_Weighted_4')
if not os.path.exists(new_dir):
os.makedirs(new_dir)
for f in os.listdir(path):
if any(c in f for c in img_formats):
img = cv2.imread(os.path.join(path, f))
r = weight(img)
cv2.imwrite(os.path.join(new_dir, f + '_add_weighted_.jpg'), r)
elapsed_time = time.time() - start_time
print('Without Joblib : ', elapsed_time)
这是我得到的结果:
('Using Joblib : ', 0.09400010108947754)
('Without Joblib : ', 15.386000156402588)
正如你所看到的使用joblib
疯狂地加快操作速度!
现在让我向您展示我的桌面上有多少张图像以及它们的总大小是多少:
overall_size = 0
count = 0
#for f in os.listdir(path):
for f in os.listdir(path):
if any(c in f for c in img_formats):
img = cv2.imread(os.path.join(path, f))
overall_size+= img.size
count+= 1
print('Collective Size of all {} images in the predefined path is {} MB'.format(count, overall_size/10**6))
结果:
Collective size of all 14 images in the predefined path is 58 MB