使用OpenCV和Python从四个角点裁剪图像的步骤如下:
import cv2
import numpy as np
image = cv2.imread('image.jpg')
cv2.imshow('Original Image', image)
cv2.waitKey(0)
# 定义四个角点的坐标(顺时针或逆时针)
points = np.array([(x1, y1), (x2, y2), (x3, y3), (x4, y4)], dtype=np.float32)
width = max(np.linalg.norm(points[0] - points[1]), np.linalg.norm(points[2] - points[3]))
height = max(np.linalg.norm(points[1] - points[2]), np.linalg.norm(points[3] - points[0]))
output_points = np.array([(0, 0), (width - 1, 0), (width - 1, height - 1), (0, height - 1)], dtype=np.float32)
matrix = cv2.getPerspectiveTransform(points, output_points)
output_image = cv2.warpPerspective(image, matrix, (int(width), int(height)))
cv2.imshow('Cropped Image', output_image)
cv2.waitKey(0)
完成以上步骤后,你将得到从四个角点裁剪后的图像。请注意,上述代码中的坐标和图像路径需要根据实际情况进行调整。
领取专属 10元无门槛券
手把手带您无忧上云