x_max = T_mult.shape[1] y_max = T_mult.shape[2] save = 1 #outall = "D:\\Juanma\\Seville 2014\\"+beam+"\\Change_detection\\" outall = root + beam + "\\Change_detection\\Consecutive_changes\\" application = "Difference change detection" # This is to initialise a class. Other options are: "Single image","ratio change detection". for img_2 in range(N - 1): date = dates[img_2] + ' - ' + dates[img_2 + 1] T11 = T_mult[ img_2, :, :, :, :] # img_2 can be = 0 in T11 to do change detection of stack wrt to first image. T22 = T_mult[img_2 + 1, :, :, :, :] #sar.visRGB_from_T(T_mult[img_2+1,:,:,1,1], T_mult[img_2+1,:,:,2,2], T_mult[img_2+1,:,:,0,0],"",factor=3.5,save=0,outall="") Tc = (T22 - T11) eigen = sar.eigendecompositions(application) List_RGB = eigen.gral_eigendecomposition( Tc) # to store eigendecomposition results in the class R_i, G_i, B_i = eigen.vis(eigen.L1_inc, eigen.L2_inc, eigen.L3_inc, add_or_remove='added') # added SMs R_d, G_d, B_d = eigen.vis(eigen.L1_dec, eigen.L2_dec, eigen.L3_dec, add_or_remove='removed') # removed SMs size = R_i.shape a = np.zeros([size[0], size[1], 3]) # Enable or disable denominator to enable or disable normalizing the colours. Makes green stronger. a[:, :, 0] = R_i #/np.nanmean(R_i) # To plot Added SMs
#folder = folders[i] """ ########################################################################################################################################################### # Read the covariance matrix for each resolution cell ############################ ########################################################################################################################################################### """ T = T_mult[i, :, :, :, :] #T=sar.array2D_of_coherency_matrices(folder,basis,in_format,ROI_size,header,datatype) # show RGB #sar.visRGB_from_T(T[:,:,1,1], T[:,:,2,2], T[:,:,0,0],"",factor=2.5) """ ########################################################################################################################################################### # return the eigenvalue and eigenvector decomposition. 2D array of image size, in which each position contains 3 eigenvelues, or 3x3 eigenvectors ######### ########################################################################################################################################################### """ L1, L2, L3, U_11, U_21, U_31, U_12, U_22, U_32, U_13, U_23, U_33 = sar.eigendecomposition_vectorized( T) """ ########################################################################################################################################################### # return the alpha, entropy, anisotropy and main scattering mechanishms images (Cloude-Pottier) ######### ########################################################################################################################################################### """ title1 = "" save = "" outall = "" alpha_avg, entropy, anisotropy, R_avg, G_avg, B_avg = sar.Alpha_Entropy_Anisotropy_decomp( L1, L2, L3, U_11, U_21, U_31,
for poly in range(len(Polygons_in_shp)): pol_names.append(Polygons_in_shp[poly]['properties']['Name']) print(pol_names) ################################################# repeat for every beam #beams=["FQ8W","FQ13W","FQ19W"] #"AllAll" #beams=["FQ8W"] #"AllAll" #dates2 = ['22May2014','15June2014','09July2014','02August2014','26August2014','19Sept2014'] #beams=["FQ13W"] #dates2 = ['22June2014','16July2014','09August2014','02Sept2014','26Sept2014'] beams = ["FQ19W"] dates2 = [ '05June2014', '29June2014', '23July2014', '16August2014', '09Sept2014' ] save = 1 application = "Difference change detection" eigen = sar.eigendecompositions(application) for t in range(len(beams)): beam = beams[t] ################################################# repeat for every polygon in the shp #for k in range(len(pol_names)): for k in range(1): #k=0 print("Parcel " + str(k + 1) + " of " + str(len(pol_names))) poly = k # Type polygon to process polygon_A = Polygons_in_shp[poly][ "geometry"] # filter the polygon from shp ################################################# Mask a polygon # use rasterio to mask and obtain the pixels inside the polygon. # Because the mask method of rasterio requires an interable object to work, we do: polygon_A_list = [polygon_A] # out_image in line below is a np array of the polygon desired
dates1 = [w.replace('Aug', 'August') for w in dates1] dates1 = [w.replace('Jul', 'July') for w in dates1] dates1 = [w.replace('Sep', 'September') for w in dates1] #################################### sort the dates import datetime sorted_dates = sorted(dates1, key=lambda x: datetime.datetime.strptime(x, '%d%B%Y')) dates2 = sorted_dates.copy() dates2 = [w.replace('June', 'Jun') for w in dates2] dates2 = [w.replace('August', 'Aug') for w in dates2] dates2 = [w.replace('July', 'Jul') for w in dates2] dates2 = [w.replace('September', 'Sep') for w in dates2] all_files = os.listdir(folder1) cols, rows, header = sar.read_config_file_snap(folder1 + "\\" + T11_master) datatype = 'float32' basis = "P" # Lexicographic (L) or Pauli (P) in_format = "img" # .Bin x_min = 0 y_min = 0 x_max = cols y_max = rows ROI_size = [y_min, y_max, x_min, x_max] stack_name = "T_stack" #"T_stack_asc" #path_to_save_stack = "D:\\Juanma\\Seville 2014\\"+beam+"\\"+stack_name path_to_RGB = "D:\\Juanma - Agrisar 2009\\My_results\\RGBs\\No_scale\\" path_to_SM = "D:\\Juanma - Agrisar 2009\\My_results\\SMs\\No_scale\\þ" #date = "2014-08-09" T_mult = np.zeros((len(dates), (x_max - x_min), (y_max - y_min), 3, 3),
dates1 = [w.replace('Jun', 'June') for w in dates1] # replace months name to names python understands dates1 = [w.replace('Aug', 'August') for w in dates1] dates1 = [w.replace('Jul', 'July') for w in dates1] dates1 = [w.replace('Sep', 'September') for w in dates1] #################################### sort the dates import datetime sorted_dates=sorted(dates1, key=lambda x: datetime.datetime.strptime(x, '%d%B%Y')) dates2=sorted_dates.copy() # replace back months name to be able to call the files in the folder dates2 = [w.replace('June', 'Jun') for w in dates2] dates2 = [w.replace('August', 'Aug') for w in dates2] dates2 = [w.replace('July', 'Jul') for w in dates2] dates2 = [w.replace('September', 'Sep') for w in dates2] all_files=os.listdir(folder1) x_max,y_max,header=sar.read_config_file_snap(folder1 +"\\"+T11_master) #T_mult=np.zeros((len(dates2),x_max,y_max,3,3)) N=len(dates2) datatype = 'float32' basis="P" # Lexicographic (L) or Pauli (P) in_format="img" # Bin or img # instantiate class to perform eigendecompositions indicating that we will use a covariance matrix correspondint to difference of two images application = "Difference change detection" #eigen = sar.eigendecompositions(application) #create dataframe with same info df=pd.DataFrame(index=np.arange(1,len(pol_names)+1),columns=['IDENT','CROP_TYPE','AREA_HA']) df['IDENT']=pol_names df['CROP_TYPE']=pol_crops
dates2 = [w.replace('June', 'Jun') for w in dates2] dates2 = [w.replace('August', 'Aug') for w in dates2] dates2 = [w.replace('July', 'Jul') for w in dates2] dates2 = [w.replace('September', 'Sep') for w in dates2] #for k in range(len(dates2)): # a=[] # for file in os.listdir(folder1): # if file.endswith(".img"): # if dates[k] in file: # a.append(file) # T11,T22,T33,T12,T13,T23=read_Img_components_from_stack_SNAP(basis,folder1,a) # sar.visRGB_from_T(T22, T33, T11,"",factor=5) all_files = os.listdir(folder1) x_max, y_max, header = sar.read_config_file_snap(folder1 + "\\" + T11_master) #T_mult=np.zeros((len(dates2),x_max,y_max,3,3)) N = len(dates2) # Empty array to save outputs of multitemporal change detection R_avg = np.zeros([N, N, x_max, y_max]) G_avg = np.zeros([N, N, x_max, y_max]) B_avg = np.zeros([N, N, x_max, y_max]) datatype = 'float32' basis = "P" # Lexicographic (L) or Pauli (P) in_format = "img" # Bin or img # Multitemporal change detection of the cropped region # First row of change matrix, Open the image 1 (i) and do change detection with respecto to all other images in the stack # Second row: Open next image (i+1) in the stack and do change detection # Continue for the n images in the stack
dates2 = [w.replace('June', 'Jun') for w in dates2] dates2 = [w.replace('August', 'Aug') for w in dates2] dates2 = [w.replace('July', 'Jul') for w in dates2] dates2 = [w.replace('September', 'Sep') for w in dates2] #for k in range(len(dates2)): # a=[] # for file in os.listdir(folder1): # if file.endswith(".img"): # if dates[k] in file: # a.append(file) # T11,T22,T33,T12,T13,T23=read_Img_components_from_stack_SNAP(basis,folder1,a) # sar.visRGB_from_T(T22, T33, T11,"",factor=5) all_files=os.listdir(folder1) x_max,y_max,header=sar.read_config_file_snap(folder1 +"\\"+T11_master) T_mult=np.zeros((len(dates2),x_max,y_max,3,3),dtype=complex) basis = "P" for i in range(len(dates2)): a=[] for file in os.listdir(folder1): if file.endswith(".img"): if dates[i] in file: a.append(file) print(a) #folder=folders[i] #x_max,y_max,header=sar.read_config_file_snap(folder+"T11") basis="P" # Lexicographic (L) or Pauli (P) in_format="img" # Bin or img
] ##############################################'FQ13W' #beam = 'FQ13W' #dates=["2014-06-22","2014-07-16","2014-08-09","2014-09-02","2014-09-26"] ##############################################'FQ13W' #beam = 'FQ19W' #dates=["2014-06-05","2014-06-29","2014-07-23","2014-08-16","2014-09-09"] folder = "D:\\Juanma\\Seville 2014\\" + beam + "\\" + dates[ 0] + ".rds2\\" #files[img] # path of the image stack_name = "T_stack" #"T_stack_asc" path_to_save_stack = "D:\\Juanma\\Seville 2014\\" + beam + "\\" + stack_name path_to_RGB = "D:\\Juanma\\Seville 2014\\" + beam + "\\RGBs\\No_scale\\" path_to_SM = "D:\\Juanma\\Seville 2014\\" + beam + "\\SMs\\No_scale\\" cols, rows, header = sar.read_config_file(folder) datatype = 'float32' basis = "L" # Lexicographic (L) or Pauli (P) in_format = "Bin" # .Bin x_min = 2000 y_min = 2000 x_max = cols - 500 y_max = rows - 500 ROI_size = [y_min, y_max, x_min, x_max] #date = "2014-08-09" T_mult = np.zeros((len(dates), (x_max - x_min), (y_max - y_min), 3, 3), dtype=complex) save_RGBs = 1 N = len(dates) for i in range(N):
dates1 = [w.replace('Jul', 'July') for w in dates1] dates1 = [w.replace('Sep', 'September') for w in dates1] #################################### sort the dates import datetime sorted_dates = sorted(dates1, key=lambda x: datetime.datetime.strptime(x, '%d%B%Y')) dates2 = sorted_dates.copy( ) # replace back months name to be able to call the files in the folder dates2 = [w.replace('June', 'Jun') for w in dates2] dates2 = [w.replace('August', 'Aug') for w in dates2] dates2 = [w.replace('July', 'Jul') for w in dates2] dates2 = [w.replace('September', 'Sep') for w in dates2] all_files = os.listdir(folder1) x_max, y_max, header = sar.read_config_file_snap(folder1 + "\\" + T11_master) #T_mult=np.zeros((len(dates2),x_max,y_max,3,3)) N = len(dates2) # Empty array to save outputs of multitemporal change detection R_avg = np.zeros([N, N, x_max, y_max]) G_avg = np.zeros([N, N, x_max, y_max]) B_avg = np.zeros([N, N, x_max, y_max]) datatype = 'float32' basis = "P" # Lexicographic (L) or Pauli (P) in_format = "img" # Bin or img # Multitemporal change detection of the cropped region # First row of change matrix, Open the image 1 (i) and do change detection with respecto to all other images in the stack # Second row: Open next image (i+1) in the stack and do change detection # Continue for the n images in the stack
shp_name = "AgriSAR2009.shp" T_mult_path = "X:\\crs2\\Paper2_Agrisar\\T_Stack\\" T_mult_name = "T_stack.npy" """ ################################################################################################################################################### Loading datacube of array_2D_of_coherency matrices ########################################################################################################################################################### """ print("Loading datacube...") T_mult = np.load(T_mult_path + T_mult_name) """ ################################################################################################################################################### read_raster_and_shp ################################################################################################################################################### """ raster, Polygons_in_shp = sar.read_raster_and_shp(tiff_path, tiff_name, shp_path, shp_name) """ ################################################################################################################################################### Crop T_mult to polygon size ################################################################################################################################################### """ #crop_types_IDs=['L-17','P-06','MP-31','B-02','CF-01','CL-03','FL-26','CY-15','O-35','SF-24','FMIX-01','G-05','D-32','W-19'] # list of crops pol_names = [] pol_crops = [] for poly in range(len(Polygons_in_shp)): pol_names.append(Polygons_in_shp[poly]['properties']['IDENT']) pol_crops.append(Polygons_in_shp[poly]['properties']['CROP_TYPE']) #print(pol_names) # initializing lists