【问题标题】:Speed up Image Feature Extraction加快图像特征提取
【发布时间】:2017-11-16 04:09:28
【问题描述】:

它是导入多张图像并提取特征的程序。 问题是太慢了 我想是因为for循环太多了。

例如

for q in range(0, height-32 , 32):  
  for w in range(0 , width-32 ,32):
   for j in range(0,64,8):
       for n in range(0,64,8):

如何更改我的代码以加快速度?

import numpy as np
from scipy.fftpack import dct
from PIL import Image
import glob
import matplotlib.pyplot as plt


def image_open(path):
    image_list = []
    #for filename in glob.glob('path/*.jpg'): 
    for filename in glob.glob(path+'/*.jpg'):  
     im=Image.open(filename)
     image_list.append(im)

    return image_list

path = 'C:\\Users\\LG\\PycharmProjects\\photo'   

images = image_open(path)


for i in range(0, len(images)):     
 box3 = (0,0,256,256)
 a = images[i].crop(box3)

 (y,cb,cr) = a.split()       
 width , height = y.size  
 y.show()

 for q in range(0, height-32 , 32):  
  for w in range(0 , width-32 ,32):



     for j in range(0,64,8):
       for n in range(0,64,8):

     print(w/32)

【问题讨论】:

  • 你想对所有这些循环做什么?如果我们不知道您为什么要循环这么多次,那么我们就无法帮助您改进算法。
  • 题名真是个大话题:-)

标签: python performance loops for-loop


【解决方案1】:

不知道你为什么要使用这么多循环,所以我建议你先尽可能地改进它。

之后,查看线程。 Threading

如果消耗的图像数量较少,您可以在单独的线程上对每个图像运行该操作。

【讨论】:

    猜你喜欢
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多