I am working on a project that uses matchTemplate and I need sub pixel accuracy and I figure I could share the resulting code.
I've found others asking the same question, but the direction I am going is different since a general curve fit will not work since the values past the top few are not stable. So the plan is to scan out and find the smallest drop and then use a tilt formula and then limit it to what the peak values would allow.
Another problem is that if the template has a lot of fine detail the result will also not be stable. There are two options; option one: upsize both the template and the source image by two before match template. option two: shift the template by .5 pixel and weave those results into every other row/col of the heat map.
given the different methods I am wondering if something like this would be good.
minMaxLocSubPix in Mat = heat map. also gives function size of source. in point = x,y point from minMaxLoc or any other point you want to explore in Polarity = Let this function know if we want large or small values in Method = what algorithm to use since mine won't be the best for every application. out Float x,y returns float error value if math result is limited to prevent moving more than one pixel.
See also this post I found http://answers.opencv.org/question/5584/find-max-with-sub-accuracy/