def contour_images(xstart,xend,ystart,yend,latlen,longlen,centerlat,centerlong,coordinates,uid,fieldid,imagedate): import math import matplotlib.path from matplotlib.path import Path import numpy as np import cv2 from PIL import Image from merge_contour_images import merge_contour_images import collections coordinates = collections.OrderedDict(sorted(coordinates.items())) print(coordinates) path_codes = [] p1= len(coordinates) x_pixel_nos = np.zeros(p1) y_pixel_nos = np.zeros(p1) temp_list = [] half_lat_len = longlen/2 half_long_len = latlen/2 x_pixel_length = xend-xstart y_pixel_length = yend-ystart pixelsperlat = x_pixel_length/latlen pixelsperlong = y_pixel_length/longlen iter_path = 0 for k,v in coordinates.items(): temp_lat = v['Latitude'] temp_long = v['Longitude'] temp_lat = float(temp_lat) temp_long = float(temp_long) lat_distance_from_center = centerlat-temp_lat long_distance_from_center = centerlong-temp_long lat_distance_from_start_edge = half_lat_len-lat_distance_from_center long_distance_from_start_edge = half_long_len - long_distance_from_center temp_x = round(lat_distance_from_start_edge*pixelsperlat) temp_y = round(long_distance_from_start_edge*pixelsperlong) temp_list.append([temp_x,temp_y]) if iter_path == 0: path_codes.append(Path.MOVETO) elif (iter_path0): path_codes.append(Path.LINETO) else: path_codes.append(Path.CLOSEPOLY) iter_path = iter_path+1 polygon = np.array(temp_list) left = np.min(polygon,axis=0) right = np.max(polygon,axis=0) x = np.arange(math.ceil(left[0]),math.floor(right[0])+1) y = np.arange(math.ceil(left[1]),math.floor(right[1])+1) xv,yv = np.meshgrid(x,y,indexing='xy') points = np.hstack((xv.reshape((-1,1)),yv.reshape((-1,1)))) path = matplotlib.path.Path(polygon,path_codes) mask = path.contains_points(points) mask.shape = xv.shape print('mask') #print(mask) h_mask, w_mask = mask.shape mask_img = np.zeros([h_mask,w_mask]) for py in range(0,h_mask): for px in range(0,w_mask): if mask[py][px]==True: mask_img[py][px]=255 else: mask_img[py][px]=0 status = cv2.imwrite('mask_img.png',mask_img) mask_img = cv2.imread('mask_img.png',0) h_mask = int(1.2*h_mask) w_mask = int(1.2*w_mask) mask_img = cv2.resize(mask_img,(w_mask,h_mask)) M = cv2.getRotationMatrix2D((round(w_mask/2),round(h_mask/2)),90,1.0) rotated_mask = cv2.warpAffine(mask_img,M,(h_mask,w_mask)) status = cv2.imwrite('mask_img.png',rotated_mask) mask = rotated_mask merge_contour_images(uid,'ndvi',mask,half_lat_len,half_long_len,fieldid,imagedate) merge_contour_images(uid,'ndwi',mask,half_lat_len,half_long_len,fieldid,imagedate) merge_contour_images(uid,'ndre',mask,half_lat_len,half_long_len,fieldid,imagedate) merge_contour_images(uid,'vari',mask,half_lat_len,half_long_len,fieldid,imagedate) return