others

import cv2
import numpy as np
import math

# 原始图像路径
image_path = "path_to_image.jpg"

# 读取原始图像
image = cv2.imread(image_path)

# polygons 坐标 (左上、右上、右下、左下)
polygons = [11.91, 25.20, 444.01, 3.55, 467.29, 301.31, 24.98, 324.55]

# 提取左上角和右上角的坐标
x1, y1 = polygons[0], polygons[1]
x2, y2 = polygons[2], polygons[3]

# 计算旋转角度(以弧度表示)
angle = math.atan2(y2 - y1, x2 - x1) * 180 / math.pi  # 将弧度转换为角度

# 获取图像中心
(h, w) = image.shape[:2]
center = (w // 2, h // 2)

# 旋转图像,使检测区域水平对齐
M = cv2.getRotationMatrix2D(center, angle, 1.0)  # angle为旋转角度
rotated = cv2.warpAffine(image, M, (w, h))

# 计算检测区域中心
center_x = (polygons[0] + polygons[2] + polygons[4] + polygons[6]) / 4
center_y = (polygons[1] + polygons[3] + polygons[5] + polygons[7]) / 4

# 计算相对于图像中心的偏移量
offset_x = (w / 2) - center_x
offset_y = (h / 2) - center_y

# 构造平移矩阵
translation_matrix = np.float32([[1, 0, offset_x], [0, 1, offset_y]])

# 平移图像
translated = cv2.warpAffine(rotated, translation_matrix, (w, h))

# 保存或显示矫正后的图像
cv2.imwrite("corrected_image.jpg", translated)
cv2.imshow("Corrected Image", translated)
cv2.waitKey(0)
cv2.destroyAllWindows()

  

import cv2
import numpy as np
import math

# 原始图像路径
image_path = "path_to_image.jpg"

# 读取原始图像
image = cv2.imread(image_path)

# 模型输出的四边形顶点坐标 (左上、右上、右下、左下)
polygons = [11.91, 25.20, 444.01, 3.55, 467.29, 301.31, 24.98, 324.55]

# 提取四边形顶点坐标
x1, y1 = polygons[0], polygons[1]
x2, y2 = polygons[2], polygons[3]
x3, y3 = polygons[4], polygons[5]
x4, y4 = polygons[6], polygons[7]

# 计算矩形的中心点
center_x = (x1 + x2 + x3 + x4) / 4
center_y = (y1 + y2 + y3 + y4) / 4

# 计算矩形的旋转角度(逆时针为正)
angle = math.degrees(math.atan2(y2 - y1, x2 - x1))

# 获取图像宽度和高度
(h, w) = image.shape[:2]

# 旋转图像
rotation_matrix = cv2.getRotationMatrix2D((center_x, center_y), angle, 1.0)
rotated_image = cv2.warpAffine(image, rotation_matrix, (w, h), flags=cv2.INTER_LINEAR)

# 保存或显示旋转后的图像
cv2.imwrite("rotated_image.jpg", rotated_image)
cv2.imshow("Rotated Image", rotated_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

  

posted @ 2024-10-12 15:29  TW-NLP  阅读(3)  评论(0编辑  收藏  举报