# [SciPy-User] how to interpret scipy.signal.correlate2d

Tony Yu tsyu80@gmail....
Mon Sep 10 10:45:03 CDT 2012

```On Mon, Sep 10, 2012 at 8:42 AM, Davide Cittaro <daweonline@gmail.com>wrote:

> Hi all,
> I would like to correlate two square matrices (same dimensions) with
> scipy.signal.correlate2d but I have some doubt on interpretation of
> results. In particular, I have a matrix A=(M, N) and another b=(m, n), when
> I apply correlate2d(A, b) I obtain a new matrix c=(M+m, N+n). What is the
> meaning of each element in the new matrix c?
> I'm trying to find out which regions of two images are similar.
>
> Thanks
>
> d
> ---
> Davide Cittaro
> daweonline@gmail.com
> http://sites.google.com/site/davidecittaro/
>
>

Hi Davide,

The size of the output depends on how you treat the boundaries of the
cross-correlation. By default, the boundary "mode" is "full". When
"mode=full", correlate2d will return values when *any* part of the two
matrices overlap. So, for example, the `c[0, 0]` value in the output would
just be the product `A[0, 0] * b[-1, -1]` (only the bottom-right corner of
`b` overlaps the top-left corner of `A`).

You could also set "mode=valid" and "mode=same", which (I think) gives
results (M-m+1, N-n+1) and (M, N), respectively. "mode=valid" only returns
values when the `b` doesn't hang over the borders of `A`; "mode=same" uses
just enough over hang (or zero-padding) to get an output that's the same
size as `A`. BTW, the actual size for "mode=full" is (M+m-1, N+n-1).

Also, you may be interested using a normalized cross-correlation, which is
implemented in scikits-image:
http://scikits-image.org/docs/dev/auto_examples/plot_template.html

Cheers,
-Tony
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20120910/fc30e7d2/attachment-0001.html
```

More information about the SciPy-User mailing list