Skip to content

Commit

Permalink
Updated filter to allow window_size definition
Browse files Browse the repository at this point in the history
  • Loading branch information
sambowers committed Oct 23, 2018
1 parent 7347301 commit 9878d91
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
7 changes: 5 additions & 2 deletions biota/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class LoadTile(object):
mask:
"""

def __init__(self, data_dir, lat, lon, year, forest_threshold = 10., area_threshold = 0., downsample_factor = 1, lee_filter = False, sm_dir = os.getcwd(), output_dir = os.getcwd()):
def __init__(self, data_dir, lat, lon, year, forest_threshold = 10., area_threshold = 0., downsample_factor = 1, lee_filter = False, window_size = 5, sm_dir = os.getcwd(), output_dir = os.getcwd()):
"""
Loads data and metadata for an ALOS mosaic tile.
"""
Expand All @@ -52,6 +52,8 @@ def __init__(self, data_dir, lat, lon, year, forest_threshold = 10., area_thresh
assert (year >= 2007 and year <= 2010) or (year >= 2015 and year <= dt.datetime.now().year), "Years must be in the range 2007 - 2010 and 2015 - present. Your input year was %s."%str(year)
assert downsample_factor >= 1 and type(downsample_factor) == int, "Downsampling factor must be an integer greater than 1."
assert type(lee_filter) == bool, "Option lee_filter must be set to 'True' or 'False'."
assert type(window_size) == int, "Option window_size must be an integer."
assert window_size % 2 == 1, "Option window_size must be an odd integer."
assert os.path.isdir(os.path.expanduser(data_dir)), "Specified data directory (%s) does not exist"%str(data_dir)
assert os.path.isdir(os.path.expanduser(sm_dir)), "Specified soil moisture directory (%s) does not exist"%str(sm_dir)
assert os.path.isdir(os.path.expanduser(output_dir)), "Specified output directory (%s) does not exist"%str(output_dir)
Expand All @@ -63,6 +65,7 @@ def __init__(self, data_dir, lat, lon, year, forest_threshold = 10., area_thresh
self.year = year
self.downsample_factor = downsample_factor
self.lee_filter = lee_filter
self.window_size = window_size
self.forest_threshold = forest_threshold
self.area_threshold = area_threshold

Expand Down Expand Up @@ -563,7 +566,7 @@ def getGamma0(self, polarisation = 'HV', units = 'natural', output = False, show

# Apply filter based on dB values
if self.lee_filter:
gamma0 = biota.filter.enhanced_lee_filter(gamma0, n_looks = self.nLooks)
gamma0 = biota.filter.enhanced_lee_filter(gamma0, n_looks = self.nLooks, window_size = self.window_size)


# Convert to natural units where specified
Expand Down
6 changes: 3 additions & 3 deletions biota/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def _window_stdev(img, window_size = 3):
variance[variance < 0] += 0.01 # Prevents divide by zero errors.

return np.sqrt(variance)[border:-border, border:-border]

# Damping factor, set to 1 which is adequate for most SAR images
k = 1
cu = (1./n_looks) ** 0.5
Expand All @@ -83,8 +83,8 @@ def _window_stdev(img, window_size = 3):
W[ci <= cu] = 1.
W[ci >= cmax] = 0.

s = np.logical_and(ci > cu, ci < cmax)
W[s] = np.exp((-k * (ci[s] - cu)) / (cmax - ci[s]))
sel = np.logical_and(ci > cu, ci < cmax)
W[sel] = np.exp((-k * (ci[sel] - cu)) / (cmax - ci[sel]))

img_filtered = (img_mean * W) + (data * (1. - W))

Expand Down

0 comments on commit 9878d91

Please sign in to comment.