/ inputs_processing / create_npys_from_levir copy.py
create_npys_from_levir copy.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  import glob
  7  
  8  
  9  #### Images locations  ####
 10  IMAGES_PATH = '/home/dvalsamis/Documents/data/LEVIR-CD/original_set/total/'
 11  TRAIN_LABEL_PATH = '/home/dvalsamis/Documents/data/LEVIR-CD/original_set/train/label/'
 12  TEST_LABEL_PATH = '/home/dvalsamis/Documents/data/LEVIR-CD/original_set/test/label/'
 13  
 14  #### Save targets for the npy files ####
 15  SAVE_IMAGES = '/home/dvalsamis/Documents/data/LEVIR-CD/Levir_NPY/total_NPY/'
 16  SAVE_TRAIN_LABELS = '/home/dvalsamis/Documents/data/LEVIR-CD/Levir_NPY/train_labels_NPY/'
 17  SAVE_TEST_LABELS = '/home/dvalsamis/Documents/data/LEVIR-CD/Levir_NPY/test_labels_NPY/'
 18  
 19  images_set = pd.DataFrame(columns=['pair1','pair2','name'])
 20  train_set = pd.DataFrame(columns=['pair1','pair2','change_mask'])
 21  test_set = pd.DataFrame(columns=['pair1','pair2','change_mask'])
 22  
 23  #Method to save the change masks to npy files
 24  def save_labels(path,folder,prefix, target, index, train=False, test=False):
 25      cm = read_changemask(path+folder)
 26      if train:
 27          train_set.loc[index,'pair1'] = str(prefix)+"_a.npy"
 28          train_set.loc[index,'pair2'] = str(prefix)+"_b.npy"
 29          train_set.loc[index,'change_mask'] = str(prefix)+"_cm.npy"
 30      if test:
 31          test_set.loc[index,'pair1'] = str(prefix)+"_a.npy"
 32          test_set.loc[index,'pair2'] = str(prefix)+"_b.npy"
 33          test_set.loc[index,'change_mask'] = str(prefix)+"_cm.npy"
 34      np.save(target + str(prefix)+"_cm.npy",cm)
 35  
 36  
 37  
 38  
 39  # final method that saves the images in a npy format
 40  def save_image(path_to_images, prefix, suffix, path_to_target):
 41      """Save the image in npy format."""
 42      image_to_save = read_rgb_img(path_to_images)  # Use the modified function to read PNG
 43      np.save(os.path.join(path_to_target, f"{prefix}_{suffix}.npy"), image_to_save)
 44      return image_to_save
 45  
 46      
 47  #Method to read the RGB bands of a given image, used ONLY for the image pairs    
 48  def read_rgb_img(path):
 49      """Read RGB PNG image without normalization."""
 50      img_files = glob.glob(os.path.join(path, '*.png'))  # Adjust if naming convention differs
 51      if not img_files:
 52          raise FileNotFoundError(f"No image files found at {path}")
 53      
 54      img = io.imread(img_files[0])  # Reading the first image matching the pattern
 55      print("Image shape:", img.shape)
 56      return img
 57  
 58  
 59  def adjust_shape(I, s):
 60      """Adjust shape of grayscale image I to s."""
 61      # crop if necesary  
 62      I = I[:s[0],:s[1]]
 63      si = I.shape
 64  
 65      # pad if necessary 
 66      p0 = max(0,s[0] - si[0])
 67      p1 = max(0,s[1] - si[1])
 68  
 69      return np.pad(I,((0,p0),(0,p1)),'edge')
 70  
 71  
 72  def read_changemask(cm_path):
 73      """Read change mask from PNG files, convert values from [0, 255] to [0, 1]. Ensure the file exists."""
 74      cm_files = glob.glob(cm_path)  # Fetch all PNG files in directory
 75      if not cm_files:
 76          raise FileNotFoundError(f"No mask files found at {cm_path}")
 77      
 78      cm = io.imread(cm_files[0], as_gray=True)  # Safely read the first file, if it exists
 79      cm_normalized = (cm / 255.0).astype(int)  # Normalize to [0, 1] and convert to integer
 80      
 81      return cm_normalized
 82  
 83  
 84  # Method to make image pairs with the CBMI images 
 85  def make_image_pairs(loc):
 86      path1 = os.path.join(IMAGES_PATH, 'A')
 87      path2 = os.path.join(IMAGES_PATH, 'B')
 88      print("Path1: --->", path1)
 89      print("Path2: --->", path2)
 90  
 91      img1 = save_image(path1, loc, "a", SAVE_IMAGES)
 92      img2 = save_image(path2, loc, "b", SAVE_IMAGES)
 93      
 94      # Adjust shape handling to ensure consistency
 95      if img1.shape != img2.shape:
 96          print("correction on shapes")
 97          s1 = img1.shape
 98          s2 = img2.shape
 99          new_shape = (min(s1[0], s2[0]), min(s1[1], s2[1]))
100          img1 = img1[:new_shape[0], :new_shape[1]]
101          img2 = img2[:new_shape[0], :new_shape[1]]
102          np.save(os.path.join(SAVE_IMAGES, f"{loc}_a.npy"), img1)
103          np.save(os.path.join(SAVE_IMAGES, f"{loc}_b.npy"), img2)
104  
105  
106  path1 = os.path.join(IMAGES_PATH, 'A')
107  folder_list = [f for f in os.listdir(path1) if f.endswith('.txt')==False]
108  
109  
110  for i in range(10):
111      make_image_pairs(i)
112      images_set.loc[i,'name'] = str(folder_list[i])
113      images_set.loc[i,'pair1'] = str(i)+'_a.npy'
114      images_set.loc[i,'pair2'] = str(i)+'_b.npy'
115      
116  print("DONE with image pairs!")
117  print("Saving dataframe")    
118  images_set.to_csv(SAVE_IMAGES + 'LEVIR_set.csv', index=False)
119  
120  train_label_list = [f for f in os.listdir(TRAIN_LABEL_PATH ) if f.endswith('.txt')==False]
121      
122  pos=0    
123  for i in range(len(folder_list)):
124      for j in range(len(train_label_list)):
125          if folder_list[i] == train_label_list[j]:
126              save_labels(TRAIN_LABEL_PATH,train_label_list[j], i, SAVE_TRAIN_LABELS, pos, train=True)
127              pos+=1
128  print("DONE with train set!")
129  print("Saving dataframe")    
130  train_set.to_csv(SAVE_TRAIN_LABELS + 'train_set.csv', index=False)
131              
132  test_label_list = os.listdir(TEST_LABEL_PATH)
133  
134  pos = 0
135  for i in range(len(folder_list)):
136      for j in range(len(test_label_list)):
137          if folder_list[i] == test_label_list[j]:
138              save_labels(TEST_LABEL_PATH,test_label_list[j], i, SAVE_TEST_LABELS, pos, train=False, test=True)
139              pos += 1
140  
141  print("DONE with test set!")
142  print("Saving dataframe")    
143  test_set.to_csv(SAVE_TEST_LABELS + 'test_set.csv', index=False)
144  
145  print("DONE")