Add normalization choice
This commit is contained in:
parent
bbec5fbb95
commit
7ec65323d9
@ -15,7 +15,7 @@ import numpy as np
|
||||
from .CreaTIFF import read, write
|
||||
|
||||
class Triskele:
|
||||
def __init__(self, raster, dtype=np.uint8, cache_dir='/tmp', verbose=True):
|
||||
def __init__(self, raster, dtype=np.uint8, normalize_to_dtype=True, cache_dir='/tmp', verbose=True):
|
||||
|
||||
self.verbose = verbose
|
||||
self.triskele_bin = Path(os.path.dirname(os.path.realpath(__file__))\
|
||||
@ -35,7 +35,7 @@ class Triskele:
|
||||
self.sdfile = self.cache_dir.joinpath('sdfile.txt')
|
||||
self.moifile = self.cache_dir.joinpath('moifile.txt')
|
||||
|
||||
self._write_infile(raster, dtype)
|
||||
self._write_infile(raster, dtype, normalize_to_dtype)
|
||||
|
||||
def filter(self, tree='max-tree', area=None, standard_deviation=None, moment_of_inertia=None, feature='weight'):
|
||||
self._setup(tree, area, standard_deviation, moment_of_inertia, feature)
|
||||
@ -45,20 +45,21 @@ class Triskele:
|
||||
def _read_outfile(self):
|
||||
return read(self.outfile)
|
||||
|
||||
def _write_infile(self, rasters, dtype):
|
||||
def _write_infile(self, rasters, dtype, normalize_to_dtype):
|
||||
## Expand if rasters is 2D
|
||||
if len(rasters.shape) < 3:
|
||||
rasters = np.expand_dims(rasters, 2)
|
||||
|
||||
## Scale to new dtype
|
||||
rep = np.iinfo(dtype)
|
||||
if normalize_to_dtype:
|
||||
## Scale to new dtype
|
||||
rep = np.iinfo(dtype)
|
||||
|
||||
rasters = rasters.astype(np.float64)
|
||||
rasters = rasters.astype(np.float64)
|
||||
|
||||
## Channel independant scale
|
||||
for i in range(rasters.shape[2]):
|
||||
rasters[:,:,i] -= rasters[:,:,i].min() - rep.min
|
||||
rasters[:,:,i] *= (rep.max - rep.min) / (rasters[:,:,i].max() - rasters[:,:,i].min())
|
||||
## Channel independant scale
|
||||
for i in range(rasters.shape[2]):
|
||||
rasters[:,:,i] -= rasters[:,:,i].min() - rep.min
|
||||
rasters[:,:,i] *= (rep.max - rep.min) / (rasters[:,:,i].max() - rasters[:,:,i].min())
|
||||
|
||||
rasters = rasters.astype(dtype)
|
||||
write(self.infile, rasters)
|
||||
|
Loading…
Reference in New Issue
Block a user