1
\$\begingroup\$

I have an image stack. I am trying to use multiprocessing to do some process on each image in stack to get new image, and then bitwise_or with the old image.

Since multiprocessing won't keep the original order, I pass image index as args.

Later I bitwise_or the new image and old image by a for loop.

Is there a way to avoid for-loop for bitwise_or to have a better performance? Thanks

import cv2 as cv
import numpy as np
from multiprocessing import Pool

def process_img(idx, img):
    new_img = do_something(img)
    return idx, new_img

def main()
    # ...

    output_stack = np.copy(img_stack)

    with Pool() as pool:
        args = zip(
            range(len(img_stack)),
            img_stack)

        new_imgs_with_idx = pool.starmap(process_img, args)

        for i, new_img in new_imgs_with_idx:  # Any better ways?
            output_stack[i] = cv.bitwise_or(output_stack[i], new_img)

    # ...

    return output_stack
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.