Monday, 15 September 2014

opencv python MatchTemplate function with multiple matches -



opencv python MatchTemplate function with multiple matches -

i'am trying find little image in big image , used matchtemplate()

img = cv2.imread("c:\picture.jpg") template = cv2.imread("c:\template.jpg") result = cv2.matchtemplate(img,template,cv2.tm_ccoeff_normed) y,x = np.unravel_index(result.argmax(), result.shape)

works fine coords of top left corner, it's 1 point. if have multiple matches on big picture, how can of them ?

here's how:

result = cv2.matchtemplate(img,template,cv.cv_tm_sqdiff) #the best match fast utilize this: (min_x,max_y,minloc,maxloc) = cv2.minmaxloc(result) (x,y) = minloc #get matches: result2 = np.reshape(result, result.shape[0]*result.shape[1]) sort = np.argsort(result2) (y1, x1) = np.unravel_index(sort[0], result.shape) #best match (y2, x2) = np.unravel_index(sort[1], result.shape) #second best match

this note fastest way above sorts matches, totally wrong ones. if performance matters you, can utilize bottleneck's partsort function instead.

python opencv

No comments:

Post a Comment