Find the maximum x, y value from a series of images

I have a stack of raster images (between 2000-4000) that I am doing a projection of maximum z-projection intensity. Therefore, from the stack I need to get a 2d array of maximum values ​​for each x, y position.

I developed a simple script that breaks files into pieces and uses multiprocessing.pool to calculate the maximum array for this cartridge. Then these arrays are compared to find the maximum for the stack.

It works, but it is slow. My system monitor shows that my processors are unlikely to work.

Can someone give me some pointers on how I can speed things up a bit?

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")
+4
source share
1

Edit:

- , . , ( ), . ( , 3 ). ,

x = np.maximum(x, y)

,

x[y > x] = y[y > x]
#or
ind = y > x
x[ind] = y[ind]

. - :

import numpy as np
from multiprocessing import Pool

def process(chunk):
    max_ = np.zeros((4000, 4000))
    for im in chunk:
        im_array = np.array(Image.open(im))
        max_ = np.maximum(max_, im_array)
    return max_

if __name__ == "__main__":
    p = Pool(8)

    chunksize = 500 #4000/8 = 500, might have less overhead
    chunks = [files[i:i+chunksize]
              for i in range(0, len(files), chunksize)]

    # this returns an array of (len(files)/chunksize, 4000, 4000)
    max_arrays = np.array(p.map(process, chunks))
    maxi = np.amax(max_array, axis=0) #finds maximum along first axis
    img = Image.fromarray(maxi) #should be of shape (4000, 4000)

, , , , . .


? ? , - :

maxi = np.zeros(image_shape) # something like (1024, 1024)

for im in files:
    im_array = np.array(Image.open(im))
    inds = im_array > maxi # find where image intensity > max intensity
    maxi[inds] = im_array[inds] # update the maximum value at each pixel 

max_im = Image.fromarray(maxi)
max_im.save("max_intensity.tif")

maxi (x, y). . , for, 3, .

+4

Source: https://habr.com/ru/post/1530295/


All Articles