https://github.com/mleseach/templatematching.jl
A Julia implementation of template matching methods.
https://github.com/mleseach/templatematching.jl
image-processing julia template-matching
Last synced: 4 months ago
JSON representation
A Julia implementation of template matching methods.
- Host: GitHub
- URL: https://github.com/mleseach/templatematching.jl
- Owner: mleseach
- License: mit
- Created: 2024-02-03T09:30:36.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2025-08-18T13:27:29.000Z (10 months ago)
- Last Synced: 2025-11-10T02:36:00.839Z (8 months ago)
- Topics: image-processing, julia, template-matching
- Language: Julia
- Homepage: https://mleseach.github.io/TemplateMatching.jl/
- Size: 3.33 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TemplateMatching
[](https://mleseach.github.io/TemplateMatching.jl/stable/)
[](https://mleseach.github.io/TemplateMatching.jl/dev/)
[](https://github.com/mleseach/TemplateMatching.jl/actions/workflows/CI.yml?query=branch%3Amaster)
TemplateMatching is a Julia package designed to offer a native Julia implementation of
template matching functionalities similar to those available in OpenCV. This package aims
to provide an easy-to-use interface for image processing and computer vision applications,
allowing users to leverage the high-performance capabilities of Julia for template matching
operations. The package offers performance slightly below that of OpenCV but significantly
better than a naive implementation.
## Documentation
Full documentation and description can be found [here](https://mleseach.github.io/TemplateMatching.jl/stable/)
## Installation
To install TemplateMatching, use the Julia package manager.
Open your Julia command-line interface and run:
```julia
using Pkg
Pkg.add("TemplateMatching")
```
## Features
Masks are not yet supported in the current version of the package.
Unlike OpenCV, TemplateMatching.jl supports n-dimensional arrays[^1].
Below is a table summarising available methods and their equivalent in opencv.
| TemplateMatching.jl | Mask | OpenCV equivalent |
|:--------------------------------|:-------------------:|:-----------------------|
| `SquareDiff` | Not yet supported | `TM_SQDIFF` |
| `NormalizedSquareDiff` | Not yet supported | `TM_SQDIFF_NORMED` |
| `CrossCorrelation` | Not yet supported | `TM_CCORR` |
| `NormalizedCrossCorrelation` | Not yet supported | `TM_CCORR_NORMED` |
| `CorrelationCoeff` | Not yet supported | `TM_CCOEFF` |
| `NormalizedCorrelationCoeff` | Not yet supported | `TM_CCOEFF_NORMED` |
[^1]: Up to 64 dimensions because of an implementation detail, but this shouldn't be a
problem in most cases.
## Short demo
Full demo can be found [here](https://mleseach.github.io/TemplateMatching.jl/stable/demos/demos/demo.html)
Import necessary packages
````julia
using ImageCore # Provides core functionalities for image processing
using ImageDraw # Provides drawing functionalities for images
using TestImages # Supplies a collection of test images for experimentation
using TemplateMatching
````
Load the mandrill test image.
````julia
img = testimage("mandrill")
````

Extract a specific portion of the image to use as the template.
````julia
template = img[50:80, 150:200]
````

Convert the image and template to arrays of Float32 type, then perform template matching using Normalized Square Difference as the metric.
````julia
img_array = channelview(img) .|> Float32
template_array = channelview(template) .|> Float32
result = match_template(img_array, template_array, NormalizedSquareDiff())
result = dropdims(result, dims = 1)
````
Display the grayscale version of the result; darker areas indicate closer matches.
````julia
result .|> Gray
````

Identify the location of the best match (the smallest value in the case of Normalized Square Difference), then draw a rectangle around on the original image.
````julia
loc = argmin(result)
draw(
img,
RectanglePoints(loc[2], loc[1], loc[2] + size(template, 2), loc[1] + size(template, 1)),
RGB(1, 0, 0)
)
````

## License
TemplateMatching is provided under the [MIT License](LICENSE).