cv2_correlator_2d

giant.image_processing:

giant.image_processing.cv2_correlator_2d(image, template, flag=5)[source]

This function performs a 2D cross correlation between image and template and returns the correlation surface using the OpenCV matchTemplate function.

The input image and template are first converted to single precision (as is required by matchTemplate) and then given to the matchTemplate function.

The flag indicates the correlation coefficients to calculate (in general you will want cv2.TM_CCOEFF_NORMED for normalized cross correlation). For more information about this function see the OpenCV documentation at https://docs.opencv.org/master/d4/dc6/tutorial_py_template_matching.html

Each pixel of the correlation surface returned by this function represents the correlation value when the center of the template is placed at this location. Thus, the location of any point in the template can be found by

>>> import numpy
>>> from giant.image_processing import cv2_correlator_2d
>>> example_image = numpy.random.randn(200, 200)
>>> example_template = example_image[30:60, 45:60]
>>> surf = cv2_correlator_2d(example_image, example_template)
>>> temp_middle = numpy.floor(numpy.array(example_template.shape)/2)
>>> template_point = numpy.array([0, 0])  # upper left corner
>>> template_point - temp_middle + numpy.unravel_index(surf.argmax(), surf.shape)
array([30., 45.])
Parameters:
  • image (ndarray) – The image that the template is to be matched against

  • template (ndarray) – the template that is to be matched against the image

  • flag (int) – A flag indicating the correlation coefficient to be calculated

Returns:

A surface of the correlation coefficients for each overlap between the template and the image.

Return type:

ndarray