From 33db105518bbab53e07ec4ba143b025b6f30fd99 Mon Sep 17 00:00:00 2001 From: liuyebo <1515783401@qq.com> Date: Wed, 10 Jul 2024 10:12:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=B6=82=E6=8A=B9=E6=A1=86?= =?UTF-8?q?=E6=97=8B=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- photo_mask.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/photo_mask.py b/photo_mask.py index 52fdb39..6e04ed9 100644 --- a/photo_mask.py +++ b/photo_mask.py @@ -115,15 +115,19 @@ def rotate_image(img, angle): def rotate_rectangle(rectangle, center, angle): def rotate_point(pt, angle, center): matrix = cv2.getRotationMatrix2D(center, angle, 1) - # 旋转后平移 - matrix[0, 2] += center[1] - center[0] - matrix[1, 2] += center[0] - center[1] + if angle != 180: + # 旋转后平移 + matrix[0, 2] += center[1] - center[0] + matrix[1, 2] += center[0] - center[1] reverse_matrix = cv2.invertAffineTransform(matrix) pt = np.array([[pt[0]], [pt[1]], [1]]) return np.dot(reverse_matrix, pt) + if angle == 0: + return list(rectangle) + x1, y1, x2, y2 = rectangle # 计算矩形的四个顶点 @@ -193,7 +197,7 @@ def find_box_of_content(content, layout): ) -def mask_image(image, content): +def get_mask_layout(image, content): with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file: cv2.imwrite(temp_file.name, image) @@ -238,7 +242,7 @@ def photo_mask(pk_phhd, content): for img in split_result: angles = get_image_rotation_angles(img["img"]) rotated_img = rotate_image(img["img"], int(angles[0])) - results = mask_image(rotated_img, content) + results = get_mask_layout(rotated_img, content) for result in results: height, width = img["img"].shape[:2]