@@ -23,7 +23,7 @@ class Depth(aloscene.tensors.SpatialAugmentedTensor):
2323 occlusion : aloscene.Mask
2424 Occlusion mask for this Depth map. Default value : None.
2525 is_bsolute: bool
26- Either depth values refer to real values or shifted and scaled ones.
26+ Either depth values refer to real values or shifted and scaled ones.
2727 scale: float
2828 Scale used to to shift depth. Pass this argument only if is_bsolute is set to True
2929 shift: float
@@ -32,14 +32,14 @@ class Depth(aloscene.tensors.SpatialAugmentedTensor):
3232
3333 @staticmethod
3434 def __new__ (
35- cls ,
36- x ,
37- occlusion : Mask = None ,
38- is_absolute = False ,
39- scale = None ,
40- shift = None ,
41- * args ,
42- names = ("C" , "H" , "W" ),
35+ cls ,
36+ x ,
37+ occlusion : Mask = None ,
38+ is_absolute = False ,
39+ scale = None ,
40+ shift = None ,
41+ * args ,
42+ names = ("C" , "H" , "W" ),
4343 ** kwargs ):
4444 if is_absolute and not (shift and scale ):
4545 raise AttributeError ('absolute depth requires shift and scale arguments' )
@@ -58,9 +58,20 @@ def __new__(
5858 def __init__ (self , x , * args , ** kwargs ):
5959 super ().__init__ (x )
6060
61- def encode_inverse (self ):
61+ def encode_inverse (self , prior_clamp_min = None , prior_clamp_max = None , post_clamp_min = None , post_clamp_max = None ):
6262 """Undo encode_absolute tansformation
63-
63+
64+ Parameters
65+ ----------
66+ prior_clamp_min: float | None
67+ Clamp min depth before to convert to idepth
68+ prior_clamp_max: float | None
69+ Clamp max depth before to convert to idepth
70+ post_clamp_min: float | None
71+ Clamp min output idepth
72+ post_clamp_max: float | None
73+ Clamp max output idepth
74+
6475 Exemples
6576 -------
6677 >>> not_absolute_depth = Depth(torch.ones((1, 1, 1)), is_absolute=False)
@@ -72,23 +83,40 @@ def encode_inverse(self):
7283 depth = self
7384 if not depth .is_absolute :
7485 raise ExecError ('can not inverse depth, already inversed' )
86+ shift = depth .shift if depth .shift is not None else 0
87+ scale = depth .scale if depth .scale is not None else 1
88+
89+ if prior_clamp_min is not None or prior_clamp_max is not None :
90+ depth = torch .clamp (depth , min = prior_clamp_min , max = prior_clamp_max )
91+
7592 depth = 1 / depth
76- depth = (depth - depth .shift ) / depth .scale
93+ depth = (depth - shift ) / scale
94+
95+ if post_clamp_min is not None or post_clamp_max is not None :
96+ depth = torch .clamp (depth , min = post_clamp_min , max = post_clamp_max )
97+
7798 depth .scale = None
7899 depth .shift = None
79100 depth .is_absolute = False
80101 return depth
81-
82- def encode_absolute (self , scale = 1 , shift = 0 ):
102+
103+ def encode_absolute (self , scale = 1 , shift = 0 , prior_clamp_min = None , prior_clamp_max = None , post_clamp_min = None , post_clamp_max = None ):
83104 """Transforms inverted depth to absolute depth
84-
105+
85106 Parameters
86107 ----------
87108 scale: (: float)
88109 Multiplication factor. Default is 1.
89-
90110 shift: (: float)
91111 Addition intercept. Default is 0.
112+ prior_clamp_min: float | None
113+ Clamp min idepth before to convert to depth
114+ prior_clamp_max: float | None
115+ Clamp max idepth before to convert to depth
116+ post_clamp_min: float | None
117+ Clamp min output idepth
118+ post_clamp_max: float | None
119+ Clamp max output idepth
92120
93121 Exemples
94122 --------
@@ -100,12 +128,24 @@ def encode_absolute(self, scale=1, shift=0):
100128 depth , names = self .rename (None ), self .names
101129 if depth .is_absolute :
102130 raise ExecError ('depth already in absolute state, call encode_inverse first' )
131+
103132 depth = depth * scale + shift
133+
134+ if prior_clamp_min is not None or prior_clamp_max is not None :
135+ depth = torch .clamp (depth , min = prior_clamp_min , max = prior_clamp_max )
136+
104137 depth [torch .unsqueeze (depth < 1e-8 , dim = 0 )] = 1e-8
105138 depth .scale = scale
106139 depth .shift = shift
107140 depth .is_absolute = True
108- return (1 / depth ).rename (* names )
141+
142+ n_depth = (1 / depth ).rename (* names )
143+
144+ if post_clamp_min is not None or post_clamp_max is not None :
145+ n_depth = torch .clamp (n_depth , min = post_clamp_min , max = post_clamp_max )
146+
147+ return n_depth
148+
109149
110150 def append_occlusion (self , occlusion : Mask , name : str = None ):
111151 """Attach an occlusion mask to the depth tensor.
0 commit comments