图像校正-透视变换

#Python图像校正

##实现内容:图像校正

一张图像中有一张A4纸,通过图像处理的方法将其校正

输入图像:
这里写图片描述

输出图像:
这里写图片描述

##使用方法(Python):基于边缘检测

  1. 将图像转成灰度图

先将input.jpg高斯模糊,再进行灰度化处理,方便转换。

img = cv2.GaussianBlur(img,(3,3),0)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

输出结果:
这里写图片描述
2. 边缘检测(检测出图像的边缘信息)

edges = cv2.Canny(gray,50,250,apertureSize = 3)
cv2.imwrite("canny.jpg", edges)

输出结果:

这里写图片描述
3. 通过霍夫变换得到A4纸边缘(可以看到A4纸中还有A4纸外有一些线,可以通过霍夫变换来去掉这些线)

lines = cv2.HoughLinesP(edges,1,np.pi/180,50,minLineLength=90,maxLineGap=10)
for x1,y1,x2,y2 in lines[0]:
    cv2.line(result1,(x1,y1),(x2,y2),(0,0,255),1)

输出结果:

这里写图片描述
4. 通过A4纸的边计算A4纸的四个角点
记录四条边中每条边的两个点,分别求出四条边的直线函数,然后两两直线联合取四个交点,得到:
(207,151),(517,285),(17,601),(343,731)

cv2.circle(result2,(207,151),2,(0,255,0),2)
cv2.circle(result2,(517,285),2,(0,255,0),2)
cv2.circle(result2,(17,601),2,(0,255,0),2)
cv2.circle(result2,(343,731),2,(0,255,0),2)

输入结果:
这里写图片描述

  1. 根据四个角点做透视变换
src = np.float32([[207, 151], [517, 285], [17, 601], [343, 731]])
dst = np.float32([[0, 0], [337, 0], [0, 488], [337, 488]])
m = cv2.getPerspectiveTransform(src, dst)
result = cv2.warpPerspective(result3, m, (337, 488))

输出结果:

这里写图片描述
完整代码:

import cv2
import numpy as np

img = cv2.imread('input.jpg')
result1 = img.copy()
result2 = img.copy()
result3 = img.copy()

img = cv2.GaussianBlur(img,(3,3),0)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

edges = cv2.Canny(gray,50,150,apertureSize = 3)
cv2.imwrite("canny.jpg", edges)
#hough transform
lines = cv2.HoughLinesP(edges,1,np.pi/180,50,minLineLength=90,maxLineGap=10)
for x1,y1,x2,y2 in lines[0]:
    cv2.line(result1,(x1,y1),(x2,y2),(0,0,255),1)
    print (x1,y1)
    print (x2,y2)
cv2.circle(result2,(207,151),5,(0,255,0),5)
cv2.circle(result2,(517,285),5,(0,255,0),5)
cv2.circle(result2,(17,601),5,(0,255,0),5)
cv2.circle(result2,(343,731),5,(0,255,0),5)

cv2.imwrite("result1.jpg", result1)
cv2.imwrite("result2.jpg", result2)

src = np.float32([[207, 151], [517, 285], [17, 601], [343, 731]])
dst = np.float32([[0, 0], [337, 0], [0, 488], [337, 488]])
m = cv2.getPerspectiveTransform(src, dst)
result = cv2.warpPerspective(result3, m, (337, 488))
cv2.imwrite("result.jpg", result)
cv2.imshow("result", result)
cv2.waitKey(0)
鱼眼图像畸变校正的透视变换原理如下: 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() ```
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值