学习笔记4(opencv and python 透视变换(鸟瞰))

您所在的位置:网站首页 鸟瞰照片主题怎么写 学习笔记4(opencv and python 透视变换(鸟瞰))

学习笔记4(opencv and python 透视变换(鸟瞰))

2024-07-14 15:00| 来源: 网络整理| 查看: 265

@ 鸟瞰图像的实现,用四边顶点坐标来进行来进行透视变换

相关opencv函数 1.cv2.getPerspectiveTransform(src,M)

src表示原图像的四边顶点的坐标,M表示为要求变换的四边顶点的坐标,最后得到一个3x3的变换矩阵

2.cv2.warpPerspective(src,M,dsize(height,width)

src为输入的图像,M为cv2.getperpectiveTransform()函数的到的变换矩阵, src为输入图像,M为cv2.getperpectiveTransform()函数的到的变换矩阵),dsize为输出图像的大小 如果还是不懂的话,看这个链接!!!

先看一下我的实现结果

在这里插入图片描述

上代码 import numpy as np import cv2 def order_points(pts): #进行初始化点的位置,左上、右上、左下、右下 rect = np.zeros((4, 2), dtype = "float32") #采取四个点的x+y的和,以最小的和为左上的点,x+y最大为右下的点 s = pts.sum(axis = 1) rect[0] = pts[np.argmin(s)] rect[2] = pts[np.argmax(s)] #定义一左下点和右上点的位置,用|x-y|表示,小的为右上,大的为左下 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 #重新计算新的图像的宽度和高度 widthA = np.sqrt(np.sum((br - bl) ** 2)) widthB = np.sqrt(np.sum((tr - tl) ** 2)) maxWidth = max(int(widthA), int(widthB)) heightA = np.sqrt(np.sum((tr - br) ** 2)) heightB = np.sqrt(np.sum((tl - bl) ** 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) print(M) warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight)) return warped image = cv2.imdecode(np.fromfile(r"E:/学习用的图片素材/receipt1.jpg"),-1) #这里没有用imread的原因是因为我的图片路径有中文,所以用的这种方法 pts = np.array([(153,119),(376,159),(30,428),(300,492)], dtype = "float32") #使用坐标完成鸟瞰的图像变化,这里的pts里的坐标,最好就是我们输入图像的四边顶点的坐标 warped = four_point_transform(image, pts) # show the original and warped images cv2.imshow("Original", image) cv2.imshow("Warped", warped) cv2.waitKey(0) 最后的结果显示

1.先看一下M吧

[[ 1.32412921e+00 5.27080561e-01 -2.65314357e+02] [-2.49406708e-01 1.39044240e+00 -1.27303419e+02] [ 4.05004587e-05 8.24014712e-04 1.00000000e+00]] 2.最后结果 在这里插入图片描述

最近在看Adrian Rosebrock的博客,以后的笔记也是看了他的博客所记录的



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3