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)
def get_cloud_mask(bqa): ''' Takes in a bqa array and returns a mask of clouds ''' landsat_confidences = { LandsatConfidence.low: 1, LandsatConfidence.medium: 2, LandsatConfidence.high: 3 } bqa = bqa.astype(np.int16) conf_is_cumulative = False masker = LandsatMasker(bqa, collection=0) cloud_conf = np.zeros_like(bqa).astype(np.int16) for conf in landsat_confidences: mask = masker.get_cloud_mask(conf, cumulative=conf_is_cumulative) cloud_conf += mask * landsat_confidences[ conf] # multiply each conf by a value return cloud_conf
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]
output_shadow_mask_path = "{0}/{1}/{1}_bqa_shadow_mask.TIF".format( landsat_scenes_path, sceneID) output_cloudshadow_mask_path = "{0}/{1}/{1}_bqa_cloudshadow_mask.TIF".format( landsat_scenes_path, sceneID) # load the QA band directly # # The "collection" parameter is required for landsat to specify the collection # number. Acceptable number: 0 (pre-collection), 1 (collection-1) masker = LandsatMasker(bqa_band_path, collection=1) # algorithm has high confidence that this condition exists # (67-100 percent confidence) conf = LandsatConfidence.medium # Get mask indicating cloud pixels with high confidence cloud_mask = masker.get_cloud_mask(conf, cumulative=True) # save the result masker.save_tif(cloud_mask, output_cloud_mask_path) if shadow: # Get mask indicating cloud pixels with high confidence cloud_shadow_mask = masker.get_cloud_shadow_mask(conf, cumulative=True) masker.save_tif(cloud_shadow_mask, output_shadow_mask_path) # Merge the Cloud and Cloud-Shadow mask gdal_calc_string = "gdal_calc.py -A {} -B {} --outfile={} --calc='logical_or(A==1,B==1)'".format( output_cloud_mask_path, output_shadow_mask_path, output_cloudshadow_mask_path) os.system(gdal_calc_string) # Done