图像透视投影变换 四边形——标准矩形

#图像处理# 将四边形–透视投影变换为标准矩形

1、什么是透视变换?
透视变换(Perspective Transformation)是将图片投影到一个新的视平面(Viewing Plane),也称作投影映射(Projective Mapping)。
2、透视原理有很多文章介绍读者可自行查询理解。
cv2.getPerspectiveTransform 透视变换
在这里插入图片描述

先来效果图
Before:
在这里插入图片描述
After:
在这里插入图片描述
(由于此处只在意所选四边形的变换实现,没有剔除新透视视角下标准矩阵中周围的图像)

代码实现:


```python
# 功能:将图像中某一四边形变换为矩阵

import cv2
import numpy as np

img = cv2.imread('C:/Users/Administrator/Desktop/testphoto/computer.jpg')
#获取源图像宽和高
w = img.shape[0]
h = img.shape[1]
#源图像中四边形坐标点(获取坐标点方法可参照我上篇博文)
point1 = np.array([[320,132],[1240,111],[414,800],[1351,738]],dtype = "float32")
#转换后得到矩形的坐标点
point2 = np.array([[0,0],[320,0],[0,180],[320,180]],dtype = "float32")
# point2 = np.array([[0,180],[320,180],[0,0],[320,0]],dtype = "float32")
M = cv2.getPerspectiveTransform(point1,point2)
out_img = cv2.warpPerspective(img,M,(w,h))
cv2.imshow("img",out_img)
cv2.waitKey(0)
以上代码仅供参考
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
鱼眼图像畸变校正的透视变换原理如下: 1. 首先,需要确定图像中的两个灭点,这可以通过球面透视投影得到。 2. 然后,根据灭点的位置,确定仿射变换矩阵,将图像进行仿射变换。 3. 最后,对变换后的图像进行裁剪,得到最终的校正图像。 下面是一个使用OpenCV进行鱼眼图像畸变校正的Python代码示例: ```python import cv2 import numpy as np # 读取鱼眼图像 img = cv2.imread('fisheye.jpg') # 确定灭点 h, w = img.shape[:2] K = np.array([[w, 0, w/2], [0, w, h/2], [0, 0, 1]]) D = np.array([0, 0, 0, 0]) newK = cv2.fisheye.estimateNewCameraMatrixForUndistortRectify(K, D, (w, h), np.eye(3)) map1, map2 = cv2.fisheye.initUndistortRectifyMap(K, D, np.eye(3), newK, (w, h), cv2.CV_16SC2) img_undistorted = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT) # 确定仿射变换矩阵 src_points = np.float32([[0, 0], [w-1, 0], [0, h-1], [w-1, h-1]]) dst_points = np.float32([[w/4, h/4], [w*3/4, h/4], [0, h], [w, h]]) M = cv2.getPerspectiveTransform(src_points, dst_points) # 进行仿射变换 img_transformed = cv2.warpPerspective(img_undistorted, M, (w, h)) # 裁剪图像 img_corrected = img_transformed[int(h/4):int(h*3/4), int(w/4):int(w*3/4)] # 显示结果 cv2.imshow('Original Image', img) cv2.imshow('Undistorted Image', img_undistorted) cv2.imshow('Transformed Image', img_transformed) cv2.imshow('Corrected Image', img_corrected) cv2.waitKey(0) cv2.destroyAllWindows() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值