我有一堆位图图像(大约在2000-4000张之间),我要对它们进行最大强度投影的z-projection。因此,我需要从这个堆栈中得到每个x,y位置的最大值的2d数组。
我设计了一个简单的脚本将文件分成多个块,并使用multiprocessing.pool来计算该块的最大数组。然后比较这些数组,以找到堆栈的最大值。
虽然它能工作,但速度很慢。我的系统监视器显示我的CPU几乎没有运行。
请问有人可以给我一些建议如何加快处理速度吗?
我设计了一个简单的脚本将文件分成多个块,并使用multiprocessing.pool来计算该块的最大数组。然后比较这些数组,以找到堆栈的最大值。
虽然它能工作,但速度很慢。我的系统监视器显示我的CPU几乎没有运行。
请问有人可以给我一些建议如何加快处理速度吗?
import Image
import os
import numpy as np
import multiprocessing
import sys
#Get the stack of images
files = []
for fn in os.listdir(sys.argv[1]):
if fn.endswith('.bmp'):
files.append(os.path.join(sys.argv[1], fn))
def processChunk(filelist):
first = True
max_ = None
for img in filelist:
im = Image.open(img)
array = np.array(im)
if first:
max_ = array
first = False
max_ = np.maximum(array, max_)
return max_
if __name__ == '__main__':
pool = multiprocessing.Pool(processes=8)
#Chop list into chunks
file_chunks = []
chunk_size = 100
ranges = range(0, len(files), chunk_size)
for chunk_idx in ranges:
file_chunks.append(files[chunk_idx:chunk_idx+chunk_size])
#find the maximum x,y vals in chunks of 100
first = True
maxi = None
max_arrays = pool.map(processChunk, file_chunks )
#Find the maximums from the maximums returned from each process
for array in max_arrays:
if first:
maxi = array
first = False
maxi = np.maximum(array, maxi)
img = Image.fromarray(maxi)
img.save("max_intensity.tif")