Template matching is a computer vision technique that lets you find a smaller image within a larger image. You can use template matching to identify the presence, number of instances of, and locations of regions in an image that match a reference template.

In this guide, we will walk through how to apply template matching to an image in a computer vision application.

Here is a demo of the Workflow we will build in this guide:


Download sample images that work with this Workflow.

Without further ado, let’s get started!

What is Template Matching?

Template matching allows you to find a reference image within a larger image. This is useful for object identification, where you know the shape of an object you expect that you want to find within a larger image.

Template matching falls within the category of classical computer vision techniques. Modern, deep-learning based approaches are able to more effectively identify objects. 

To learn more about object detection and deep learning, refer to our What is Object Detection? Guide.

Let’s talk through the steps required to implement template matching. We will use Roboflow Workflows to build a template matching application that can be deployed in the cloud or locally on your own hardware.

Step #1: Create a Workflow

To get started, create a free Roboflow account. Then, navigate to the Roboflow dashboard and click the Workflows tab in the left sidebar. This will take you to the Workflows homepage, from which you can create a Workflow.

Click “Create Workflow” to create a Workflow.

A window will appear in which you can choose from several templates. For this guide, select “Custom Workflow”:

Click “Create Workflow” to create your Workflow.

You will be taken into the Workflows editor in which you can configure your Workflow:

With a blank Workflow ready, we can start using template matching.

Step #2: Add a Template Matching Block

Click “Add Block” in the top right corner of the Roboflow Workflows builder, then search for “Template Matching”:

When you add the block, you will be asked to configure two variables:

  1. An input image that will be run through the template matching algorithm, and;
  2. A reference “template” image.

On the “Input” block, click the option to add an input image:

Assign a name to the new input image:

This Workflow will accept two inputs:

  1. A reference image, and;
  2. An image on which template matching is run.

Go back to the Template Matching block and make sure the Template Image is the reference image in the input:

Step #3: Add a Visualizer

By default, Workflows returns a JSON response with the information computed from your Workflow. You can also add visualizer blocks to create visual representations of the results from your Workflow.

Let’s add a Bounding Box Visualization so we can see the exact regions where our template matches in an image.

Add a Bounding Box Visualization component:

Connect the block to the input image and the predictions from template matching:

Make sure the input image is equal to image, not template.

We are now ready to test our Workflow!

Step #3: Test the Workflow

Your final Workflow should look like this:

In this Workflow, we:

  1. Accept a template and an input image;
  2. Apply template matching to the input image using the template;
  3. Visualize results from template matching.
  4. Return the template matching information in JSON, and a visual response showing the matched templates.

Let’s test our Workflow.

Click “Run Preview” in the top task bar in the Workflows editor. Set your input image and the template image:

We set the input image as:

The template to match against is:

Here is the result from our Workflow:

Our Workflow successfully identified the location of objects like the reference image we passed through our Workflow.

Conclusion

You can use template matching to localize objects in an image. In this guide, we walked through how to apply template matching with Roboflow Workflows, a web-based computer vision application builder.

We created a new Workflow, added a template matching block, then tested the Workflow with a reference and input image.

To learn more about Roboflow Workflows and the blocks available, check out the Roboflow Workflows Blocks gallery. To see templates of workflows, refer to our Workflows Templates gallery.