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

Default "phase" normalization of phase_cross_correlation gives invalid error estimate #6376

Open
mwshinn opened this issue May 7, 2022 · 4 comments
Labels
🐛 Bug 😴 Dormant No recent activity

Comments

@mwshinn
Copy link

mwshinn commented May 7, 2022

Description

The new "phase" normalization for the phase_cross_correlation always gives an error measurement of 1. This applies across a wide range of inputs/datatypes. This is a regression as of 0.19.2.

Looking at the code, this normalization appears to convert the "image_product" matrix into a matrix of ones, making the cross-correlation matrix a delta spike at the origin. The error is the maximum of this matrix, which is always 1. I have not been able to find in the documentation or comments any information on what this new normalization method is supposed to do, so I can't submit a PR (or even know if this is the intended behavior).

Way to reproduce

import skimage.registration
import numpy as np
img = skimage.data.camera().astype(float)

# UNEXPECTED BEHAVIOR: Correlating an image with itself gives large error
shift,error,_ = skimage.registration.phase_cross_correlation(img, img)
print(f"Error is {error}, should be zero")

# CORRECT BEHAVIOR: Correlating an image with white noise gives the same error
shift,error,_ = skimage.registration.phase_cross_correlation(img, np.random.randn(*img.shape))
print(f"Error is {error}, should be 1")

# CORRECT BEHAVIOR: Works as expected with the old normalization
shift,error,_ = skimage.registration.phase_cross_correlation(img, img, normalization=None)
print(f"Error is {error}, should be zero")

A workaround to make scripts compatible with 0.19.2 as well as previous versions is:

try:
    res = skimage.registration.phase_cross_correlation(img1, img2, normalization=None)
except TypeError: # Invalid argument normalization
    res = skimage.registration.phase_cross_correlation(img1, img2)

Version information

Linux-4.18.0-17-generic-x86_64-with-glibc2.27
scikit-image version: 0.19.2
numpy version: 1.20.3
@alexdesiqueira
Copy link
Member

Hey @mwshinn,
thank you for reporting! Would you like to submit a pull request to fix it?
Thanks again!

@mwshinn
Copy link
Author

mwshinn commented May 16, 2022

I can submit a PR to fix it, but it's not clear to me which solution would be preferred. This is not so much a software bug as a conceptual bug: when using a phase-normalised matrix, the metric chosen as the error measure here almost always returns 1. There are a few potential solutions I can think of, listed below in order of preference:

  1. Keep unnormalised correlation as the default, and require the argument flag to switch to phase-normalised. When people switch, they then knowingly accept the fact that their error measure is meaningless. This also is best for backward compatibility with previous versions which used this as the default.
  2. Use the error metric evaluated on the non-phase-normalised matrix. Computation-wise, this requires an additional inverse FFT to be performed, but otherwise, is easy to interpret.
  3. Use a different error metric which works on phase-normalised data

@vincefn
Copy link

vincefn commented May 18, 2022

Hi - I just hit that bug which is present since 0.19.0, as I was relying on the returned error to select the best configuration.

I would support solution 1 above (normalization=None), keeping unnormalised correlation as the default, as this would by default keep the same behaviour as scikit-image<0.19

@github-actions
Copy link

Hey, there hasn't been any activity on this issue for more than 180 days. For now, we have marked it as "dormant" until there is some new activity. You are welcome to reach out to people by mentioning them here or on our forum if you need more feedback! If you think that this issue is no longer relevant, you may close it by yourself; otherwise, we may do it at some point (either way, it will be done manually). In any case, thank you for your contributions so far!

@github-actions github-actions bot added the 😴 Dormant No recent activity label Nov 15, 2022
@lagru lagru added the 🐛 Bug label Sep 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 Bug 😴 Dormant No recent activity
Projects
None yet
Development

No branches or pull requests

5 participants