SoFunction
Updated on 2024-11-10

Python and OpenCV for multi-scale template matching implementation

This blog post will realize how to extend standard template matching to multiple scales, thus making it more robust. Making it possible to handle matches with different template and input image sizes.

1. The rendering

Template Matching Problem: False detection occurs for inconsistencies in the template and the image.

The small template on the left side of the image below and the large one in the image on the right side were not detected even though they were identical, just different sizes.

在这里插入图片描述

Optimization: Multi-scale template matching, works perfectly for templates and images with panning and zooming.
Below:

在这里插入图片描述

Multi-scale template matching, gif Detailed rendering

在这里插入图片描述

2. Principles

  • Use for template matching is not very robust. When the size of the template does not match the size on the detected image, it will face false detection.
  • Template matching has translation invariance. It can be made more robust to changes in scalability (i.e., size) by extending it.
  • Multiscale template matching can handle changes in translation and scaling, but is not robust to changes in rotation or non-affine transformations.
  • For rotations on non-affine transformations, keypoints can be detected using detecting keypoints, extracting local invariant descriptors, and applying keypoint matching (keypoint matching).
  • Multi-scale template matching can provide very good results if the templates are fairly strict and the edges are well mapped, and the only concerns are translation and scaling;
  • Template matching using edge mapping instead of the original image can greatly improve the accuracy of template matching.
Template matching does not give a good indication of whether an object does not appear in the image. This can be done by setting a threshold for the correlation coefficient, but is actually unreliable and robust.Optimization: a more robust approach - keypoint matching.

3. Steps

1) In each iteration, the image is resized and Canny edge map is computed;
2) Apply template matching to find the bounding box (x, y) coordinates of the image with the largest correlation coefficient;
3) Finally, store these values in bookkeeping variables;
4) At the end of the algorithm, the (x, y)-coordinates of the region with the largest correlation coefficient response on all scales are found and the bounding box is drawn;

4. Source code

# USAGE
# python  --template cod_logo.png --images images
# USAGE2 Understanding Practical Inspection Principles and Details
# python  --template cod_logo.png --images images --visualize 1

# Import the necessary packages
import argparse  # argparse parses command line arguments
import glob  # Get the path to the input image

import cv2  # opencv bindings
import imutils  # Some methods of image processing
import numpy as np  # numpy for numerical processing

# Build command line and parse parameters
# --template Template path
# --images Original image path
# --visualize Flag whether to show the visualization results for each iteration
ap = ()
ap.add_argument("-t", "--template", required=True, help="Path to template image")
ap.add_argument("-i", "--images", required=True,
                help="Path to images where template will be matched")
ap.add_argument("-v", "--visualize",
                help="Flag indicating whether or not to visualize each iteration")
args = vars(ap.parse_args())

# Load template image, convert grayscale map, detect edges
# Template matching using edges instead of the original image can greatly improve the accuracy of template matching.
template = (args["template"])
template = (template, cv2.COLOR_BGR2GRAY)
template = (template, 50, 200)
(tH, tW) = [:2]
("Template", template)

# Iterate over images to match templates
for imagePath in (args["images"] + "/*.jpg"):

    # Load image, convert to grayscale, initialize bookkeeping variables for tracking matching regions
    image = (imagePath)
    gray = (image, cv2.COLOR_BGR2GRAY)
    found = None

    # Iterate over image dimensions
    for scale in (0.2, 1.0, 20)[::-1]:
        # Scale the image according to scale and keep its aspect ratio
        resized = (gray, width=int([1] * scale))
        r = [1] / float([1])

        # Zoom to image smaller than template, terminate
        if [0] < tH or [1] < tW:
            break

        # Detect edges in scaled grayscale image for template matching
        # Compute a Canny edge representation of the image using exactly the same parameters as the template image;
        # Use application templates to match;
        # Get the correlation result and return a 4-tuple containing the min-correlation value, the max-correlation value, the (x, y)-coordinate of the min-value, and the (x, y)-coordinate of the max-value, respectively. We are only interested in the maximum and (x, y)-coordinates, so we keep only the maximum and discard the minimum.
        edged = (resized, 50, 200)
        result = (edged, template, cv2.TM_CCOEFF)
        (_, maxVal, _, maxLoc) = (result)

        # Check for visualization
        if ("visualize", False):
            # Bounding boxes drawn on detected areas
            clone = ([edged, edged, edged])
            (clone, (maxLoc[0], maxLoc[1]),
                          (maxLoc[0] + tW, maxLoc[1] + tH), (0, 0, 255), 2)
            ("Visualize", clone)
            (0)

        # If we find a new maximum correction value, update the value of the bookkeeping variable
        if found is None or maxVal > found[0]:
            found = (maxVal, maxLoc, r)

    # Unwrap the bookkeeping variables and calculate the bounding box (x, y) coordinates based on the resized ratio
    (_, maxLoc, r) = found
    (startX, startY) = (int(maxLoc[0] * r), int(maxLoc[1] * r))
    (endX, endY) = (int((maxLoc[0] + tW) * r), int((maxLoc[1] + tH) * r))

    # Draw bounding boxes on detection results and display images
    (image, (startX, startY), (endX, endY), (0, 0, 255), 2)
    ("Image", image)
    (0)

5. Reference

/2015/01/26/multi-scale-template-matching-using-python-opencv/

This article on Python and OpenCV for multi-scale template matching implementation of the article is introduced to this, more related OpenCV multi-scale template matching content, please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!