我如何加快与Python中的NumPy数组的操作2.7

问题描述:

我尝试处理许多图像表示为NumPy数组,但它需要很长时间。这就是我尝试做我如何加快与Python中的NumPy数组的操作2.7

# image is a list with images 
max = np.amax(image[k])# k is current image index in loop 
# here i try to normalize SHORT color to BYTE color and make it fill all range from 0 to 255 
# in images max color value is like 30000 min is usually 0 
i = 0 
while i < len(image[k]): 
    j = 0 
    while j < len(image[k][i]): 
     image[k][i][j] = float(image[k][i][j])/(max) * 255 
     j += 1 
    i += 1 

如果我只看过图片(共170(图片512×512是)),而它需要大约7秒,如果我这样做正常化需要20分钟。这一切都在代码中。在这里,我试图让我的图像着色

maskLoot1=np.zeros([len(mask1), 3*len(mask1[0])]) 
for i in range(len(mask1)): 
    for j in range(len(mask1[0])): 
     maskLoot1[i][j*3]=mask1[i][j] 
     maskLoot1[i][j*3+1]=mask1[i][j] 
     maskLoot1[i][j*3+2]=mask1[i][j] 

接着我尝试用彩色那些来替换选定的区域中的像素,例如120(灰色) - >(255 40 0)在RGB模型。

  for i in range(len(mask1)): 
       for j in range(len(mask1[0])): 
       #mask is NumPy array with selected pixel painted in white (255) 
        if (mask[i][j] > 250): 
         maskLoot1[i][j * 3] = lootScheme[mask1[i][j]][1] #red chanel 
         maskLoot1[i][j * 3+1] = lootScheme[mask1[i][j]][2] #green chanel 
         maskLoot1[i][j * 3+2] = lootScheme[mask1[i][j]][3] #bluechanel 

而且它也需要很多时间,而不是20分钟,但长期以来让我的脚本滞后。认为这仅仅是我在数组上的很多操作中的2个,而如果是第二种情况,我们可以为别人使用一些bultin函数是不太可能的。那么有没有办法加快我的节奏?

+3

'image [k] = image [k]/max * 255'? – furas

+0

@furas对于这种情况是的,但我也在试图做下一步:我有一个坐标列表(x,y)代表所选区域,我尝试用有色区域代替它们,例如120(灰色) - >(255 40 0)in rgb model。我编辑了一个问题。 – tttaaabbb

+2

@tttaaabbb将您的问题逐个填写,不要将这3-4个问题合并为一个问答。我没有看到这些问题之间的关系,以证明他们被放在一个地方。 – Divakar

一般来说,for循环比while循环快得多。同样使用函数

maskLoot1[i][j*3]=mask1[i][j] 
maskLoot1[i][j*3+1]=mask1[i][j] 
maskLoot1[i][j*3+2]=mask1[i][j] 

并且在循环中调用函数应该会显着加快进程。

为了您的掩模制造代码试试这个更换,以循环:

maskLoot1 = np.dstack(3*[mask1]).reshape((mask1.shape[0],3*mask1.shape[1])) 

还有许多其他的方法/实现上述的变化,例如,

maskLoot1 = np.tile(mask1[:,:,None], 3).reshape((mask1.shape[0],3*mask1.shape[1])) 

至于第一部分你的问题最好的答案是在你的问题的第一评论@furas

首先,考虑移动到Python 3. *。 Numpy正在放弃对Python Numpy is dropping support for Python 2.7 from 2020的支持。

对于您的代码问题。您错过了下面使用Numpy的要点。 Numpy是从较低级别的库编译而来的,运行速度非常快,您不应该在Python中循环索引,而应该将矩阵传递给Numpy。

问题1 正常化非常快,使用listcomp和np.array

import numpy as np 
import time 

# create dummy image structure (k, i, j, c) or (k, i, j) 
# k is image index, i is row, j is columns, c is channel RGB 
images = np.random.uniform(0, 30000, size=(170, 512, 512)) 

t_start = time.time() 
norm_images = np.array([(255*images[k, :, :]/images[k, :, :].max()).astype(int) for k in range(170)]) 
t_end = time.time() 

print("Processing time = {} seconds".format(t_end-t_start)) 
print("Input shape = {}".format(images.shape)) 
print("Output shape = {}".format(norm_images.shape)) 
print("Maximum input value = {}".format(images.max())) 
print("Maximum output value = {}".format(norm_images.max())) 

这就产生了如下的输出

Processing time = 0.2568979263305664 seconds 
Input shape = (170, 512, 512) 
Output shape = (170, 512, 512) 
Maximum input value = 29999.999956185838 
Maximum output value = 255 

它需要0.25秒!

问题2 不知道你在这里的意思是什么,但如果你想克隆一个单色图像的值,RGB值,你可以做这样的

# coloring (by copying value and keeping your structure) 
color_img = np.array([np.tile(images[k], 3) for k in range(170)]) 
print("Output shape = {}".format(color_img.shape)) 

将会产生

Output shape = (170, 512, 1536) 

如果您想保留一个(c,i,j,k)结构

color_img = np.array([[images[k]]*3 for k in range(170)]) # that creates (170, 3, 512, 512) 
color_img = np.swapaxes(np.swapaxes(color_img, 1,2), 2, 3) # that creates (170, 512, 512, 3) 

这一切都需要0.26秒!

问题3 着色某些区域,我会再次使用函数和listcomp。由于这是一个例子,我使用了默认的颜色(255,40,0),但您可以使用任何东西,包括LUT。

# create mask of zeros and ones 
mask = np.floor(np.random.uniform(0,256, size=(512,512))) 
default_scheme = (255, 40, 0) 

def substitute(cimg, mask, scheme): 
    ind = mask > 250 
    cimg[ind, :] = scheme 
    return cimg 

new_cimg = np.array([substitute(color_img[k], mask, default_scheme) for k in range(170)])