I drastically improved my matching algorithm by combining it with a histogram comparison.
My final matching probability is calculated like this:
probability = (matchingProbability * 2 + histProbability) / 3
Also I found that inverting images can improve template matching further.
this.tmplMeanStdDev = getMeanStdDev(tmpl)
const meanArr = tmpl.mean()
$E(u,v)$ is zero for zero shift ($u=v=0$), where the two patches being compared are equal. As you increase the distance, the difference tends to increase. Depending on the local shape of the image, the $E(u,v)$ surface will have a different shape: at a corner or a small point, it will steeply increase from 0 in all directions. Along an edge, it will remain ...