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

Template matching with an alpha channel #570

Closed
wants to merge 4 commits into from

Conversation

daladim
Copy link
Contributor

@daladim daladim commented Apr 26, 2024

This MR is adding the match_template_with_alpha (and its match_template_parallel_with_alpha counterpart).
This make this crate support "masked" templates (or "non rectangular" templates or templates "with an alpha channel", name it as you'd like :D )

A few notes:

  • In order not to change the public API, I added new functions, instead of making the same function accept more data types (which would boil down to exposing the _inner function).
  • I'm not sure I am normalizing the right way (in sum_squares) in the case of an image with alpha. I'd be happy to have a review on this part (it's late and my brain is unable to get the meaning of the math behind this normalization, especially as the normalization term depending on the location of the image)

Neat side effects:

  • This implementations de-duplicates (a bit) the code between match_template and match_template_parallel
  • I'm making sure the _parallel functions are tested in CI
  • I'm adding the gray_alpha_image! macro

@daladim daladim changed the title Template matching with alpha Template matching with an alpha channel Apr 26, 2024
@daladim daladim force-pushed the template_matching_with_alpha branch from 294673d to 02dd7c8 Compare April 26, 2024 15:27
@daladim daladim force-pushed the template_matching_with_alpha branch from 02dd7c8 to 2c15a74 Compare April 26, 2024 15:28
@theotherphil
Copy link
Contributor

This and #568 are both adding masked template matching.

cc @daladim and @cospectrum

@cospectrum
Copy link
Contributor

This and #568 are both adding masked template matching.

cc @daladim and @cospectrum

My PR is compatible with OpenCV, which is a priority for me personally.

@theotherphil
Copy link
Contributor

@cospectrum by “compatible with” do you mean being able to produce equal results, or something else?

@cospectrum
Copy link
Contributor

@cospectrum by “compatible with” do you mean being able to produce equal results, or something else?

Equal results, according to their formulas. #567 (comment)

@daladim
Copy link
Contributor Author

daladim commented Apr 28, 2024

I did not see the open issue before implementing. Nice that the formulae for normalisations are posted there (and I realize that I also forgot to square the masked value for the correlation version).
I'll try to compare both MRs tomorrow and see if I can make one with the best of both worlds.

@daladim
Copy link
Contributor Author

daladim commented Apr 29, 2024

@cospectrum 's version is arguably better than mine :) (does not support images-with-alpha-channel, but has a correct computation, especially with regards to the normalization)
I'd be in favor of merging his as-is...before a third guy creates an MR with a third implementation!

I may try to create a separate MR to add tests for the match_template_parallel (currently, no checks are ensuring its results are consistent with the sequential version).
And I won't merge my gray_alpha_image! macro, as no one would use it for now

@daladim
Copy link
Contributor Author

daladim commented Apr 29, 2024

I may as well add a helper function that extracts the template from an image with an alpha channel, to be more convenient when loading a PNG file

@daladim
Copy link
Contributor Author

daladim commented Apr 29, 2024

Before @cospectrum merges his, I've added tests for the parallelized version at cospectrum#1

@daladim
Copy link
Contributor Author

daladim commented Apr 29, 2024

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

Successfully merging this pull request may close these issues.

3 participants