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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值