Skip to content

Add flag to phase cross correlation to disambiguate periodic shifts - #6617

Merged
stefanv merged 12 commits into
scikit-image:mainfrom
jni:phase-xcorr-disambiguate
Jan 24, 2023
Merged

stefanv merged 12 commits into
scikit-image:mainfrom
jni:phase-xcorr-disambiguate

Conversation

@jni

@jni jni commented Nov 8, 2022

Copy link
Copy Markdown
Member

Closes #6604

This PR adds a disambiguate=False to phase_cross_correlation that uses real-space cross-correlation to disambiguate between the possible (periodic) shifts implied by the shift returned by phase_cross_correlation.

Perhaps we can default the flag to True, but it will come at a performance cost. With the False default, behaviour is unchanged for existing users.

@jni

jni commented Nov 8, 2022

Copy link
Copy Markdown
Member Author

Oh, another question, I didn't know whether stats.pearsonr was the best way to compute the cross-correlation between the images.

@jni

jni commented Dec 8, 2022

Copy link
Copy Markdown
Member Author

@stefanv @lagru would either of you care to have a look at this? 😊 🙏

Thinking more about the defaults: perhaps we can leave it as False for now and switch to True in skimage2?

@stefanv

stefanv commented Dec 8, 2022

Copy link
Copy Markdown
Member

I'm fine with setting the default to True. I do wonder if Pearson is the best way to go about it; no property of the FFT (already computed?) to exploit?

Review from a comfortable distance, so feel free to ignore ☺

@jni

jni commented Dec 8, 2022

Copy link
Copy Markdown
Member Author

No I don't think that the FFT values can help here — it needs to happen in real space. But there might be a faster function to compute cross-correlation in that space.

@jni

jni commented Dec 8, 2022

Copy link
Copy Markdown
Member Author

Indeed np.corrcoef is almost 2x faster than scipy.stats.pearsonr.

jni added 2 commits December 8, 2022 15:26
In [2]: x = np.random.random(int(1e6))

In [3]: y = np.random.random(int(1e6))

In [4]: %timeit np.corrcoef(x, y)
28.4 ms ± 1.86 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [5]: %timeit np.cov(x, y)
27.2 ms ± 1.75 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [6]: np.corrcoef(x, y)
Out[6]:
array([[ 1.        , -0.00121138],
       [-0.00121138,  1.        ]])

In [7]: from scipy import stats

In [8]: stats.pearsonr(x, y)
Out[8]: (-0.0012113788952588546, 0.22575063366199608)

In [9]: %timeit stats.pearsonr(x, y)
51.7 ms ± 3.3 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
@stefanv

stefanv commented Dec 8, 2022

Copy link
Copy Markdown
Member

This looks good to me, thanks Juan. The only remaining question is what the memory usage is doing all the warps/correlations simultaneously (as now) vs in the loop.

@jni

jni commented Dec 15, 2022

Copy link
Copy Markdown
Member Author

Sorry, can you elaborate on your question? Do you mean is it worth not doing ndi.shift to put the boxes in place to save that memory copy? It's 1x the image. To me it seems very much worthwhile as I think the slice logic is much simpler that way.

@stefanv

stefanv commented Dec 15, 2022

Copy link
Copy Markdown
Member

Ah, right, so you only make shifts for the few candidate positions, yes that should be fine then. 👍

@mkcor mkcor left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate this healthy addition, @jni! I also think we should default to disambiguate=False for now, for backwards compatibility.

one with the highest cross-correlation.

The strategy we use is to perform the shift on the moving image *using the
'grid-wrap' mode* in `scipy.ndimage`. The original image borders then

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, what is the original image? Can we stick with 'reference image' and 'moving image,' please?

disambiguate : bool
The shifts returned by this function are only accurate *modulo* the
image shape, due to the periodic nature of the Fourier transform. If
this is set to ``True``, the *real* space cross-correlation is

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this is set to ``True``, the *real* space cross-correlation is
this parameter is set to ``True``, the *real* space cross-correlation is

(Maybe we've lost track of what we're talking about because of the previous sentence--which is useful and helpful and welcome.)

The shifts returned by this function are only accurate *modulo* the
image shape, due to the periodic nature of the Fourier transform. If
this is set to ``True``, the *real* space cross-correlation is
computed for each possible shift, and the shift with the highest

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer 'shift' in the singular form, but so far you've used the plural, so we should be consistent one way or the other. I prefer 'shift' so that, in this context (in these docstrings), by 'shift' we mean the tuple, say, (-150, 200), and by 'shifts' we mean the set of:

(-150, 200)
(-150, -1800)
(1850, 200)
(1850, -1800)

Otherwise, 'shifts' mean either (-150, 200) or the set of all possible shifts.

used if any of ``reference_mask`` or ``moving_mask`` is not
None.
disambiguate : bool
The shifts returned by this function are only accurate *modulo* the

@mkcor mkcor Dec 16, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The shifts returned by this function are only accurate *modulo* the
The shift returned by this function is only accurate *modulo* the

moving_image : numpy array
The moving image: applying the shift to this image overlays it on the
reference image. Must be the same shape as the reference image.
shifts : tuple of float

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, I just deleted an in-line comment of mine here: It was my misunderstanding. Here, it's the plural indeed. Then, I suggest:

Suggested change
shifts : tuple of float
shifts : tuple of (tuple of) float

even if, in 1D, it's only a tuple of floats, but I'd like to distinguish it from real_shift (line 145 below).

Returns
-------
real_shift : tuple of float
The shifts disambiguated in real space.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The shifts disambiguated in real space.
The shift disambiguated in real space.

The shifts returned by this function are only accurate *modulo* the
image shape, due to the periodic nature of the Fourier transform. If
this is set to ``True``, the *real* space cross-correlation is
computed for each possible shift, and the shift with the highest

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
computed for each possible shift, and the shift with the highest
computed for each possible shift, and the shift with highest

image shape, due to the periodic nature of the Fourier transform. If
this is set to ``True``, the *real* space cross-correlation is
computed for each possible shift, and the shift with the highest
cross-correlation of the overlapping area is returned.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cross-correlation of the overlapping area is returned.
cross-correlation with the overlapping area is returned.

@stefanv stefanv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved pending @mkcor's suggestions.

@stefanv stefanv added this to the 0.20 milestone Jan 23, 2023
@stefanv

stefanv commented Jan 24, 2023

Copy link
Copy Markdown
Member

Given that @mkcor's suggestions have been addressed, I am going to take her review as an approval.

Thanks @jni!

@stefanv
stefanv merged commit 0ff35b2 into scikit-image:main Jan 24, 2023
@jni
jni deleted the phase-xcorr-disambiguate branch January 24, 2023 12:18
@imagesc-bot

Copy link
Copy Markdown

@m-albert

Copy link
Copy Markdown
Contributor

This is super useful 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: optionally resolve real-space shift from phase cross-correlation registration

6 participants