Skip to content

Enhanced Correlation Coefficient implementation for distortion correction - #7666

Draft
loicmusy wants to merge 17 commits into
scikit-image:pre-megamove-integrationfrom
loicmusy:ecc_transform
Draft

loicmusy wants to merge 17 commits into
scikit-image:pre-megamove-integrationfrom
loicmusy:ecc_transform

Conversation

@loicmusy

Copy link
Copy Markdown

Description

(Modified from PR#7050)
Implementation of the enhanced correlation coefficient used for image alignement.

The code is based on the original paper from G.D Evangelidis and the implementation by the authors into OpenCV (here).

Compared to the initial implementation in PR#7050, this one leverage numpy broadcasting for most of the operations, and therefore is roughly 40% faster.

Checklist

Release note

For maintainers and optionally contributors, please refer to the instructions on how to document this PR for the release notes.

Add `find_transform_ECC()` in skimage.registration, which allows for the determination of a transformation matrix linking two images.

@lagru lagru 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.

Thank you @Coilm for coming back to this. This is marked as a "Draft" but let me know if you already want broad feedback and / or help. :)

@lagru

lagru commented Jan 23, 2025

Copy link
Copy Markdown
Member

Please ignore most of the CI for now. The one things you could do is to setup & run your pre-commit hooks:

pre-commit install
pre-commit run --all # invoke hooks manually
# commit changes

The pre-commit.ci - pr job is complaining about some formatting.

@loicmusy

Copy link
Copy Markdown
Author

@lagru hello ! I definitely would be interested in some feedback on the implementation,

I marked it as draft because it is still missing proper docstring/tests/demos.

@lagru
lagru self-requested a review January 24, 2025 17:30
@loicmusy

Copy link
Copy Markdown
Author

Anyone is able to help me with the circleci;doc error?

ModuleNotFoundError: No module named 'skimage.registration._enhanced_correlation_coef'

I am probably missing some part of init somewhere?

@stefanv

stefanv commented Jan 31, 2025

Copy link
Copy Markdown
Member

@Coilm I think you forgot to update https://github.com/scikit-image/scikit-image/blob/main/skimage/registration/meson.build

@eliotl

eliotl commented Feb 9, 2025

Copy link
Copy Markdown

Hi @Coilm, I found this because I'm looking into using ECC in python, great work! Cheeky question: I wish there were an additional motion type between euclidean and affine for similarity transforms: e.g. it would use translation, rotation, and scaling, but not shear. Does that seem possible with ECC? Not a feature request, just curious in general how you would implement it.

@loicmusy

loicmusy commented Feb 10, 2025 •

Copy link
Copy Markdown
Author

Hello @eliotl,
This should indeed be possible by implementing an extra jacobian calculation. In fact we are currently discussing in the scikit-image zulip about this. However the focus is now in implementing this PR and PR #7421, and custom transform would be discussed at a later time.

If you want to try to implement it yourself, here is the procedure:

Considere your transformation matrix defined in the homogeneous coordinate $X = (x,y,1)^T$, in your case it would be a combinaison of a scaling $S$, a rotation $R$ and a translation $T$, the transformation matrix M is then $$M = TSR$$, with

$$S = \begin{pmatrix} S_x & 0 & 0 \\ 0 & S_y & 0 \\ 0 & 0 & 1 \end{pmatrix}, R = \begin{pmatrix} \cos(\theta) & \sin(\theta) & 0 \\ -\sin(\theta) & \cos(\theta) & 0 \\ 0 & 0 & 1 \end{pmatrix}, T = \begin{pmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{pmatrix} $$

If you do the matrix product, then $M$ is:

$$M = \begin{pmatrix} S_x\cos(\theta) & S_x\sin(\theta) & t_x \\ -S_y\sin(\theta) & S_y\cos(\theta) & t_y \\ 0 & 0 & 1 \end{pmatrix} $$

Therefore MX give us two equations:

$$\phi_1 = S_x \cos(\theta)x - S_x \sin(\theta)y + t_x$$ $$\phi_2 = S_y \sin(\theta)x + S_y \cos(\theta)y + t_y$$

Then the gradient is defined as

$$G_{(n)} = \frac{\partial I}{\partial x} \frac{\partial \phi_1}{\partial a_n} + \frac{\partial I}{\partial y} \frac{\partial \phi_2}{\partial a_n}$$

where $a_n$, would be $\theta$, $s_x$, $s_y$, $t_x$, $t_y$.

$$G_{\theta} = \frac{\partial I}{\partial x} S_x(-\sin(\theta)x - \cos(\theta)y) + \frac{\partial I}{\partial y}S_y(\cos(\theta)x - \sin(\theta)y)$$ $$G_{S_x} = \frac{\partial I}{\partial x} (\cos(\theta) x - \sin(\theta) y)$$ $$G_{S_y} = \frac{\partial I}{\partial y} (\cos(\theta) y + \sin(\theta) x)$$ $$G_{t_x} = \frac{\partial I}{\partial x}$$ $$G_{t_y} = \frac{\partial I}{\partial x}$$

Therefore the function to compute the jacobian should look something like this (did not test it):

def compute_jacobian_custom(grad, xy_grid, warp_matrix):
    grad_iw_x, grad_iw_y = grad
    x_grid, y_grid = xy_grid

    h0 = warp_matrix[0, 0]  # a0 cos(theta)
    h1 = -warp_matrix[0, 1] # a0 sin(theta)
    h2 = warp_matrix[1,0 ]  # a1 sin(theta)
    h3 = warp_matrix[1, 1]  # a1 cos(theta)
    theta = np.arctan2(h1, h0)

    return np.stack([
        (-h1 * x_grid - h0 * y_grid) * grad_iw_x + (h3 * x_grid - h2 * y_grid) * grad_iw_y,
        grad_iw_x * (np.cos(theta) * x_grid - np.sin(theta) * y_grid),
        grad_iw_y * (np.sin(theta) * x_grid + np.cos(theta) * y_grid),
        grad_iw_x,
        grad_iw_y
        ])

and the update function would be something like this:

def update_warping_matrix_custom(map_matrix, update):
    new_theta = update[0]
    new_theta += np.arctan2(-map_matrix[0, 1], map_matrix[0, 0])

    map_matrix[0, 0] = update[1] * np.cos(new_theta)
    map_matrix[1, 1] = update[2] * np.cos(new_theta)
    map_matrix[1, 0] = update[2] * np.sin(new_theta)
    map_matrix[0, 1] = -update[1]* np.sin(new_theta)

    map_matrix[0, 2] += update[3]
    map_matrix[1, 2] += update[4]

    return map_matrix

Let me know if you manage to make it work, or if you need help.

@eliotl

eliotl commented Feb 11, 2025

Copy link
Copy Markdown

😍 wow, thank you so much!

@stefanv stefanv mentioned this pull request Mar 11, 2025
5 of 6 tasks
@tkunicki

Copy link
Copy Markdown

@Coilm Thanks for this work. I have been testing it with some large images and have some minor changes to support running using CuPy on the GPU, reduce memory footprint, and support homographic transforms (CuPy doesn't support geometric_transform). Is there a way to share these changes if you're interested?

@loicmusy

Copy link
Copy Markdown
Author

@Coilm Thanks for this work. I have been testing it with some large images and have some minor changes to support running using CuPy on the GPU, reduce memory footprint, and support homographic transforms (CuPy doesn't support geometric_transform). Is there a way to share these changes if you're interested?

Hello @tkunicki ! I am glad to see that this implementation was usefull for you. You can do a pull request on my mirror:
https://github.com/Coilm/scikit-image/pulls and I'll have a look!

@matthew-brett
matthew-brett changed the base branch from main to pre-megamove-integration June 19, 2026 19:57
@matthew-brett

Copy link
Copy Markdown
Contributor

We have changed the base branch for this PR from the main branch to the pre-megamove-integration branch.

Summary - what this means for your PR

You can continue to work on your PR, now based on pre-megamove-integration. You should update your branch to the current state of pre-megamove-integration (this is what the Github "Update branch" button does). Please do not merge the scikit-image main branch into your PR branch.

Once we've merged your PR into the pre-megamove-integration branch, we'll update your changes for Megamove, and merge into the main branch.

In addition - our automated script detected that your branch had merge conflicts with the pre-Megamove branch. Please make sure to merge in the pre-megamove-integration branch, and resolve the merge conflicts in the way that you want.

Read on for the Megamove rationale, and more detailed instructions.

Details

Rationale

We've changed the base branch to make it easier and smoother to continue to work on your PR, while we work through a rather large change in the directory structure of the repository - a change that we have called Megamove.

See the Megamove explanation issue for more detail.

Changing the base branch means you do not see merge conflicts related to the Megamove change, and can continue to work with the repository in its original state, without worrying about porting those changes across to the new post-Megamove structure.

As you'll see in the linked issue, you can continue to work on your PR as before, and you can continue to use the Github GUI to "Update branch" - this updates from the new pre-megamove-integration branch. However, there is one caveat: please, do not try and merge the scikit-image main branch into this one, but instead, keep track of the pre-megamove-integration branch. The issue explains more.

To emphasize again, do not try and merge the scikit-image main branch. You will get Megamove related merge conflicts that are rather tricky to resolve correctly.

When we've approved your PR, we'll merge into the pre-megamove-integration branch, and then process it so we can merge it into the main branch. We'll let you know when we've merged your PR changes into main.

Any questions, please reply here.

This branch has not been deployed

No deployments
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.

6 participants