<< cornerHarris | Object detection | Datatypes >>

scicv >> Object detection > matchTemplate

# matchTemplate

Finds the locations of a template image in a larger image

### Syntax

    result = matchTemplate(img, img_template, method)

### Parameters

img

  Image in which the template is searched (Mat).

img_template

  Searched template, must be of same data type as img (Mat).

result

  Map of matching results (32-bit floating point Mat).

method

  comparison method (double).

  - `CV_TM_SQDIFF`: square difference method, perfect match will be 0, bad match will be large.
  - `CV_TM_SQDIFF_NORMED`: normalized version of the square difference method.
  - `CV_TM_CCORR`: correlation method, perfect match will be large, bad match will be 0.
  - `CV_TM_CCORR_NORMED`: normalized version of the correlation method.
  - `CV_TM_CCOEFF`: correlation coefficient method (correlation of the template relative to its mean against the image relative to its mean), perfect match will be 1 perfect mismatch will be -1, 0 means no correlation.
  - `CV_TM_CCOEFF_NORMED`: normalized version of the correlation coefficient method.

### Description

The function slides through the image image, compares with template template_img using the specified method method and stores the comparison results in result.

Results are more accurate with correlation methods than simpler methods square difference method, but require more computations.

### Examples

    scicv_Init();

    img = imread(getSampleImage("puffins.png"));

    img_template = imread(getSampleImage("puffin_pattern.png"));

    img_result = matchTemplate(img, img_template, CV_TM_SQDIFF_NORMED);

    gcf().color_map = graycolormap(255);
    colorbar(0, 255);
    img_out = normalize(img_result, 0, 255, NORM_MINMAX);

    matplot(img_out);

    delete_Mat(img);
    delete_Mat(img_template);
    delete_Mat(img_result);

### See also

- minMaxLoc

Report an issue
<< cornerHarris | Object detection | Datatypes >>