# Comparing Two Matrix Notation

I have two matrix A and B, I want to find pattern B in matrix A. So I get 2 pattern similar like pattern B. What the name of this operation? and How I write this in mathematics notation?

• Assuming everything is binary, you compute the spatial convolution of $A$ under filter $B$, and are counting the number of times the convolution is at least the sum of the elements in $B$. Jan 28 '20 at 1:12
• Concerning the comment/answer by @cdipaolo: Beware that a convolution flips the kernel with respect to the midoint du to the minus-sign in the definition of the convolution. This is not a problem for your particular kernel B, but other patterns need to be point mirrored before applying the convolution. Jan 30 '20 at 8:12

You are looking for Chamfer-Matching: it will yield a zero value at all center points where the pattern occurs.

Ordinary template matching (aka cross correlation) does not work in your case, because that would also consider the white points of the template, not only the black points. Chamfer-Matching, instead, only sums the distance transform values of A at the black points of B. If '1' stands for black, and D is the distance transform of A, and B is centered such that its midpoint has coordinate $$(0,0))$$, then Chamfer-Matching computes the following image C:

$$C(x,y) = \sum_{dx,dy} D(x+dx, y+dy)\cdot B(dx, dy)$$