73 lines
1.9 KiB
Python
73 lines
1.9 KiB
Python
#!/usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
# \file dfc_sdaps.py
|
|
# \brief TODO
|
|
# \author Florent Guiotte <florent.guiotte@gmail.com>
|
|
# \version 0.1
|
|
# \date 27 août 2018
|
|
#
|
|
# TODO details
|
|
|
|
import numpy as np
|
|
import ld2dap
|
|
|
|
def run(rasters, treshold=1e4, areas=None, sd=None, moi=None, split=1, split_dim=0):
|
|
"""DFC Self Dual Attribute Profiles
|
|
|
|
Compute description vectors for parameters. Rasters can be splitted along
|
|
`split_dim` before description proceeds.
|
|
|
|
"""
|
|
|
|
# Parse attribute type
|
|
treshold = float(treshold)
|
|
areas = None if areas is None else np.array(areas).astype(np.float).astype(np.int)
|
|
sd = None if sd is None else np.array(sd).astype(np.float)
|
|
moi = None if moi is None else np.array(moi).astype(np.float)
|
|
|
|
# Load and filter
|
|
loader = ld2dap.LoadTIFF(rasters)
|
|
dfc_filter = ld2dap.Treshold(treshold)
|
|
normalize = ld2dap.Normalize(dtype=np.uint8)
|
|
raw_out = ld2dap.RawOutput()
|
|
|
|
raw_out.input = normalize
|
|
normalize.input = dfc_filter
|
|
dfc_filter.input = loader
|
|
raw_out.run()
|
|
|
|
# Split
|
|
n = split; d = split_dim
|
|
|
|
step = int(raw_out.data.shape[d] / n)
|
|
view = np.moveaxis(raw_out.data, d, 0)
|
|
cuts = list()
|
|
for i in range(n):
|
|
cut = np.moveaxis(view[i*step:(i+1)*step+1], 0, d)
|
|
cuts.append(cut)
|
|
|
|
# Describe
|
|
dcuts = list()
|
|
for cut in cuts:
|
|
rinp = ld2dap.RawInput(cut, raw_out.metadata)
|
|
aps = ld2dap.SelfDualAttributeProfiles(areas, sd, moi, normalize_to_dtype=False)
|
|
vout = ld2dap.RawOutput()
|
|
|
|
vout.input = aps
|
|
aps.input = rinp
|
|
vout.run()
|
|
|
|
dcuts.append(vout.data)
|
|
|
|
# Merge
|
|
descriptors = np.zeros(raw_out.data.shape[:2] + (dcuts[0].shape[-1],))
|
|
view = np.moveaxis(descriptors, d, 0)
|
|
|
|
for i, cut in enumerate(dcuts):
|
|
view[i*step:(i+1)*step+1] = np.moveaxis(cut, 0, d)
|
|
|
|
return descriptors
|
|
|
|
def version():
|
|
return 'v0.0'
|