批量把大于400像素的文档图片裁剪矫正。
请确保你已经安装了OpenCV库。如果没有,请使用以下命令安装:
pip install opencv-python-headless
import cv2
import numpy as np
import os
def order_points(pts):
# 初始化坐标点,顺序为左上,右上,右下,左下
rect = np.zeros((4, 2), dtype="float32")
# 左上点有最小的和,右下点有最大的和
s = pts.sum(axis=1)
rect[0] = pts[np.argmin(s)]
rect[2] = pts[np.argmax(s)]
# 计算点的差,右上点有最小的差,左下点有最大的差
diff = np.diff(pts, axis=1)
rect[1] = pts[np.argmin(diff)]
rect[3] = pts[np.argmax(diff)]
return rect
def four_point_transform(image, pts):
# 获取坐标点,并将它们分成左上,右上,右下,左下
rect = order_points(pts)
(tl, tr, br, bl) = rect
# 计算输入的w和h值
widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
maxWidth = max(int(widthA), int(widthB))
heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
maxHeight = max(int(heightA), int(heightB))
# 透视变换的目标点
dst = np.array([
[0, 0],
[maxWidth - 1, 0],
[maxWidth - 1, maxHeight - 1],
[0, maxHeight - 1]], dtype="float32")
# 计算变换矩阵并应用它
M = cv2.getPerspectiveTransform(rect, dst)
warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
return warped
def crop_and_warp_image(image_path):
image = cv2.imread(image_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, threshold1=100, threshold2=200)
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if not contours:
return None
max_contour = max(contours, key=cv2.contourArea)
x, y, w, h = cv2.boundingRect(max_contour)
if w > 400 and h > 400:
# 这里假设max_contour是矩形的近似轮廓
epsilon = 0.05 * cv2.arcLength(max_contour, True)
approx = cv2.approxPolyDP(max_contour, epsilon, True)
if len(approx) == 4:
# 转换为四点坐标
pts = approx.reshape(4, 2)
warped = four_point_transform(image, pts)
return warped
else:
print(f"Cannot find four corners for {image_path}")
return None
cut_dir = 'cut'
os.makedirs(cut_dir, exist_ok=True)
image_files = [f for f in os.listdir('.') if f.lower().endswith(('.png', '.jpg', '.jpeg', '.tiff', '.bmp', '.gif'))]
for image_file in image_files:
warped_image = crop_and_warp_image(image_file)
if warped_image is not None:
cv2.imwrite(os.path.join(cut_dir, f'cut_{image_file}'), warped_image)