ImageDev

CorrelationByDifference2d

Performs a correlation by the difference between a two-dimensional grayscale image and a grayscale kernel.

Access to parameter description

For an introduction: section Image Correlation.

This algorithm performs a correlation with a difference metric between a gray level image $I$ and a gray level kernel $K$, returning the correlation image $O$.

The different possibilities for selecting a correlation peak are presented below using a 1-D correlation between an image and kernel. In the image, the kernel appears 6 times with different contrast and luminosity.
<b> Figure 1.</b> 1D image and kernel
Figure 1. 1D image and kernel

The 6 examples show the kernel appearing with different contrast and luminosity.

<b> Figure 2.</b> Examples of possible 1D correlation
Figure 2. Examples of possible 1D correlation


The correlation by difference metric is computed in accordance with the CorrelationMode parameter.

DIRECT Correlation Mode

$$ O(n,m)=\sum_{i=1}^{kx} \sum_{j=1}^{ky} \left|K(i,j)-I(n+i-\frac{kx}{2},m+j-\frac{ky}{2})\right| $$ In this mode, 3 of the 6 patterns matching the kernel are detected. These are only patterns having similar luminosity. The best matching is obtained with the similar contrast and luminosity pattern.

<b> Figure 3.</b> Example of Difference 1D direct correlation
Figure 3. Example of Difference 1D direct correlation

MEAN Correlation Mode

$$ O(n,m)=\sum_{i=1}^{kx} \sum_{j=1}^{ky} \left|\left(K(i,j)-\mu(K)\right)-\left(I(n+i-\frac{kx}{2}, m+j-\frac{ky}{2})-\mu(I)(n,m)\right)\right| $$ In this mode, 2 of the 6 patterns matching the kernel are detected. These are only patterns having similar contrast. The confidence rate is the same for the 2 patterns.
<b> Figure 4.</b> Example of Difference 1D mean correlation
Figure 4. Example of Difference 1D mean correlation

VARIANCE Correlation Mode

$$ O(n,m)=\frac{\sum\limits_{i=1}^{kx} \sum\limits_{j=1}^{ky} \left|K(i,j)-I(n+i-\frac{kx}{2}, m+j-\frac{ky}{2})\right|}{\sqrt{\sigma^2(K)\times \sigma^2(I)(n,m)}} $$ In this mode, 3 of the 6 patterns matching the kernel are detected. These are only patterns having similar luminosity. The confidence rate is the same for the 3 patterns.

<b> Figure 5.</b> Example of Difference 1D variance correlation
Figure 5. Example of Difference 1D variance correlation

MEAN_VARIANCE Correlation Mode

$$ O(n,m)=\frac{\sum\limits_{i=1}^{kx} \sum\limits_{j=1}^{ky} \left|\left(K(i,j)-\mu(K)\right) -\left(I(n+i-\frac{kx}{2},m+j-\frac{ky}{2})-\mu(I)(n,m)\right)\right|}{\sqrt{\sigma^2(K)\times \sigma^2(I)(n,m)}} $$ In this mode, the 6 patterns matching the kernel are detected, with the same confidence rate.

<b> Figure 6.</b> Example of Difference 1D mean and variance correlation
Figure 6. Example of Difference 1D mean and variance correlation


where: $$ \mu(K)=\frac{\sum\limits_{i=1}^{kx} \sum\limits_{j=1}^{ky} K(i,j)}{kx\times ky} $$ $$ \mu(I)(n,m)=\frac{\sum\limits_{i=1}^{kx} \sum\limits_{j=1}^{ky} I(n+i,m+j)}{kx\times ky} $$ $$ \sigma^2(K)=\sum_{i=1}^{kx} \sum_{j=1}^{ky} \left(K(i,j)-\mu(K)\right)^{2} $$ $$ \sigma^2(I)(n,m)=\sum_{i=1}^{kx} \sum_{j=1}^{ky} \left( I(n+i-\frac{kx}{2},m+j-\frac{ky}{2} ) - \mu(I)(n,m)\right)^{2} $$
Note: This algorithm returns the main correlation peak in the outputMeasurement object. More correlation peaks can be extracted from the outputImage correlation image with the LocalMaxima2d algorithm.

See also

Function Syntax

This function returns a CorrelationByDifference2dOutput structure containing outputImage and outputMeasurement.
// Output structure of the correlationByDifference2d function.
struct CorrelationByDifference2dOutput
{
    /// The output correlation image. Its dimensions are forced to the same values as the input. Its data type is forced to floating point.
    std::shared_ptr< iolink::ImageView > outputImage;
    /// The correlation matching results.
    CorrelationMsr::Ptr outputMeasurement;
};

// Function prototype
CorrelationByDifference2dOutput correlationByDifference2d( std::shared_ptr< iolink::ImageView > inputImage, std::shared_ptr< iolink::ImageView > inputKernelImage, CorrelationByDifference2d::OffsetMode offsetMode, CorrelationByDifference2d::CorrelationMode correlationMode, std::shared_ptr< iolink::ImageView > outputImage = nullptr, CorrelationMsr::Ptr outputMeasurement = nullptr );
This function returns a tuple containing output_image and output_measurement.
// Function prototype.
correlation_by_difference_2d(input_image: idt.ImageType,
                             input_kernel_image: idt.ImageType,
                             offset_mode: CorrelationByDifference2d.OffsetMode = CorrelationByDifference2d.OffsetMode.OFFSET_1,
                             correlation_mode: CorrelationByDifference2d.CorrelationMode = CorrelationByDifference2d.CorrelationMode.DIRECT,
                             output_image: idt.ImageType = None,
                             output_measurement: Union[Any, None] = None) -> Tuple[idt.ImageType, CorrelationMsr]
This function returns a CorrelationByDifference2dOutput structure containing outputImage and outputMeasurement.
/// Output structure of the CorrelationByDifference2d function.
public struct CorrelationByDifference2dOutput
{
    /// 
    /// The output correlation image. Its dimensions are forced to the same values as the input. Its data type is forced to floating point.
    /// 
    public IOLink.ImageView outputImage;
    /// The correlation matching results.
    public CorrelationMsr outputMeasurement;
};

// Function prototype.
public static CorrelationByDifference2dOutput
CorrelationByDifference2d( IOLink.ImageView inputImage,
                           IOLink.ImageView inputKernelImage,
                           CorrelationByDifference2d.OffsetMode offsetMode = ImageDev.CorrelationByDifference2d.OffsetMode.OFFSET_1,
                           CorrelationByDifference2d.CorrelationMode correlationMode = ImageDev.CorrelationByDifference2d.CorrelationMode.DIRECT,
                           IOLink.ImageView outputImage = null,
                           CorrelationMsr outputMeasurement = null );

Class Syntax

Parameters

Parameter Name Description Type Supported Values Default Value
input
inputImage
The input grayscale image. Image Binary, Label or Grayscale nullptr
input
inputKernelImage
The correlation kernel. Image Binary, Label or Grayscale nullptr
input
offsetMode
The calculation offset, in pixels. The greater this value, computation is faster but detection is less precise.
OFFSET_1 The correlation is computed with a step of 1.
OFFSET_2 The correlation is computed with a step of 2.
OFFSET_4 The correlation is computed with a step of 4.
OFFSET_8 The correlation is computed with a step of 8.
Enumeration OFFSET_1
input
correlationMode
The normalization mode for correlation.
DIRECT Direct correlation (no normalization).
MEAN Mean normalized correlation (luminosity).
VARIANCE Variance normalized correlation (contrast).
MEAN_VARIANCE Mean and variance normalized correlation (luminosity and contrast).
Enumeration DIRECT
output
outputImage
The output correlation image. Its dimensions are forced to the same values as the input. Its data type is forced to floating point. Image nullptr
output
outputMeasurement
The correlation matching results. CorrelationMsr nullptr
Parameter Name Description Type Supported Values Default Value
input
input_image
The input grayscale image. image Binary, Label or Grayscale None
input
input_kernel_image
The correlation kernel. image Binary, Label or Grayscale None
input
offset_mode
The calculation offset, in pixels. The greater this value, computation is faster but detection is less precise.
OFFSET_1 The correlation is computed with a step of 1.
OFFSET_2 The correlation is computed with a step of 2.
OFFSET_4 The correlation is computed with a step of 4.
OFFSET_8 The correlation is computed with a step of 8.
enumeration OFFSET_1
input
correlation_mode
The normalization mode for correlation.
DIRECT Direct correlation (no normalization).
MEAN Mean normalized correlation (luminosity).
VARIANCE Variance normalized correlation (contrast).
MEAN_VARIANCE Mean and variance normalized correlation (luminosity and contrast).
enumeration DIRECT
output
output_image
The output correlation image. Its dimensions are forced to the same values as the input. Its data type is forced to floating point. image None
output
output_measurement
The correlation matching results. CorrelationMsr None
Parameter Name Description Type Supported Values Default Value
input
inputImage
The input grayscale image. Image Binary, Label or Grayscale null
input
inputKernelImage
The correlation kernel. Image Binary, Label or Grayscale null
input
offsetMode
The calculation offset, in pixels. The greater this value, computation is faster but detection is less precise.
OFFSET_1 The correlation is computed with a step of 1.
OFFSET_2 The correlation is computed with a step of 2.
OFFSET_4 The correlation is computed with a step of 4.
OFFSET_8 The correlation is computed with a step of 8.
Enumeration OFFSET_1
input
correlationMode
The normalization mode for correlation.
DIRECT Direct correlation (no normalization).
MEAN Mean normalized correlation (luminosity).
VARIANCE Variance normalized correlation (contrast).
MEAN_VARIANCE Mean and variance normalized correlation (luminosity and contrast).
Enumeration DIRECT
output
outputImage
The output correlation image. Its dimensions are forced to the same values as the input. Its data type is forced to floating point. Image null
output
outputMeasurement
The correlation matching results. CorrelationMsr null

Object Examples

auto polystyrene = ioformat::readImage( std::string( IMAGEDEVDATA_IMAGES_FOLDER ) + "polystyrene.tif" );

CorrelationByDifference2d correlationByDifference2dAlgo;
correlationByDifference2dAlgo.setInputImage( polystyrene );
correlationByDifference2dAlgo.setInputKernelImage( polystyrene );
correlationByDifference2dAlgo.setOffsetMode( CorrelationByDifference2d::OffsetMode::OFFSET_1 );
correlationByDifference2dAlgo.setCorrelationMode( CorrelationByDifference2d::CorrelationMode::DIRECT );
correlationByDifference2dAlgo.execute();

std::cout << "outputImage:" << correlationByDifference2dAlgo.outputImage()->toString();
std::cout << "minComputed: " << correlationByDifference2dAlgo.outputMeasurement()->minComputed( 0 ) ;
polystyrene = ioformat.read_image(imagedev_data.get_image_path("polystyrene.tif"))

correlation_by_difference_2d_algo = imagedev.CorrelationByDifference2d()
correlation_by_difference_2d_algo.input_image = polystyrene
correlation_by_difference_2d_algo.input_kernel_image = polystyrene
correlation_by_difference_2d_algo.offset_mode = imagedev.CorrelationByDifference2d.OFFSET_1
correlation_by_difference_2d_algo.correlation_mode = imagedev.CorrelationByDifference2d.DIRECT
correlation_by_difference_2d_algo.execute()

print("output_image:", str(correlation_by_difference_2d_algo.output_image))
print("minComputed: ", str(correlation_by_difference_2d_algo.output_measurement.min_computed(0)))
ImageView polystyrene = ViewIO.ReadImage( @"Data/images/polystyrene.tif" );

CorrelationByDifference2d correlationByDifference2dAlgo = new CorrelationByDifference2d
{
    inputImage = polystyrene,
    inputKernelImage = polystyrene,
    offsetMode = CorrelationByDifference2d.OffsetMode.OFFSET_1,
    correlationMode = CorrelationByDifference2d.CorrelationMode.DIRECT
};
correlationByDifference2dAlgo.Execute();

Console.WriteLine( "outputImage:" + correlationByDifference2dAlgo.outputImage.ToString() );
Console.WriteLine( "minComputed: " + correlationByDifference2dAlgo.outputMeasurement.minComputed( 0 ) );

Function Examples

auto polystyrene = ioformat::readImage( std::string( IMAGEDEVDATA_IMAGES_FOLDER ) + "polystyrene.tif" );

auto result = correlationByDifference2d( polystyrene, polystyrene, CorrelationByDifference2d::OffsetMode::OFFSET_1, CorrelationByDifference2d::CorrelationMode::DIRECT );

std::cout << "outputImage:" << result.outputImage->toString();
std::cout << "minComputed: " << result.outputMeasurement->minComputed( 0 ) ;
polystyrene = ioformat.read_image(imagedev_data.get_image_path("polystyrene.tif"))

result_output_image, result_output_measurement = imagedev.correlation_by_difference_2d(polystyrene, polystyrene, imagedev.CorrelationByDifference2d.OFFSET_1, imagedev.CorrelationByDifference2d.DIRECT)

print("output_image:", str(result_output_image))
print("minComputed: ", str(result_output_measurement.min_computed(0)))
ImageView polystyrene = ViewIO.ReadImage( @"Data/images/polystyrene.tif" );

Processing.CorrelationByDifference2dOutput result = Processing.CorrelationByDifference2d( polystyrene, polystyrene, CorrelationByDifference2d.OffsetMode.OFFSET_1, CorrelationByDifference2d.CorrelationMode.DIRECT );

Console.WriteLine( "outputImage:" + result.outputImage.ToString() );
Console.WriteLine(  "minComputed: " + result.outputMeasurement.minComputed( 0 )  );