您的位置:首页 > 财经 > 产业 > 推广app网站_统一社会信用代码_营销团队外包_seo推广有哪些方式

推广app网站_统一社会信用代码_营销团队外包_seo推广有哪些方式

2025/2/23 5:51:52 来源:https://blog.csdn.net/agito_cheung/article/details/145798344  浏览:    关键词:推广app网站_统一社会信用代码_营销团队外包_seo推广有哪些方式
推广app网站_统一社会信用代码_营销团队外包_seo推广有哪些方式

可以使用OpenCV实现的图像校正算法,包含透视校正和旋转校正的步骤,并附有详细注释。

具体如下:

import cv2
import numpy as npdef order_points(pts):"""将四个点按左上、右上、右下、左下顺序排列"""rect = np.zeros((4, 2), dtype="float32")# 按x坐标排序,分左右两组x_sorted = pts[np.argsort(pts[:, 0]), :]left = x_sorted[:2, :]right = x_sorted[2:, :]# 左组按y坐标排序(左上,左下)left = left[np.argsort(left[:, 1]), :](tl, bl) = left# 右组按y坐标排序(右上,右下)right = right[np.argsort(right[:, 1]), :](tr, br) = rightreturn np.array([tl, tr, br, bl], dtype="float32")def perspective_correction(img):"""透视校正:检测图像中的四边形并进行透视变换"""gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)blurred = cv2.GaussianBlur(gray, (5, 5), 0)# 自适应阈值处理增强边缘edged = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)# 查找轮廓contours, _ = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)contours = sorted(contours, key=cv2.contourArea, reverse=True)[:5]  # 取面积前5的轮廓for contour in contours:peri = cv2.arcLength(contour, True)approx = cv2.approxPolyDP(contour, 0.02 * peri, True)if len(approx) == 4:  # 检测到四边形ordered_pts = order_points(approx.reshape(4, 2))# 计算目标图像尺寸(tl, tr, br, bl) = ordered_ptswidth_top = np.linalg.norm(tr - tl)width_bottom = np.linalg.norm(br - bl)height_left = np.linalg.norm(bl - tl)height_right = np.linalg.norm(br - tr)max_width = max(int(width_top), int(width_bottom))max_height = max(int(height_left), int(height_right))# 目标四点坐标dst = np.array([[0, 0],[max_width - 1, 0],[max_width - 1, max_height - 1],[0, max_height - 1]], dtype="float32")# 计算变换矩阵并应用M = cv2.getPerspectiveTransform(ordered_pts, dst)warped = cv2.warpPerspective(img, M, (max_width, max_height))return warpedreturn None  # 未找到四边形def rotate_correction(image):"""旋转校正:检测直线角度并旋转图像"""gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)edges = cv2.Canny(gray, 50, 150, apertureSize=3)lines = cv2.HoughLines(edges, 1, np.pi / 180, 200)  # 霍夫变换检测直线if lines is None:return imageangles = []for line in lines:rho, theta = line[0]# 计算直线角度(转换为度数)angle = np.degrees(theta) - 90  # 转换为相对于垂直线的角度angles.append(angle)median_angle = np.median(angles)# 仅在校正角度超过阈值时旋转if abs(median_angle) > 0.5:(h, w) = image.shape[:2]center = (w // 2, h // 2)M = cv2.getRotationMatrix2D(center, median_angle, 1.0)corrected = cv2.warpAffine(image, M, (w, h), borderMode=cv2.BORDER_REPLICATE)return correctedreturn imagedef main(image_path):# 读取图像img = cv2.imread(image_path)if img is None:print("Error: 无法读取图像")return# 可选:镜头畸变校正(需提供相机参数)# img = cv2.undistort(img, camera_matrix, dist_coeffs)# 透视校正perspective_img = perspective_correction(img)if perspective_img is not None:# 旋转校正final_img = rotate_correction(perspective_img)else:# 直接旋转校正原图final_img = rotate_correction(img)# 显示结果cv2.imshow("Original", img)if perspective_img is not None:cv2.imshow("Perspective Corrected", perspective_img)cv2.imshow("Final Corrected", final_img)cv2.waitKey(0)cv2.destroyAllWindows()if __name__ == "__main__":import sysif len(sys.argv) != 2:print("Usage: python image_correction.py <image_path>")else:main(sys.argv[1])

算法说明:

  1. 透视校正

    • 通过自适应阈值处理和轮廓检测找到图像中的四边形区域
    • 使用透视变换将四边形区域转换为正视图
    • 适用于文档、标牌等包含明显四边形结构的图像
  2. 旋转校正

    • 使用Canny边缘检测和霍夫变换检测直线
    • 计算直线角度的中位数作为图像倾斜角度
    • 根据倾斜角度旋转图像实现水平校正
  3. 畸变校正

    • 代码中已预留镜头畸变校正接口(需取消注释并提供相机标定参数)
    • 使用cv2.undistort()函数实现径向和切向畸变校正

使用说明:

  1. 安装依赖:pip install opencv-python numpy
  2. 运行命令:python image_correction.py <图片路径>
  3. 按任意键关闭窗口

该算法能有效校正大多数文档类图像的透视畸变和旋转问题,实际效果取决于图像中的特征检测质量,可通过调整阈值参数优化结果。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com