wendang

#导入工具包
import cv2
import argparse
import func
import pytesseract
#设置参数
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,
 help = "Path to the image to be scanned")
args = vars(ap.parse_args())

#读取图像
image = cv2.imread(args["image"])
#复制图像
orig = image.copy()
#修改图像大小
image = cv2.resize(image,(0,0),fx=0.2,fy=0.2)
#图像预处理
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)#灰度图
gray = cv2.GaussianBlur(gray,(5,5),0)#高斯滤波
edg =cv2.Canny(gray,100,255)#边缘检测

#展示处理结果
print("SETP 1:边缘检测")
func.cv_show("edg", edg)

#轮廓检测
cnt,hierarchy =cv2.findContours(edg,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)#只保留顶点
cnts =sorted(cnt,key = cv2.contourArea,reverse = True)[:5]#轮廓由大到小排序,只保留五个

#遍历轮廓
for c in cnts:
 #计算轮廓近似
 peri = cv2.arcLength(c,True)
 # C表示输入的点集
 # epsilon表示从原始轮廓到近似轮廓的最大距离,它是一个准确度参数
 # True表示封闭的
 approx = cv2.approxPolyDP(c, 0.02 * peri, True)#轮廓近似

 # 4个点的时候就拿出来
 if len(approx) == 4:
  screenCnt = approx
  break

#展示结果
print("SETP 2:获取轮廓")
cv2.drawContours(image,[screenCn

你可能感兴趣的:(python)