def fmask(self): '''-----\n This method applies Fmask algorithm or its equivalent with the BQA band (default for Ecopotential)''' os.chdir(self.ruta_escena) print('Starting Cloud Mask') try: print('Starting Fmask') t = time.time() #Last value is the cloud confidence value a = os.system( 'removethisisFmaskinstalled/usr/GERS/Fmask_4_0/application/run_Fmask_4_0.sh /usr/local/MATLAB/MATLAB_Runtime/v93 3 3 1 {}' .format(self.umbral)) a if a == 0: self.cloud_mask = 'Fmask' print('Cloud Mask (Fmask) generated in ' + str(t - time.time()) + ' seconds') else: print('Starting Cloud Mask with BQA band') outfile = os.path.join( os.path.join(self.ruta_escena, self.escena + '_Fmask.tif')) for i in os.listdir(self.ruta_escena): if i.endswith('BQA.TIF'): bqa = os.path.join(self.ruta_escena, i) masker = LandsatMasker(bqa, collection=1) conf = LandsatConfidence.high cloud = masker.get_cloud_mask(conf) cirrus = masker.get_cirrus_mask(conf) shadow = masker.get_cloud_shadow_mask(conf) MASCARA = cloud + cirrus + shadow masker.save_tif(MASCARA, outfile) self.cloud_mask = 'BQA' print('Cloud Mask (BQA) generated in ' + str(t - time.time()) + ' seconds') print('Calling get_water') self.get_water() except Exception as e: print("Unexpected error:", type(e), e)
def img_preprocess(qa, b4, b3, b2, tdimn): masker = LandsatMasker(qa, collection=1) conf = LandsatConfidence.high mask1 = masker.get_cirrus_mask(conf) mask2 = masker.get_cloud_mask(conf) mask = np.bitwise_or(mask1, mask2) mask = misc.imresize(mask, tdimn) #mask = misc.imrotate(mask,18,interp='nearest') img1 = gdal.Open(b4) orig1 = img1.ReadAsArray() orig1 = misc.imresize(orig1, tdimn) orig1 = orig1[:, :, np.newaxis] img2 = gdal.Open(b3) orig2 = img2.ReadAsArray() orig2 = misc.imresize(orig2, tdimn) orig2 = orig2[:, :, np.newaxis] img3 = gdal.Open(b2) orig3 = img3.ReadAsArray() orig3 = misc.imresize(orig3, tdimn) orig3 = orig3[:, :, np.newaxis] orig = np.concatenate((orig1, orig2, orig3), axis=2) #orig = misc.imrotate(orig,18,interp='nearest') #io.imsave(os.path.join(path,'rgb2.png'),orig) #img = io.imread(os.path.join(path,'rgb2.png')) # ============================================================================= # plt.figure(figsize=(40,20)) # plt.subplot(131) # plt.imshow(orig) # plt.title('Cloud-1') # # plt.figure(figsize=(40,20)) # plt.subplot(131) # plt.imshow(np.squeeze(orig1,axis=2),cmap = plt.get_cmap('gist_gray')) # plt.imshow(mask, cmap = plt.get_cmap('Reds'), alpha=0.5) # plt.title('Cloud-2') # ============================================================================= orig = np.reshape(orig, (1, mask.shape[0], mask.shape[1], 3)) img_m = np.reshape(mask, (1, mask.shape[0], mask.shape[1], 1)) return (orig, img_m)
def img_preprocess(qa,b4,b3,b2, pt): masker = LandsatMasker(qa,collection=1) conf = LandsatConfidence.high mask1 = masker.get_cirrus_mask(conf) mask2 = masker.get_cloud_mask(conf) mask = np.bitwise_or(mask1,mask2) mask = misc.imresize(mask,tdimn) img1 = gdal.Open(b4) orig1 = img1.ReadAsArray() orig1 = misc.imresize(orig1,tdimn) orig1 = orig1[:,:,np.newaxis] img2 = gdal.Open(b3) orig2 = img2.ReadAsArray() orig2 = misc.imresize(orig2,tdimn) orig2 = orig2[:,:,np.newaxis] img3 = gdal.Open(b2) orig3 = img3.ReadAsArray() orig3 = misc.imresize(orig3,tdimn) orig3 = orig3[:,:,np.newaxis] orig = np.concatenate((orig1,orig2,orig3),axis=2) # ============================================================================= # io.imsave(os.path.join(pt,'rgb2.png'),orig) # img_t = load_img(os.path.join(pt,'rgb2.png'),grayscale=True) # img_t = img_to_array(img_t) # ============================================================================= # ============================================================================= # plt.figure(figsize=(40,20)) # plt.subplot(131) # plt.imshow(orig,cmap = plt.get_cmap('gist_gray')) # plt.imshow(mask, cmap = plt.get_cmap('Reds'), alpha=0.3) # plt.title('Cloud-2') # plt.show() # ============================================================================= img_m = np.reshape(mask,(mask.shape[0],mask.shape[1],1)) #print(img_t.shape,img_m.shape) return (orig, img_m)
def img_preprocess(i, qa): masker = LandsatMasker(qa, collection=1) conf = LandsatConfidence.high mask1 = masker.get_cirrus_mask(conf) mask2 = masker.get_cloud_mask(conf) mask = np.bitwise_or(mask1, mask2) mask = misc.imresize(mask, tdimn) orig = misc.imread(os.path.join(dir1, "All_Imgs/orig_{}.png".format(i))) # ============================================================================= # plt.figure(figsize=(40,20)) # plt.subplot(131) # plt.imshow(orig) # plt.imshow(mask, cmap = plt.get_cmap('Reds'), alpha=0.3) # plt.title('Cloud-2') # plt.show() # ============================================================================= img_m = np.reshape(mask, (mask.shape[0], mask.shape[1], 1)) return (orig, img_m)
from skimage import io import tensorflow as tf from keras.models import Sequential from keras.layers import Conv2D, MaxPooling2D from keras.layers import Activation, Dropout, Flatten, Dense from keras import optimizers from keras.utils import to_categorical import os #%% path = os.path.dirname(__file__) tdimn = (256, 256) masker = LandsatMasker('LC08_L1TP_210017_20170711_20170711_01_RT_BQA.TIF', collection=1) conf = LandsatConfidence.high mask1 = masker.get_cirrus_mask(conf) mask2 = masker.get_cloud_mask(conf) mask = np.bitwise_or(mask1, mask2) mask = misc.imresize(mask, tdimn) mask = misc.imrotate(mask, 18, interp='nearest') #%% img1 = gdal.Open("LC08_L1TP_210017_20170711_20170711_01_RT_B4.TIF") orig1 = img1.ReadAsArray() orig1 = misc.imresize(orig1, tdimn) orig1 = orig1[:, :, np.newaxis] img2 = gdal.Open("LC08_L1TP_210017_20170711_20170711_01_RT_B3.TIF") orig2 = img2.ReadAsArray() orig2 = misc.imresize(orig2, tdimn) orig2 = orig2[:, :, np.newaxis]