imageproc icon indicating copy to clipboard operation
imageproc copied to clipboard

Add template matching

Open theotherphil opened this issue 6 years ago • 6 comments

https://docs.opencv.org/3.2.0/de/da9/tutorial_template_matching.html

https://github.com/opencv/opencv/blob/master/modules/imgproc/src/templmatch.cpp

https://www.reddit.com/r/rust/comments/7zg820/looking_for_image_processing_library_with/

  • Greyscale SSE: https://github.com/PistonDevelopers/imageproc/pull/257
  • Optional normalisation of SSE metric: https://github.com/PistonDevelopers/imageproc/pull/259

theotherphil avatar Feb 22 '18 18:02 theotherphil

Hi, Opencv has minmaxloc function which is used to find the coordinates of the best match, what would be the best way to do it here? Thanks.

edit: tried this now and it takes around 1.25 secs on a 500 x 1000 px image and 60 x 30 template. Am I doing something wrong or is it supposed to be this slow?

estw272 avatar Apr 15 '18 15:04 estw272

Implementing minxMaxLoc should be easy using the pixels iterator from GenericImage: https://docs.rs/image/0.18.0/image/trait.GenericImage.html#method.pixels. Edit: implemented in https://github.com/PistonDevelopers/imageproc/pull/262

That performance doesn't sound surprising. This implementation does O(width * height * template width * template height) work. I suspect the OpenCV implementation uses FFT rather than the naive matching used here.

Edit: note for myself (or anyone else who might have a go at making this faster) - this rust FFT library looks promising https://github.com/awelkie/RustFFT

theotherphil avatar Apr 15 '18 19:04 theotherphil

The example provided seems to be failing?:

$ cat Cargo.toml
[package]
name = "imgproc_match_template_test"
version = "0.1.0"
edition = "2021"

[dependencies]
image = "0.25.0"
imageproc = "0.23.0"

$ cargo build
(...)
   Compiling imgproc_match_template_test v0.1.0 (/Users/rvalls/dev/personal/imgproc_match_template_test)
error[E0277]: the trait bound `Luma<f32>: image::traits::Pixel` is not satisfied
  --> src/main.rs:64:34
   |
64 | fn convert_to_gray_image(image: &Image<Luma<f32>>) -> GrayImage {
   |                                  ^^^^^^^^^^^^^^^^ the trait `image::traits::Pixel` is not implemented for `Luma<f32>`
   |
   = help: the following other types implement trait `image::traits::Pixel`:
             image::color::Rgb<T>
             image::color::Luma<T>
             image::color::Rgba<T>
             image::color::LumaA<T>

For more information about this error, try `rustc --explain E0277`.
error: could not compile `imgproc_match_template_test` (bin "imgproc_match_template_test") due to 1 previous error

/cc @cospectrum Most probably I'm doing something wrong here since CI should have flagged that example?

brainstorm avatar Mar 14 '24 11:03 brainstorm

@brainstorm Try image = "0.24". We haven't updated to "0.25" yet.

cospectrum avatar Mar 14 '24 13:03 cospectrum

@brainstorm Try image = "0.24". We haven't updated to "0.25" yet.

Well, forget versions, I just tried directly from HEAD:

[package]
name = "imgproc_match_template_test"
version = "0.1.0"
edition = "2021"

[dependencies]
image = { git="https://github.com/image-rs/image" }
imageproc = { git="https://github.com/image-rs/imageproc", features = ["rayon"] }

And faced the exact same error I pointed above :_/

brainstorm avatar Mar 14 '24 22:03 brainstorm

@brainstorm 0.25 and 0.24 are not semver compatible, cargo will treat them as 2 different crates. imageproc depends on 0.24 both in master and on crates.io

cospectrum avatar Mar 14 '24 22:03 cospectrum

Can this issue be closed since imageproc::template_matching::match_template() now exists?

ripytide avatar May 12 '24 19:05 ripytide