fix bug for templatematch kernel, getting right template match and sorting