Signed-off-by: bluepulsar <cheungken3@yahoo.com.hk>
This commit is contained in:
+6
-19
@@ -1,8 +1,7 @@
|
||||
import numpy as np
|
||||
import cv2
|
||||
from scipy.ndimage import gaussian_filter
|
||||
|
||||
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=10):
|
||||
def compute_foe(flow_field, min_magnitude=1.0, expansion_threshold=1, downsample_factor=1, return_all_contours=False, kernel_size=(5,5), denoise_sigma=10):
|
||||
"""
|
||||
计算光流场的扩张焦点(Focus of Expansion)。
|
||||
|
||||
@@ -25,34 +24,22 @@ def compute_foe(flow_field, min_magnitude=1.0, expansion_threshold=1, downsample
|
||||
flow_ds = flow_field[::downsample_factor, ::downsample_factor, :]
|
||||
u_ds, v_ds = flow_ds[:, :, 0], flow_ds[:, :, 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)
|
||||
magnitude_ds = cv2.GaussianBlur(magnitude_ds, (0, 0), denoise_sigma)
|
||||
divergence_ds = np.gradient(u_ds, axis=1) + np.gradient(v_ds, axis=0)
|
||||
|
||||
# 降噪:在计算散度之后对散度图进行平滑
|
||||
if denoise_sigma > 0:
|
||||
divergence_ds = cv2.GaussianBlur(divergence_ds, (0, 0), denoise_sigma)
|
||||
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:
|
||||
u, v = flow_field[:, :, 0], flow_field[:, :, 1]
|
||||
|
||||
# 降噪:在计算散度之前对光流场进行平滑
|
||||
if denoise_sigma > 0:
|
||||
u = cv2.GaussianBlur(u, (0, 0), denoise_sigma)
|
||||
v = cv2.GaussianBlur(v, (0, 0), denoise_sigma)
|
||||
|
||||
magnitude = np.sqrt(u**2 + v**2)
|
||||
magnitude = cv2.GaussianBlur(magnitude, (0, 0), denoise_sigma)
|
||||
divergence = np.gradient(u, axis=1) + np.gradient(v, axis=0)
|
||||
|
||||
# 降噪:在计算散度之后对散度图进行平滑
|
||||
if denoise_sigma > 0:
|
||||
divergence = cv2.GaussianBlur(divergence, (0, 0), denoise_sigma)
|
||||
divergence = cv2.GaussianBlur(divergence, (0, 0), denoise_sigma)
|
||||
|
||||
# 创建掩码
|
||||
valid_mask = magnitude > min_magnitude
|
||||
@@ -88,7 +75,7 @@ def compute_foe(flow_field, min_magnitude=1.0, expansion_threshold=1, downsample
|
||||
return result
|
||||
|
||||
def compute_multiple_foe(flow_field, min_magnitude=1.0, expansion_threshold=0.001,
|
||||
downsample_factor=10, min_contour_area=3000, denoise_sigma=10, kernel_size=(5,5)):
|
||||
downsample_factor=1, min_contour_area=3000, denoise_sigma=15, kernel_size=(5,5)):
|
||||
"""
|
||||
计算光流场的多个扩张焦点(Foci of Expansion)。
|
||||
|
||||
|
||||
@@ -241,7 +241,7 @@ class Sensor:
|
||||
|
||||
return self.flow
|
||||
|
||||
def get_foe(self, multiple_foe=False, min_contour_area=3000, min_magnitude=1.0, expansion_threshold=1, downsample_factor=10, kernel_size=(5,5), denoise_sigma=1.5):
|
||||
def get_foe(self, multiple_foe=False, min_contour_area=3000, min_magnitude=1.0, expansion_threshold=0.01, downsample_factor=1, kernel_size=(5,5), denoise_sigma=15):
|
||||
|
||||
if multiple_foe:
|
||||
return compute_multiple_foe(self.vnormal, min_contour_area=min_contour_area, min_magnitude=min_magnitude, expansion_threshold=expansion_threshold, downsample_factor=downsample_factor, kernel_size=kernel_size, denoise_sigma=denoise_sigma)
|
||||
|
||||
Reference in New Issue
Block a user