cksghl1004/cpp_moondream2
0200
1import numpy as np2 3 4def remove_outlier_points(points_tuples, k_nearest=2, threshold=2.0):5 """6 Robust outlier detection for list of (x,y) tuples.7 Only requires numpy.8 9 Args:10 points_tuples: list of (x,y) tuples11 k_nearest: number of neighbors to consider12 threshold: multiplier for median distance13 14 Returns:15 list: filtered list of (x,y) tuples with outliers removed16 list: list of booleans indicating which points were kept (True = kept)17 """18 points = np.array(points_tuples)19 n_points = len(points)20 21 # Calculate pairwise distances manually22 dist_matrix = np.zeros((n_points, n_points))23 for i in range(n_points):24 for j in range(i + 1, n_points):25 # Euclidean distance between points i and j26 dist = np.sqrt(np.sum((points[i] - points[j]) ** 2))27 dist_matrix[i, j] = dist28 dist_matrix[j, i] = dist29 30 # Get k nearest neighbors' distances31 k = min(k_nearest, n_points - 1)32 neighbor_distances = np.partition(dist_matrix, k, axis=1)[:, :k]33 avg_neighbor_dist = np.mean(neighbor_distances, axis=1)34 35 # Calculate mask using median distance36 median_dist = np.median(avg_neighbor_dist)37 mask = avg_neighbor_dist <= threshold * median_dist38 39 # Return filtered tuples and mask40 filtered_tuples = [t for t, m in zip(points_tuples, mask) if m]41 return filtered_tuples42 