/ inputs_processing / create_npys_from_onera.py
create_npys_from_onera.py
  1  import os
  2  import numpy as np
  3  from skimage import io
  4  import pandas as pd
  5  from scipy.ndimage import zoom
  6  
  7  #### Images locations  ####
  8  IMAGES_PATH = '/home/dvalsamis/Documents/data/CBMI/CBMI_0.3/CBMI_0.3/Dataset/CBMI_Images/'
  9  TRAIN_LABEL_PATH = '/home/dvalsamis/Documents/data/CBMI/CBMI_0.3/CBMI_0.3/Dataset/CBMI_Train_Labels/'
 10  TEST_LABEL_PATH = '/home/dvalsamis/Documents/data/CBMI/CBMI_0.3/CBMI_0.3/Dataset/CBMI_Test_Labels/'
 11  
 12  #### Save targets for the npy files ####
 13  SAVE_IMAGES = '/home/dvalsamis/Documents/data/CBMI/CBMI_0.3/CBMI_0.3/NPY_dataset/CBMI_Images_NPY/'
 14  SAVE_TRAIN_LABELS = '/home/dvalsamis/Documents/data/CBMI/CBMI_0.3/CBMI_0.3/NPY_dataset/CBMI_Train_Labels_NPY/'
 15  SAVE_TEST_LABELS = '/home/dvalsamis/Documents/data/CBMI/CBMI_0.3/CBMI_0.3/NPY_dataset/CBMI_Test_Labels_NPY/'
 16  
 17  images_set = pd.DataFrame(columns=['pair1','pair2','name'])
 18  train_set = pd.DataFrame(columns=['pair1','pair2','change_mask'])
 19  test_set = pd.DataFrame(columns=['pair1','pair2','change_mask'])
 20  
 21  
 22  #Method to save the change masks to npy files
 23  def save_labels(path,folder,prefix, target, index, train=False, test=False):
 24      cm = read_changemask(path+folder)
 25      if train:
 26          train_set.loc[index,'pair1'] = str(prefix)+"_a.npy"
 27          train_set.loc[index,'pair2'] = str(prefix)+"_b.npy"
 28          train_set.loc[index,'change_mask'] = str(prefix)+"_cm.npy"
 29      if test:
 30          test_set.loc[index,'pair1'] = str(prefix)+"_a.npy"
 31          test_set.loc[index,'pair2'] = str(prefix)+"_b.npy"
 32          test_set.loc[index,'change_mask'] = str(prefix)+"_cm.npy"
 33      np.save(target + str(prefix)+"_cm.npy",cm)
 34  
 35  # final method that saves the images in a npy format
 36  def save_image(path_to_images, prefix, suffix, path_to_target):
 37      image_to_save = read_sentinel_img_eq20(path_to_images)
 38      np.save(path_to_target + str(prefix) +"_"+ str(suffix) + ".npy", image_to_save)
 39      
 40      return image_to_save
 41      
 42  #Method to read the RGB bands of a given image, used ONLY for the image pairs    
 43  def read_sentinel_img(path):
 44      """Read cropped Sentinel-2 image: RGB bands."""
 45      im_name = os.listdir(path)[0][:-7]
 46  
 47      r = io.imread(path + im_name + "B04.tif")
 48      g = io.imread(path + im_name + "B03.tif")
 49      b = io.imread(path + im_name + "B02.tif")
 50      vnir = io.imread(path + im_name + "B08.tif")
 51  
 52      I = np.stack((r,g,b,vnir),axis=2).astype('float')
 53      print("RGB image shape:", I.shape)
 54  
 55      return I
 56  
 57  def adjust_shape(I, s):
 58      """Adjust shape of grayscale image I to s."""
 59      # crop if necesary  
 60      I = I[:s[0],:s[1]]
 61      si = I.shape
 62  
 63      # pad if necessary 
 64      p0 = max(0,s[0] - si[0])
 65      p1 = max(0,s[1] - si[1])
 66  
 67      return np.pad(I,((0,p0),(0,p1)),'edge')
 68  
 69  
 70  def read_sentinel_img_eq20(path):
 71      """Read cropped Sentinel-2 image: bands with resolution less than or equals to 20m."""
 72      im_name = os.listdir(path)[0][:-7]
 73      print(im_name)
 74  
 75      r = io.imread(path+'/' + im_name + "B04.tif")
 76      s = r.shape
 77  
 78      ir1 = adjust_shape(zoom(io.imread(path +'/'+ im_name + "B05.tif"),2),s)
 79      ir2 = adjust_shape(zoom(io.imread(path +'/'+ im_name + "B06.tif"),2),s)
 80      ir3 = adjust_shape(zoom(io.imread(path +'/'+ im_name + "B07.tif"),2),s)
 81      nir2 = adjust_shape(zoom(io.imread(path +'/'+ im_name + "B8A.tif"),2),s)
 82      swir2 = adjust_shape(zoom(io.imread(path +'/'+ im_name + "B11.tif"),2),s)
 83      swir3 = adjust_shape(zoom(io.imread(path +'/'+ im_name + "B12.tif"),2),s)
 84  
 85      I = np.stack((ir1,ir2,ir3,nir2,swir2,swir3),axis=2).astype('float')    
 86  
 87      return I
 88  
 89  def read_changemask(cm_path):
 90      cm = io.imread(cm_path + '/cm/cm.tif', as_gray=True) != 0
 91      return cm
 92  
 93  # Method to make image pairs with the CBMI images 
 94  def make_image_pairs(folder, loc):
 95      path1 = os.path.join(IMAGES_PATH, folder, 'img1_cropped')
 96      path2 = os.path.join(IMAGES_PATH, folder, 'img2_cropped')
 97      print("Path1: --->", path1)
 98      print("Path2: --->", path2)
 99      
100      img1 = save_image(path1, loc, "a", SAVE_IMAGES)
101      img2 = save_image(path2, loc, "b", SAVE_IMAGES)
102      
103      if img1.shape != img2.shape:
104          print("correction on shapes")
105          s1 = img1.shape
106          s2 = img2.shape
107          
108          # Calculate the padding values to make the shapes equal
109          pad_height = max(0, s1[0] - s2[0])
110          pad_width = max(0, s1[1] - s2[1])
111          
112          # Pad img2 to match the shape of img1
113          img2 = np.pad(img2, ((0, pad_height), (0, pad_width), (0, 0)), 'edge')
114          print('Img2 after correction:', img2.shape)
115          
116          # Save the corrected img2
117          np.save(os.path.join(SAVE_IMAGES, f"{loc}_b.npy"), img2)
118  
119     
120  folder_list = [f for f in os.listdir(IMAGES_PATH) if f.endswith('.txt')==False]
121  
122  
123  for i in range(len(folder_list)):
124      make_image_pairs(folder_list[i], i)
125      images_set.loc[i,'name'] = str(folder_list[i])
126      images_set.loc[i,'pair1'] = str(i)+'_a.npy'
127      images_set.loc[i,'pair2'] = str(i)+'_b.npy'
128      
129  print("DONE with image pairs!")
130  print("Saving dataframe")    
131  images_set.to_csv(SAVE_IMAGES + 'CBMI_set.csv', index=False)
132  
133  train_label_list = [f for f in os.listdir(TRAIN_LABEL_PATH ) if f.endswith('.txt')==False]
134      
135  pos=0    
136  for i in range(len(folder_list)):
137      for j in range(len(train_label_list)):
138          if folder_list[i] == train_label_list[j]:
139              save_labels(TRAIN_LABEL_PATH,train_label_list[j], i, SAVE_TRAIN_LABELS, pos, train=True)
140              pos+=1
141  print("DONE with train set!")
142  print("Saving dataframe")    
143  train_set.to_csv(SAVE_TRAIN_LABELS + 'train_set.csv', index=False)
144              
145  test_label_list = os.listdir(TEST_LABEL_PATH)
146  
147  pos = 0
148  for i in range(len(folder_list)):
149      for j in range(len(test_label_list)):
150          if folder_list[i] == test_label_list[j]:
151              save_labels(TEST_LABEL_PATH,test_label_list[j], i, SAVE_TEST_LABELS, pos, train=False, test=True)
152              pos += 1
153  
154  print("DONE with test set!")
155  print("Saving dataframe")    
156  test_set.to_csv(SAVE_TEST_LABELS + 'test_set.csv', index=False)
157  
158  print("DONE")