scan_file.py
import numpy as npimport argparseimport cv2# 设置参数ap = argparse.ArgumentParser()ap.add_argument("-i", "--image", required=True,help="Path to the image to be scanned")args = vars(ap.parse_args())print(args)# 绘图展示def cv_show(name, img):cv2.imshow(name, img)cv2.waitKey(0)cv2.destroyAllWindows()# 读取图片数据image = cv2.imread(args['image'])# cv_show('image',image)print(image.shape)# 将要对图像进行大小变化,先保存一下变化率ratio = image.shape[0] / 500.0# 获得原始图像orig = image.copy()# 按比例变化图像大小def resize(image, width=None, height=None, inter=cv2.INTER_AREA):dim = None(h, w) = image.shape[:2]if width is None and height is None:return imageif width is None:r = height / float(h)dim = (int(w * r), height)print('width is None', dim)else:r = width / float(w)dim = (width, int(h * r))print('height is None', dim)resized = cv2.resize(image, dim, interpolation=inter)return resizedimage = resize(orig, height=500)print(image.shape)# cv_show('image',image)# 对图像进行预处理操作# 转灰度图gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)cv_show('gray', gray)# 去噪操作gray = cv2.GaussianBlur(gray, (5, 5), 0)# cv_show('gray',gray)# 进行Canny边缘检测edged = cv2.Canny(gray, 75, 200)print(edged.shape)# cv_show('Canny_edged',edged)# 展示预处理结果print("STEP 1: 边缘检测")cv_show('image', image)cv_show('Canny_edged', edged)# 再进行轮廓检测# RETR_LIST:检索所有的轮廓,并将其保存到一条链表当中# CHAIN_APPROX_SIMPLE:压缩水平的、垂直的和斜的部分,也就是,函数只保留他们的终点部分。cnts, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)draw_img = image.copy()res = cv2.drawContours(draw_img, cnts, -1, (0, 0, 255), 2)cv_show('find_Cnts', draw_img)# 根据轮廓面积排序选择最大的五个轮廓cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5]draw_img = image.copy()res = cv2.drawContours(draw_img, cnts, -1, (0, 0, 255), 2)cv_show('find_Cnts', res)# 遍历这五个最大的轮廓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 = approxbreak# 展示结果print("STEP 2: 获取轮廓")cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2)cv_show('Get_Cnts', image)# 透视变换def order_points(pts):# 一共4个坐标点rect = np.zeros((4, 2), dtype="float32")# 按顺序找到对应坐标0123分别是 左上,右上,右下,左下# 计算左上,右下s = pts.sum(axis=1)rect[0] = pts[np.argmin(s)]rect[2] = pts[np.argmax(s)]# 计算右上和左下diff = np.diff(pts, axis=1)rect[1] = pts[np.argmin(diff)]rect[3] = pts[np.argmax(diff)]return rectdef four_point_transform(image, pts):# 获取输入坐标点rect = order_points(pts)(tl, tr, br, bl) = rect# 计算输入的w和h值widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))maxWidth = max(int(widthA), int(widthB))heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))maxHeight = max(int(heightA), int(heightB))# 变换后对应坐标位置dst = np.array([[0, 0],[maxWidth - 1, 0],[maxWidth - 1, maxHeight - 1],[0, maxHeight - 1]], dtype="float32")# 计算变换矩阵# 只能是矩形形状较好M = cv2.getPerspectiveTransform(rect, dst)warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))# 返回变换后结果return warpedwarped =four_point_transform(orig, screenCnt.reshape(4, 2) * ratio)# cv_show("warped",warped)# 二值处理warped = cv2.cvtColor(warped,cv2.COLOR_BGR2GRAY)ref = cv2.threshold(warped,100,255,cv2.THRESH_BINARY)[1]# cv_show('Binary_warped',warped)# 保存图像cv2.imwrite('scan.jpg',ref)# 展示结果print("STEP 3: 变换")cv_show("Original", resize(orig, height = 650))cv_show("Scanned", resize(ref, height = 650))
test.py
# https://digi.bib.uni-mannheim.de/tesseract/
# 配置环境变量如E:\Program Files (x86)\Tesseract-OCR
# -v进行测试
# tesseract XXX.png 得到结果
# pip install pytesseract
# anaconda lib site-packges pytesseract pytesseract.py
# tesseract_cmd 修改为绝对路径即可
from PIL import Image
import pytesseract
import cv2
import ospreprocess = 'blur' #threshimage = cv2.imread('scan.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)if preprocess == "thresh":gray = cv2.threshold(gray, 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]if preprocess == "blur":gray = cv2.medianBlur(gray, 3)# filename = "{}.png".format(os.getpid())
filename = "test.png"
cv2.imwrite(filename, gray)text = pytesseract.image_to_string(Image.open(filename))
print(text)
os.remove(filename)cv2.imshow("Image", image)
cv2.imshow("Output", gray)
cv2.waitKey(0)
记得安装谷歌的tesseract-ocr-setup-4.00.00dev.exe