def find_upper_img(uid,fieldid,all_coords_dict): import cv2 import numpy as np from matplotlib import pyplot as plt large = uid+'_TCI_large.png' print('upper img coords') small = uid+fieldid+'_static_map_large.png' #all_coords_dict = {} coords_dict = {} #try: img = cv2.imread(large,0) img2 = img.copy() template = cv2.imread(small,0) w, h = template.shape[::-1] orig_w = w template = template[:round(h/2),:w] w, h = template.shape[::-1] cv2.imwrite('upper_half.png',template) print(h) # All the 6 methods for comparison in a list # All the 6 methods for comparison in a list methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR','cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED'] ww =0 for meth in methods: img = img2.copy() method = eval(meth) # Apply template Matching res = cv2.matchTemplate(img,template,method) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]: top_left = min_loc else: top_left = max_loc bottom_right = (top_left[0] + w, top_left[1] + h) ystart = top_left[0] yend = top_left[0]+orig_w xstart = top_left[1] xend = top_left[1]+orig_w coords_dict = {} coords_dict['xstart'] =xstart coords_dict['xend'] =xend coords_dict['ystart'] =ystart coords_dict['yend'] =yend try: temp_coords_dict = all_coords_dict[str(xstart)] coord_count = int(temp_coords_dict['count']) #print(coord_count) coord_count = coord_count+1 coords_dict['count']=coord_count except: coords_dict['count'] = 1 all_coords_dict[str(xstart)]=coords_dict print(all_coords_dict) return all_coords_dict