Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it safe to multiprocess pystackreg? #12

Closed
loomcode opened this issue Feb 24, 2021 · 2 comments
Closed

Is it safe to multiprocess pystackreg? #12

loomcode opened this issue Feb 24, 2021 · 2 comments

Comments

@loomcode
Copy link

loomcode commented Feb 24, 2021

I'm trying to multiprocess image pairs to speed up registration:

def regWorker(ref, cur):
    sr = StackReg(StackReg.TRANSLATION)
    ref_img = cv2.imread(ref, -1).astype(np.float64)
    cur_img = cv2.imread(cur, -1).astype(np.float64)
    registered = sr.register_transform(ref_img, cur_img).astype(np.uint16)
    return cur, registered

def register_tseries(img_paths, n_proc):
    l = list(img_paths.values())
    inp = [(l[i], l[i+1]) for i in range(len(l)-1)]
    p = Pool(processes=n_proc)
    res = p.starmap(regWorker, inp)
    for r in res:
        cv2.imwrite(r[0], r[1])

The output from the code doesn't quite match that from the following sequential call and I'm wondering why?

def register_tseries(img_paths, n_proc):
    l = list(img_paths.values())
    inp = [(l[i], l[i + 1]) for i in range(len(l) - 1)]
    sr = StackReg(StackReg.TRANSLATION)
    for i in inp:
        ref_img = cv2.imread(i[0], -1).astype(np.float64)
        cur_img = cv2.imread(i[1], -1).astype(np.float64)
        registered = sr.register_transform(ref_img, cur_img).astype(np.uint16)
        cv2.imwrite(i[1], registered)

Thanks!

@glichtner
Copy link
Owner

Hi,

in the sequential version you're replacing the second image ("cur") by the registered one and you then read that registered one back in the next iteration (now as "ref"). In the parallel version, you are often reading the unregistered one as the reference as it wasn't registered previously. Additionally you might be reading corrupt files if you happen to read a file in one process that is concurrently written in another process.

To parallelize you need to first determine the transformation matrix for each pair of images (you can do that in parallel), then sequentially multiply each transformation matrix with the previous one, then you can again in parallel apply the transformation matrices to each image. This won't work with the bilinear transformation.

Hope that helps.

@loomcode
Copy link
Author

loomcode commented Feb 24, 2021

That makes a lot of sense. Thanks for the tip on calculating the transformations in parallel. I'll give that shot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants