# 图像校正-透视变换

#Python图像校正

##实现内容：图像校正

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

1. 将图像转成灰度图

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

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)


03-29 11万+

06-18 1732

05-09 2万+

01-03 885

10-29 1万+

09-15 367

12-10 655

10-30 1万+

09-13 1万+

09-03 1万+

03-20 966

01-14 5774

11-14 208

02-15 2014

09-08 846

08-11 1万+

06-24 3848

03-20 4506

#### 网易2018校招笔试编程题

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客