openCV+python实现倾斜校正⽂章⽬录
⽰例代码
import cv2
import imutils
import numpy as np
img = cv2.imread(r'C:\Users\ml\Desktop\0.png')
result3 = py()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray,(5,5),0)
dilate = cv2.dilate(blurred, StructuringElement(cv2.MORPH_RECT,(3,3)))
edged = cv2.Canny(dilate,30,120,3)# 边缘检测
cnts = cv2.py(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)# 轮廓检测cnts = cnts[0]if imutils.is_cv2()else cnts[1]# 判断是opencv2还是opencv3
docCnt =None
if len(cnts)>0:
cnts =sorted(cnts, urArea, reverse=True)# 根据轮廓⾯积从⼤到⼩排序
for c in cnts:
peri = cv2.arcLength(c,True)# 计算轮廓周长
approx = cv2.approxPolyDP(c,0.02*peri,True)# 轮廓多边形拟合
# 轮廓为4个点表⽰到纸张
if len(approx)==4:
docCnt = approx
break
print(docCnt)
box =[docCnt[0][0],docCnt[1][0],docCnt[2][0],docCnt[3][0]]
rectangle_L =int(np.sqrt(pow((box[0][0]-box[1][0]),2)+pow((box[0][1]-box[1][1]),2)))
rectangle_W =int(np.sqrt(pow((box[0][0]-box[3][0]),2)+pow((box[0][1]-box[3][1]),2)))
print(rectangle_L)
print(rectangle_W)
src = np.float32(docCnt)
dst = np.float32([[0,0],[0, rectangle_L],[rectangle_W, rectangle_L],[rectangle_W,0]])
m = PerspectiveTransform(src, dst)
result = cv2.warpPerspective(result3, m,(rectangle_W, rectangle_L))
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
from imutils.perspective import four_point_transform
img = cv2.imread(r'C:\Users\ml\Desktop\0.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# 转化为灰度图,加快处理速度
binary = dianBlur(gray,7)# 中值滤波
cv2.imshow('binary',binary)rectangle函数opencv
# blurred = cv2.GaussianBlur(gray, (5, 5), 0)
# cv2.imshow('blurred',blurred)
dilate = cv2.dilate(binary, StructuringElement(cv2.MORPH_RECT,(3,3)))
cv2.imshow('dilate',dilate)
edged = cv2.Canny(dilate,30,120,3)# 边缘检测
cv2.imshow('edged',edged)
cnts = cv2.py(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)# 轮廓检测cnts = cnts[0]if imutils.is_cv2()else cnts[1]# 判断是opencv2还是opencv3
docCnt =None
if len(cnts)>0:
cnts =sorted(cnts, urArea, reverse=True)# 根据轮廓⾯积从⼤到⼩排序
for c in cnts:
peri = cv2.arcLength(c,True)# 计算轮廓周长
approx = cv2.approxPolyDP(c,0.02*peri,True)# 轮廓多边形拟合
# 轮廓为4个点表⽰到纸张
if len(approx)==4:
docCnt = approx
break
print(docCnt)
result = four_point_transform(img, shape(4,2))# 对原始图像进⾏四点透视变换
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
ix,iy =0,0
box =[]
img = cv2.imread(r'C:\Users\ml\Desktop\tng.jpeg')
# mouse callback function
def draw_circle(event,x,y,flags,param):
global ix, iy
if event == cv2.EVENT_LBUTTONDBLCLK:# 设置⿏标事件
ix, iy = x, y
cv2.circle(img,(x,y),6,(0,0,255),-1)
cv2.imshow('image', img)
cv2.namedWindow('image')
cv2.imshow('image', img)
cv2.setMouseCallback('image',draw_circle)
print(ix,iy)
box.append((ix,iy))
print(box)
i=1
'''
while(1):
i=i+1
cv2.setMouseCallback('image',draw_circle)
if operator.eq(box[i], box[1])==True:
break
'''
cv2.waitKey(0)
cv2.destroyAllWindows()
函数列表
函数作⽤
cv2.GaussianBlur()⾼斯滤波:去除噪⾳,使图像变得平滑cv2.cvtColor()颜⾊空间转换
cv2.Canny()边缘检测cv2.approxPolyDP(curve, epsilon, closed)多边形逼近
参考资料

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。