print(f"Количество зануленных пикселей: {zero_pixels}")
print(f"Процент зануленных пикселей относительно всех пикселей: {zero_percentage}%")
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
code
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)
Ошибка возникает в строке, где вы используете функцию np.random.choice(). Ошибка указывает, что входное значение "а" должно быть больше 0, если не выбраны никакие образцы. Проверьте, что размер оставшихся точек больше 0 перед использованием этой функции.
User
Как из массива numpy рандомно выбрать 10 элементов из другого массива
Assistant
Чтобы случайным образом выбрать 10 элементов из массива numpy, вы можете использовать функцию np.random.choice(). Вот пример:
В этом примере мы используем np.random.choice() для выбора 10 случайных элементов из массива arr. size=10 указывает, что мы хотим выбрать 10 элементов, а replace=False гарантирует, что каждый выбранный элемент будет уникальным.
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()
ПОЛУЧАЮ ОШИБКУ , В ЧЕМ МОЖЕТ БЫТЬ ДЕЛО?!
ERROR: a must be greater than 0 unless no samples are taken; Traceback: File "/3divi/estimators/test_estimator.py", line 72, in estimator; result = solver.solve(image); File "/tmp/tmplwyog_v6/solver.py", line 11, in solve; result = self.alg.find_corners(image_gray); File "/tmp/tmplwyog_v6/corner_detector/corner_detector.py", line 163, in find_corners; best_lines = self.__get_best_lines(potential_points); File "/tmp/tmplwyog_v6/corner_detector/corner_detector.py", line 118, in __get_best_lines; sample_indices = np.random.choice(remaining_points.shape[0], size=3, replace=False); File "mtrand.pyx", line 902, in numpy.random.mtrand.RandomState.choice
ПОМОГИТЕ!