def anglebetweenlines(line1: np.ndarray, line2: np.ndarray) -> float: """Calculates the…
- User
def angle_between_lines(line1: np.ndarray, line2: np.ndarray) -> float: """Calculates the angle in degrees between two lines.""" k1, b1 = line1 k2, b2 = line2 angle_rad = np.arctan2(k2 - k1, 1 + k1 * k2) angle_deg = np.degrees(angle_rad) return angle_deg
def get_best_lines(potential_points: np.ndarray, min_angle: float = 30.0) -> np.ndarray: """ Gets best combinations of lines by all points. Line y = kx + b. Returns np.ndarray like [[k1, b1], [k2, b2]]. """ best_lines = [] best_inliers = [] iterations_num = 1000 threshold_distance = 5 threshold_k = 0.25 threshold_b = 10
for _ in range(3): local_best_lines = [] local_best_inliers = [] if potential_points.shape[0] < 3: break for i in range(iterations_num): sample_indices = np.random.choice(potential_points.shape[0], size=3, replace=False) sample_points = potential_points[sample_indices] sample_x = sample_points[:, 1] sample_y = sample_points[:, 0] coefficients = np.polyfit(sample_x, sample_y, deg=1) distances = np.abs(potential_points[:, 0] - np.matmul(np.vstack((potential_points[:, 1], np.ones_like(potential_points[:, 1]))).T, coefficients)) / np.sqrt(coefficients[0] ** 2 + 1) inliers = np.where(distances < threshold_distance)[0] is_similar = False for line in local_best_lines: diff_k = np.abs(coefficients[0] - line[0]) diff_b = np.abs(coefficients[0] - line[0]) if diff_k <= threshold_k and diff_b <= threshold_b: is_similar = True break if not is_similar and len(inliers) > len(local_best_inliers): local_best_lines = [coefficients] local_best_inliers = inliers elif not is_similar and len(inliers) == len(local_best_inliers): local_best_lines.append(coefficients) mask = np.ones(len(potential_points), dtype=bool) for equation in local_best_lines: k, b = equation x = potential_points[:, 1] y = potential_points[:, 0] distances = np.abs(y - (k * x + b)) mask[np.where(distances < threshold_distance)[0]] = False best_lines.extend(local_best_lines) best_inliers.extend(local_best_inliers) potential_points = potential_points[mask] if len(best_lines) >= 2: angles = [] for i in range(len(best_lines)): for j in range(i+1, len(best_lines)): angle = angle_between_lines(best_lines[i], best_lines[j]) angles.append(angle) if all(angle >= min_angle for angle in angles): break return np.array(best_lines[:3])Поставь в этом коде правильно пробелы(табуляцию)
- Assistant
- User
Ну и где табуляция?!
- Assistant
- User
В этом коде мы удаляем potential_points, через которые уже нашли лучшую прямую? Если нет, добавь это, чтобы все три прямые были лучшие и разные
- Assistant