def find_img(uid,fieldid,new_string_json): from oct2py import octave import cv2 import numpy as np from matplotlib import pyplot as plt from find_upper_img import find_upper_img from find_bottom_img import find_bottom_img from find_left_img import find_left_img from find_right_img import find_right_img from find_img import find_img large1 = uid+'_TCI.png' large2 = uid+'_ETCI.png' #large3 = 'BNDVI_clipped.png' #large4 = 'B3_clipped.png' #large5 = 'B4_clipped.png' large6 = 'B8_clipped.png' larges = [large1, large2] small = uid+fieldid+'_static_map.png' all_coords_dict = {} coords_dict = {} template = cv2.imread(small,0) #cv2.imwrite('bwt.png',template) #t2 = cv2.imread(small,0) w,h = template.shape[::-1] # All the 6 methods for comparison in a list methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED','cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED'] ww =0 meth_count =0 meth_match=0 for l in larges: img = cv2.imread(l,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]+w xstart = top_left[1] xend = top_left[1]+h if meth_count==1: meth1_xs = xstart print('xvalues') print(xstart) print(xend) print(ystart) print(yend) for (key,val) in all_coords_dict.items(): xs = key #print('xsys') #print(xs) dict_coord = all_coords_dict[xs] ys = dict_coord["ystart"] #print(ys) if abs(int(xs)-xstart)<15 and abs(int(ys)-ystart)<15: xstart = xs ystart = ys if meth_count==2: if int(xstart)==int(meth1_xs): meth_match=1 print(('mm'+str(meth_match))) break coords_dict = {} coords_dict['xstart'] =xstart coords_dict['xend'] =xend coords_dict['ystart'] =ystart coords_dict['yend'] =yend meth_count = meth_count+1 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) temp_count = 0 print(all_coords_dict) max_cur_count = 0 for cur_key in all_coords_dict: cur_count = int(all_coords_dict[cur_key]['count']) if cur_count>max_cur_count: max_cur_count = cur_count if cur_count>temp_count: true_coords = all_coords_dict[cur_key] temp_count = cur_count # print('max_cur') # print(max_cur_count) print(all_coords_dict) print(meth_match) if max_cur_count>2: for cur_key in all_coords_dict: cur_count = int(all_coords_dict[cur_key]['count']) if cur_count>max_cur_count: max_cur_count = cur_count if cur_count>temp_count: true_coords = all_coords_dict[cur_key] temp_count = cur_count print('max_cur') print(max_cur_count) xstart = int(true_coords['xstart']) xend = int(true_coords['xend']) ystart = int(true_coords['ystart']) yend = int(true_coords['yend']) print('xvalues_true_large') print(xstart) print(xend) print(ystart) print(yend) aqw2 = 1 #aqw1 = octave.crop_images_large(uid,xstart,xend,ystart,yend,new_string_json) #aqw2 = octave.crop_images(uid,xstart,xend,ystart,yend,new_string_json) return aqw2,xstart,xend,ystart,yend elif max_cur_count<3 and meth_match==1: for cur_key in all_coords_dict: cur_count = int(all_coords_dict[cur_key]['count']) if cur_count>max_cur_count: max_cur_count = cur_count if cur_count>temp_count: true_coords = all_coords_dict[cur_key] temp_count = cur_count print('max_cur') print(max_cur_count) xstart = int(true_coords['xstart']) xend = int(true_coords['xend']) ystart = int(true_coords['ystart']) yend = int(true_coords['yend']) print('xvalues_true_large') print(xstart) print(xend) print(ystart) print(yend) aqw2=1 return aqw2,xstart,xend,ystart,yend else: #xstart,xend,ystart,yend = find_img(uid,fieldid) print('xvalues_true_small') #print(xstart) #print(xend) #print(ystart) #print(yend) #aqw1 = octave.crop_images_large(uid,xstart,xend,ystart,yend,new_string_json) #aqw2 = octave.crop_images_small_gmap(uid,xstart,xend,ystart,yend,new_string_json) return 2,0,0,0,0 #except: # return 0,0,0,0