import cv2
import glob, os.path
import gzip
import numpy as np
import caffe
caffe.set_mode_cpu() # running on gpu mode
TMP_PATH = "./TMP/" # temp folder where landsat bands will be extracted
model_def = './deepwatermap3_deploy.prototxt' # model description file
model_weights = './deepwatermap3.caffemodel' # trained model file './rivanet_iter_460351.caffemodel'
d = './p001r062_WC_20010731' # path to the directory where landsat bands are stored
def im2double(I):
""" Converts image datatype to float """
if I.dtype == 'uint8':
I = I.astype('float')/255
if I.dtype == 'uint16':
I = I.astype('float')/65535
return I
def mndwi(green, mir):
green = im2double(green)
mir = im2double(mir)
numerator = green-mir
denominator = green+mir
numerator[denominator==0] = 0
denominator[denominator==0] = 1
mndwi = numerator / denominator
return mndwi
MU = np.asarray([69, 57, 59, 59, 64, 46], dtype='float32')
bands = ("_10.tif.gz", "_20.tif.gz", "_30.tif.gz", "_40.tif.gz", "_50.tif.gz", "_70.tif.gz")
I_all = None
# add bands
for i in range(0, len(bands)):
input_band_path = glob.glob(d + '/*' + bands[i])[0]
# extract the landsat archives into a temporary folder
temp_dir = TMP_PATH
if not os.path.exists(temp_dir):
os.makedirs(temp_dir)
temp_im_dir = os.path.join(temp_dir, os.path.basename(input_band_path)[:-3])
with gzip.open(input_band_path, 'rb') as in_file:
s = in_file.read()
with open(temp_im_dir, 'w') as out_file:
out_file.write(s)
# open temp_im_dir, load it as a channel in I
I_band = cv2.imread(temp_im_dir, cv2.IMREAD_UNCHANGED)
if I_all is None:
R, C = I_band.shape
I_all = np.zeros((R, C, 6), 'uint8')
I_all[:,:,i] = I_band
# delete the temp file
os.remove(temp_im_dir)
其中 I_all[:,:,i] = I_band一直出错