fft_correlator_2d

giant.image_processing:

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

This function performs normalized cross correlation between a template and an image in the frequency domain.

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.)

The correlation in this method is roughly performed by

  1. take the 2D fourier transform of the image and the fliplr/flipud template

  2. multiply each term of the frequency image and template together

  3. take the inverse fourier transform of the product from step 2.

  4. normalize the correlation coefficients

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 as numpy
>>> from giant.image_processing import fft_correlator_2d
>>> example_image = numpy.random.randn(200, 200)
>>> example_template = example_image[30:60, 45:60]
>>> surf = fft_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 (Sequence[Sequence] | ndarray) – The image that the template is to be matched against

  • template (Sequence[Sequence] | 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