def detectlines(image): Загрузка и предобработка изображения imageblur =…
- User
def detect_lines(image): # Загрузка и предобработка изображения image_blur = np.zeros_like(image) kernel = np.array([[1/16, 1/8, 1/16], [1/8, 1/4, 1/8], [1/16, 1/8, 1/16]], dtype=np.float32) image_blur[1:-1, 1:-1] = np.convolve(np.convolve(image, kernel, "valid"), kernel.T, "valid")
# Применение оператора Собеля для выделения границ sobelx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], dtype=np.float32) sobely = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]], dtype=np.float32) gradient_x = np.zeros_like(image_blur) gradient_y = np.zeros_like(image_blur) gradient_x[1:-1, 1:-1] = np.abs(np.convolve(np.convolve(image_blur, sobelx, "valid"), sobelx.T, "valid")) gradient_y[1:-1, 1:-1] = np.abs(np.convolve(np.convolve(image_blur, sobely, "valid"), sobely.T, "valid")) # Вычисление градиента и ориентации границ gradient_magnitude = np.sqrt(gradient_x ** 2 + gradient_y ** 2) angle = np.arctan2(gradient_y, gradient_x) * 180 / np.pi # Создание бинарного изображения с пороговым значением thresh = np.zeros_like(image, dtype=np.uint8) thresh[gradient_magnitude > 100] = 255 # Найденные линии lines = hough_lines(thresh, 1, np.pi/180, threshold=100) # Вывод найденных линий на исходное изображение final_image = np.copy(image) for rho, theta in lines: a = np.cos(theta) b = np.sin(theta) x0 = a * rho y0 = b * rho x1 = int(x0 + 1000*(-b)) y1 = int(y0 + 1000*(a)) x2 = int(x0 - 1000*(-b)) y2 = int(y0 - 1000*(a)) return final_imagedef hough_lines(image, rho_resolution, theta_resolution, threshold): height, width = image.shape diagonal = np.sqrt(height ** 2 + width ** 2) rho_max = int(diagonal / rho_resolution) rho_values = np.arange(-rho_max, rho_max + 1) * rho_resolution theta_values = np.arange(0, np.pi, theta_resolution)
cos_theta = np.cos(theta_values) sin_theta = np.sin(theta_values) accumulator = np.zeros((len(rho_values), len(theta_values)), dtype=np.int32) y_indices, x_indices = np.nonzero(image) for i in range(len(x_indices)): x = x_indices[i] y = y_indices[i] for j in range(len(theta_values)): rho = int(x * cos_theta[j] + y * sin_theta[j]) rho_index = np.abs(rho_values - rho).argmin() accumulator[rho_index, j] += 1 rho_indices, theta_indices = np.nonzero(accumulator > threshold) lines = [] for i in range(len(rho_indices)): rho_index = rho_indices[i] theta_index = theta_indices[i] lines.append((rho_values[rho_index], theta_values[theta_index])) return linesimage = cv2.imread("0.0/00.pgm", cv2.COLOR_BGR2GRAY) lines = detect_lines(image) print(lines)
Получаю ошибку:
ValueError Traceback (most recent call last) in <cell line: 76>() 74 75 image = cv2.imread("0.0/00.pgm", cv2.COLOR_BGR2GRAY) ---> 76 lines = detect_lines(image) 77 print(lines)
2 frames in detect_lines(image) 5 [1/8, 1/4, 1/8], 6 [1/16, 1/8, 1/16]], dtype=np.float32) ----> 7 image_blur[1:-1, 1:-1] = np.convolve(np.convolve(image, kernel, "valid"), kernel.T, "valid") 8 9 # Применение оператора Собеля для выделения границ
- Assistant
The error message suggests that the image variable passed to the
detect_linesfunction is too deep for the desired array. This error likely occurs because the image variable is not in the correct format or shape.To resolve this issue, make sure that the image variable is loaded correctly and is in the expected format (e.g., grayscale). Additionally, ensure that the image variable has the correct shape required by the function.