Skip to content

Commit 281c228

Browse files
committed
Adding more stabile depth
1 parent 06225d9 commit 281c228

File tree

2 files changed

+71
-14
lines changed

2 files changed

+71
-14
lines changed

dynamic-calibration/utils/dynamic_calibration_interactive.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from collections import deque
12
import numpy as np
23
import cv2
34
import time
@@ -13,7 +14,10 @@
1314
print_final_calibration_results,
1415
)
1516

16-
17+
# Rolling average for depth ROI means
18+
DEPTH_HISTORY_SIZE = 10
19+
depth_history = deque(maxlen=DEPTH_HISTORY_SIZE)
20+
last_mouse_pos = None # reset history if mouse moves to new pixel
1721
mouse_coords = (-1, -1)
1822

1923

@@ -234,23 +238,31 @@ def on_mouse_disparity(event, x, y, flags, param):
234238
cy = int(round(local_y * 2))
235239
cx = int(round(local_x * 2))
236240

237-
half = 10 # 9x9 windowradius 4
241+
half = 10 # ROI radiuswindow size = 2*half + 1
238242
y0 = max(0, cy - half)
239243
y1 = min(depth_frame.shape[0], cy + half + 1)
240244
x0 = max(0, cx - half)
241245
x1 = min(depth_frame.shape[1], cx + half + 1)
242246

243247
roi = depth_frame[y0:y1, x0:x1].astype(np.float32)
244248

245-
# If you want to ignore invalid zeros (common in depth):
249+
# Rolling-mean over last 10 frames; clears if cursor center changes
250+
cur_mouse_pos = (cx, cy)
251+
if last_mouse_pos != cur_mouse_pos:
252+
depth_history.clear()
253+
last_mouse_pos = cur_mouse_pos
254+
246255
valid = roi > 0
247256
if np.any(valid):
248-
depth_val = float(roi[valid].mean()) / 1000.0 # mm → m
257+
depth_mm = float(roi[valid].mean()) # in millimeters
258+
depth_history.append(depth_mm)
259+
# else: keep previous history if current ROI is invalid
260+
261+
if len(depth_history) > 0:
262+
depth_val = float(np.mean(depth_history)) / 1000.0 # mm -> m
249263
else:
250264
depth_val = float("nan")
251-
depth_val = (
252-
depth_frame[int(local_y * 2), int(local_x * 2)] / 1000.0
253-
) # mm -> meters
265+
254266
display_text_depth = f"Depth: {depth_val:.2f}m"
255267
text_pos = (scaled_mouse_x + 10, scaled_mouse_y + 10)
256268
disp_x0 = disp_x_start + (x0 // 2)

dynamic-calibration/utils/dynamic_controler.py

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import Optional, Tuple
2+
from collections import deque
23
import numpy as np
34
import time
45

@@ -59,6 +60,11 @@ def __init__(self):
5960
self._roi_half = 10 # radius in depth pixels (=> (2h+1)^2 window)
6061
self._hud_on = True # toggle with 'g'
6162

63+
self._avg_len = 10
64+
self._roi_mean_hist = deque(maxlen=self._avg_len)
65+
self._center_hist = deque(maxlen=self._avg_len)
66+
self._hist_cursor_xy = None
67+
6268
self.out_annotations = self.createOutput(
6369
possibleDatatypes=[
6470
dai.Node.DatatypeHierarchy(dai.DatatypeEnum.ImgAnnotations, True)
@@ -135,17 +141,56 @@ def _draw_depth_hud(self, hud: dai.ImgAnnotation, preview_w: int, preview_h: int
135141
roi = self._last_depth[y0:y1, x0:x1]
136142

137143
# stats (ignore zeros if depth)
144+
if self._hist_cursor_xy != (cx, cy):
145+
self._roi_mean_hist.clear()
146+
self._center_hist.clear()
147+
self._hist_cursor_xy = (cx, cy)
148+
138149
if self._depth_is_mm:
139150
valid = roi > 0
140-
center_val = float(self._last_depth[cy, cx]) / 1000.0 # mm → m
141-
mean_val = (
142-
float(roi[valid].mean() / 1000.0) if np.any(valid) else float("nan")
151+
152+
# center (mm)
153+
center_mm = float(self._last_depth[cy, cx])
154+
if center_mm > 0:
155+
self._center_hist.append(center_mm)
156+
157+
# roi mean (mm)
158+
if np.any(valid):
159+
roi_mean_mm = float(roi[valid].mean())
160+
self._roi_mean_hist.append(roi_mean_mm)
161+
162+
# aggregated (m)
163+
if len(self._center_hist) > 0:
164+
center_val = float(np.mean(self._center_hist)) / 1000.0
165+
else:
166+
center_val = float("nan")
167+
168+
if len(self._roi_mean_hist) > 0:
169+
mean_val = float(np.mean(self._roi_mean_hist)) / 1000.0
170+
else:
171+
mean_val = float("nan")
172+
173+
val_text = (
174+
f"Depth: {center_val:.2f} m, "
175+
f"ROI mean: {mean_val:.2f} m "
176+
f"(avg {len(self._roi_mean_hist)}/{self._avg_len})"
143177
)
144-
val_text = f"Depth: {center_val:.2f} m, ROI mean: {mean_val:.2f} m"
145178
else:
146-
center_val = float(self._last_depth[cy, cx])
147-
mean_val = float(roi.mean())
148-
val_text = f"Disp: {center_val:.1f}, ROI mean: {mean_val:.1f}"
179+
# disparity path (no zero filtering)
180+
center_disp = float(self._last_depth[cy, cx])
181+
roi_mean_disp = float(roi.mean())
182+
183+
self._center_hist.append(center_disp)
184+
self._roi_mean_hist.append(roi_mean_disp)
185+
186+
center_val = float(np.mean(self._center_hist))
187+
mean_val = float(np.mean(self._roi_mean_hist))
188+
189+
val_text = (
190+
f"Disp: {center_val:.1f}, "
191+
f"ROI mean: {mean_val:.1f} "
192+
f"(avg {len(self._roi_mean_hist)}/{self._avg_len})"
193+
)
149194

150195
# normalized positions relative to preview (assume 1:1 mapping depth→preview)
151196
u = (cx + 0.5) / W

0 commit comments

Comments
 (0)