Here's how:
result = cv2.matchTemplate(img,template,cv.CV_TM_SQDIFF)
(min_x,max_y,minloc,maxloc) = cv2.minMaxLoc(result)
(x,y) = minloc
result2 = np.reshape(result, result.shape[0]*result.shape[1])
sort = np.argsort(result2)
(y1, x1) = np.unravel_index(sort[0], result.shape)
(y2, x2) = np.unravel_index(sort[1], result.shape)
This is the fastest way, as the aforementioned sortings of all matches, even completely wrong ones. If performance is important to you, you can use the partsort bottleneck instead .
source
share