others
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | 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() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 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() |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程