image-rs / imageproc

Image processing operations
MIT License
735 stars 145 forks source link

Template matching with an alpha channel #570

Closed daladim closed 4 months ago

daladim commented 4 months ago

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:

Neat side effects:

theotherphil commented 4 months ago

This and https://github.com/image-rs/imageproc/pull/568 are both adding masked template matching.

cc @daladim and @cospectrum

cospectrum commented 4 months ago

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 commented 4 months ago

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

cospectrum commented 4 months ago

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

Equal results, according to their formulas. https://github.com/image-rs/imageproc/issues/567#issuecomment-2053753777

daladim commented 4 months ago

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 commented 4 months ago

@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 commented 4 months ago

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 commented 4 months ago

Before @cospectrum merges his, I've added tests for the parallelized version at https://github.com/cospectrum/imageproc/pull/1

daladim commented 4 months ago

This MR could be closed now I've created