Skip to contents

matchTemplate compares a template against overlapping image regions using the specified method. After the function finishes the comparison, the best matches can be found as global minimums (when methods "SQDIFF" or "SQDIFF_NORMED" are used) or maximums (when methods "CCORR", "CCORR_NORMED", "CCOEFF" or "CCOEF_NORMED" are used) using the minMaxLoc function.

Usage

matchTemplate(image, template, method, mask = NULL, target = "new")

Arguments

image

An Image object.

template

An Image object with the same number of channels and bit depth as image. template cannot be greater than image in any dimension.

method

A string indicating the comparison method to use. It can be any of the following (see https://bit.ly/2RjELvJ for a full description of each comparison method):

  • "SQDIFF"

  • "SQDIFF_NORMED"

  • "CCORR"

  • "CCORR_NORMED"

  • "CCOEFF"

  • "CCOEFF_NORMED"

mask

An Image object with the same dimensions as template (default: NULL). It can have either one channel or the same number of channels as template. It can be an 8U or 32F Image object. If 8U, it is interpreted as a binary mask, meaning only elements where mask is nonzero are used and are kept unchanged independent of the actual mask value. If 32F, then the mask values are used as weights. mask is not supported when method='CCOEFF_NORMED'.

target

The location where the results should be stored. It can take 2 values:

"new":

a new Image object is created and the results are stored inside (the default).

An Image object:

the results are stored in another existing Image object. This is fast but will replace the content of target. Note that target must be a single-channel, 32F Image object with (R-r+1) rows and (C-c+1) columns, where CxR and cxr are the dimensions of image and template, respectively.

Value

If target="new", the function returns an Image object with (R-r+1) rows and (C-c+1) columns, where CxR and cxr are the dimensions of image and template, respectively. If target is an Image object, the function returns nothing and modifies that Image object in place.

See also

Author

Simon Garnier, garnier@njit.edu

Examples

balloon <- image(system.file("sample_img/balloon1.png", package = "Rvision"))
sub <- subImage(balloon, 290, 170, 150, 150)
match <- matchTemplate(balloon, sub, method = "SQDIFF")
mm <- minMaxLoc(match)