def get_coords_by_threshold(
image: np.ndarray, threshold: int = IMAGE_THRESHOLD
) -> tuple:
"""Gets pixel coords with intensity more then 'theshold'."""
coords = np.where(image > threshold)
return coords
def delete_pixels_by_min_threshold(
image: np.ndarray, min_threshold: int = IMAGE_THRESHOLD
) -> np.ndarray:
"""Deletes coords from image and returns this image."""
anti_coords = np.where(image < min_threshold)
image[anti_coords] = 0
return image
def get_neigh_nums_list(
coords: tuple, kernel_size: int = 3
) -> list:
"""
Gets list with number of neighborhoods by kernel.
Returns list like [[x1, y1, N1], [x2, y2, N2]],
Where N - neighborhood's number.
"""
neigh_nums = []
offset = (kernel_size - 1) // 2
for x, y in zip(coords[0], coords[1]):
if x < 0 + offset or x > image.shape[0] - offset:
continue
if y < 0 + offset or y > image.shape[1] - offset:
continue
kernel = image[x-offset:x+offset+1, y-offset:y+offset+1]
step_neigh_num = np.count_nonzero(kernel) - 1
neigh_nums.append([x, y, step_neigh_num])
return neigh_nums
def sort_neight_nums_by_N(neigh_nums: list) -> np.ndarray:
"""
Sort neigh_nums by N parameter.
Removes N=-1, N=0 and sorts by N desc.
"""
np_neigh_nums = np.array(neigh_nums)
Для того чтобы найти точки пересечения прямых, можно воспользоваться функцией numpy.linalg.solve.
Первым шагом необходимо получить уравнения прямых в виде y = kx + b. Затем можно использовать функцию numpy.linalg.solve, передавая коэффициенты прямых в виде двухмерного массива.
Заметьте, что в данном примере предполагается, что найдены именно три прямых. Если их количество может быть любым, то можно использовать цикл для перебора всех возможных комбинаций и нахождения точек пересечения их парами.
for equation in best_lines:
k, b = equation
x = np.arange(0, image.shape[1])
y = k * x + b
plt.imshow(image, cmap="gray")
plt.scatter(potential_points[:, 1], potential_points[:, 0], color="red", marker="o")
plt.plot(x, y, color="green")
plt.scatter(potential_points[:, 1], potential_points[:, 0], color="red", marker="o")
plt.show()
Найди точки пересечения прямых, представь их в виде np.ndarray типа float размерностью (3, 2), где в каждой строке координаты найденной вершины x и y