spatial_correlator_2d

giant.image_processing:

giant.image_processing.spatial_correlator_2d(image, template)[source]

This function performs normalized cross correlation directly (spatial correlation).

The correlation is performed over the full image, aligning the center of the template with every pixel in the image. (Note that this means that if the center of the template should be outside of the image this function will not work.)

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 spatial_correlator_2d
>>> example_image = numpy.random.randn(200, 200)
>>> example_template = example_image[30:60, 45:60]
>>> surf = spatial_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 -- replace 0, 0 with whichever template location you
>>> # want (starting with the upper left as 0, 0).
>>> 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

Returns:

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

Return type:

ndarray