developing depth estimation

This commit is contained in:
bluepulsar
2025-12-28 18:20:30 +08:00
parent 017561578d
commit 6ec9eaa334
7 changed files with 782 additions and 55 deletions
+42 -52
View File
@@ -1,7 +1,7 @@
import numpy as np
import cv2
def compute_foe(flow_field, min_magnitude=1.0, expansion_threshold=1, downsample_factor=10, return_all_contours=False,kernel_size=(5,5)):
def compute_foe(flow_field, min_magnitude=1.0, expansion_threshold=1, downsample_factor=10, return_all_contours=False, kernel_size=(5,5), denoise_sigma=1.5):
"""
计算光流场的扩张焦点(Focus of Expansion)。
@@ -11,97 +11,83 @@ def compute_foe(flow_field, min_magnitude=1.0, expansion_threshold=1, downsample
expansion_threshold: 扩张判定阈值,用于区分扩张和收缩区域
downsample_factor: 下采样因子,用于平滑计算(默认10)
return_all_contours: 是否返回所有轮廓(默认False,仅返回最大轮廓)
kernel_size: 形态学操作的核大小
denoise_sigma: 降噪高斯滤波的标准差(默认1.5,设为0禁用降噪)
Returns:
dict: 包含扩张焦点信息的字典,包括:
- 'foe_point': 扩张焦点坐标 (x, y),如果找到单个焦点
- 'expansion_region': 扩张区域轮廓(最大轮廓)
- 'expansion_mask': 扩张区域二值掩码
- 'contraction_mask': 收缩区域二值掩码
- 'divergence_map': 散度图
- '_all_contours': 所有轮廓列表(仅当return_all_contours=True时)
dict: 包含扩张焦点信息的字典
"""
H, W = flow_field.shape[:2]
# 平滑处理:下采样光流场,然后计算并插值回原尺寸
# 下采样并计算散度,然后插值回原尺寸
if downsample_factor > 1:
# 下采样光流场
flow_field_downsampled = flow_field[::downsample_factor, ::downsample_factor, :]
h_ds, w_ds = flow_field_downsampled.shape[:2]
flow_ds = flow_field[::downsample_factor, ::downsample_factor, :]
u_ds, v_ds = flow_ds[:, :, 0], flow_ds[:, :, 1]
# 在下采样的光流场上计算幅度和散度
u_ds = flow_field_downsampled[:, :, 0]
v_ds = flow_field_downsampled[:, :, 1]
# 降噪:在计算散度之前对光流场进行平滑
if denoise_sigma > 0:
u_ds = cv2.GaussianBlur(u_ds, (0, 0), denoise_sigma)
v_ds = cv2.GaussianBlur(v_ds, (0, 0), denoise_sigma)
# 计算下采样后的光流幅度
magnitude_ds = np.sqrt(u_ds**2 + v_ds**2)
divergence_ds = np.gradient(u_ds, axis=1) + np.gradient(v_ds, axis=0)
# 计算下采样后的散度
du_dx_ds = np.gradient(u_ds, axis=1)
dv_dy_ds = np.gradient(v_ds, axis=0)
divergence_ds = du_dx_ds + dv_dy_ds
# 降噪:在计算散度之后对散度图进行平滑
if denoise_sigma > 0:
divergence_ds = cv2.GaussianBlur(divergence_ds, (0, 0), denoise_sigma)
# 插值回原尺寸
magnitude = cv2.resize(magnitude_ds, (W, H), interpolation=cv2.INTER_CUBIC)
divergence = cv2.resize(divergence_ds, (W, H), interpolation=cv2.INTER_CUBIC)
else:
# 不进行下采样,直接计算
# 计算光流幅度
magnitude = np.sqrt(flow_field[:, :, 0]**2 + flow_field[:, :, 1]**2)
u, v = flow_field[:, :, 0], flow_field[:, :, 1]
# 计算散度 (divergence)
u = flow_field[:, :, 0]
v = flow_field[:, :, 1]
# 降噪:在计算散度之前对光流场进行平滑
if denoise_sigma > 0:
u = cv2.GaussianBlur(u, (0, 0), denoise_sigma)
v = cv2.GaussianBlur(v, (0, 0), denoise_sigma)
# 使用中心差分计算散度
du_dx = np.gradient(u, axis=1)
dv_dy = np.gradient(v, axis=0)
divergence = du_dx + dv_dy
magnitude = np.sqrt(u**2 + v**2)
divergence = np.gradient(u, axis=1) + np.gradient(v, axis=0)
# 降噪:在计算散度之后对散度图进行平滑
if denoise_sigma > 0:
divergence = cv2.GaussianBlur(divergence, (0, 0), denoise_sigma)
# 创建有效点掩码(幅度足够大的点)
# 创建掩码
valid_mask = magnitude > min_magnitude
# 创建扩张和收缩区域掩码
expansion_mask = np.zeros_like(divergence, dtype=bool)
contraction_mask = np.zeros_like(divergence, dtype=bool)
expansion_mask[valid_mask] = divergence[valid_mask] > expansion_threshold
contraction_mask[valid_mask] = divergence[valid_mask] < -expansion_threshold
expansion_mask = (valid_mask & (divergence > expansion_threshold))
contraction_mask = (valid_mask & (divergence < -expansion_threshold))
result = {
'expansion_mask': expansion_mask,
'contraction_mask': contraction_mask,
'divergence_map': divergence
'divergence_map': divergence,
'foe_points': [0, 0],
'is_foe_found': False
}
# 寻找扩张区域轮廓
expansion_binary = expansion_mask.astype(np.uint8) * 255
if np.any(expansion_binary):
# 应用形态学闭运算来填充小孔和连接邻近区域
kernel = np.ones(kernel_size, np.uint8)
expansion_binary_closed = cv2.morphologyEx(expansion_binary, cv2.MORPH_CLOSE, kernel)
expansion_binary_closed = cv2.morphologyEx(expansion_binary, cv2.MORPH_CLOSE, np.ones(kernel_size, np.uint8))
contours, _ = cv2.findContours(expansion_binary_closed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if contours:
# 如果请求返回所有轮廓,保存它们
result['is_foe_found'] = True
if return_all_contours:
result['_all_contours'] = contours
# 找到最大的扩张区域轮廓
largest_contour = max(contours, key=cv2.contourArea)
result['expansion_region'] = largest_contour
# 计算扩张区域的质心作为焦点
M = cv2.moments(largest_contour)
if M["m00"] > 0:
cx = int(M["m10"] / M["m00"])
cy = int(M["m01"] / M["m00"])
result['foe_point'] = (cx, cy)
result['foe_point'] = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
return result
def compute_multiple_foe(flow_field, min_magnitude=1.0, expansion_threshold=0.001,
downsample_factor=10, min_contour_area=3000):
downsample_factor=10, min_contour_area=3000, denoise_sigma=1.5, kernel_size=(5,5)):
"""
计算光流场的多个扩张焦点(Foci of Expansion)。
@@ -111,6 +97,8 @@ def compute_multiple_foe(flow_field, min_magnitude=1.0, expansion_threshold=0.00
expansion_threshold: 扩张判定阈值,用于区分扩张和收缩区域
downsample_factor: 下采样因子,用于平滑计算(默认10)
min_contour_area: 最小轮廓面积阈值,小于此值的轮廓会被忽略
denoise_sigma: 降噪高斯滤波的标准差(默认1.5,设为0禁用降噪)
kernel_size: 形态学操作的核大小
Returns:
dict: 包含多个扩张焦点信息的字典,包括:
@@ -126,7 +114,9 @@ def compute_multiple_foe(flow_field, min_magnitude=1.0, expansion_threshold=0.00
min_magnitude=min_magnitude,
expansion_threshold=expansion_threshold,
downsample_factor=downsample_factor,
return_all_contours=True
return_all_contours=True,
kernel_size=kernel_size,
denoise_sigma=denoise_sigma
)
# 构建多焦点结果,复用基础数据