Вот код, который я использую:
Код: Выделить всё
import cv2
import numpy as np
input_image = cv2.imread('img3.pgm', cv2.IMREAD_GRAYSCALE)
print(f"length of input image rows : {len(input_image)}")
print(f"length of input image columns: {len(input_image[0])}")
sobel_x = cv2.Sobel(input_image, cv2.CV_64F, 1, 0, ksize=3) # Sobel in x-direction
sobel_y = cv2.Sobel(input_image, cv2.CV_64F, 0, 1, ksize=3) # Sobel in y-direction
edge_magnitude = cv2.magnitude(sobel_x, sobel_y)
edge_magnitude_cropped = np.clip(edge_magnitude, 0, 255).astype(np.uint8)
print(f"length of filtered_image row {len(edge_magnitude_cropped)}" )
print(f"length of filtered_image col {len(edge_magnitude_cropped[0])}" )
cv2.imwrite('filtered_image_cv_cropped.pgm', edge_magnitude_cropped)
print("\nFiltered image saved as 'filtered_image_cv.pgm'.")
with open('original_image_cv_cropped.txt', 'w') as f:
for row in input_image:
f.write(" ".join(str(pixel) for pixel in row) + "\n")
print("\nOriginal image data saved as 'original_image_cv.txt'.")
with open('filtered_image_cv_cropped.txt', 'w') as f:
for row in edge_magnitude_cropped:
f.write(" ".join(str(pixel) for pixel in row) + "\n")
print("\nFiltered image data saved as 'filtered_image_cv.txt'.")
Подробнее здесь: https://stackoverflow.com/questions/792 ... e-even-tho