# Speedup ListCorrelation when using generalized correlation function

ImageCorrelation provides several general function that can be used to replace the default Dot function. For example, in the documentation of ImageCorrelation, there is an example using CosineDistance as a general function:

ImageCorrelate[p, kr, CosineDistance ]; // AbsoluteTiming
(* {0.006005, Null} *)


We can see that the example runs very fast, and it's almost as fast as the default Dot version

ImageCorrelate[p, kr ]; // AbsoluteTiming
(* {0.004939, Null} *)


ImageCorrelate supports several general correlation functions such as SquaredEuclideanDistance, ManhattanDistance etc, and all of them have the about the same performance as the default one.

So I'm trying to do the similar thing to my data using ListCorrelation.

pData = ImageData[p];
krData = ImageData[kr];

Dimensions[pData]
Dimensions[krData]
(* {200, 150} *)
(* {50, 30} *)

ListCorrelate[krData, pData, {1, -1}, {}, (#1 - #2)^2 &, Plus]; // AbsoluteTiming
(* {38.7298, Null} *)


We can see that ListCorrelate is much slower, and it is orders of magnitudes slower than the default Dot function

ListCorrelate[krData, pData]; // AbsoluteTiming
(* {0.001633, Null} *)


So is there a way to speedup ListCorrelate to the same level of ImageCorrelate, for commonly used general functions in ImageCorrelate such as SquaredEuclideanDistance, CosineDistance etc?

• The generalised version of ListCorrelate was always a real lot slower than the default (which is a shame because it opens up some nice usage possibilities). So unless some rewriting of the function too place recently you are not likely to match or get close the speed of the other function. – Mike Honeychurch Dec 23 '15 at 23:06
• Might get modest improvement by using Compile on theoptional function. – Daniel Lichtblau Dec 24 '15 at 20:07

ImageCorrelate[pData//Image, krData, CosineDistance]//ImageData