欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

OpenCV-文档扫描OCR识别-04

发布时间:2024/9/15 编程问答 37 豆豆
生活随笔 收集整理的这篇文章主要介绍了 OpenCV-文档扫描OCR识别-04 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

# 导入工具包 import numpy as np import argparse import cv2 def order_points(pts):# 初始化4个坐标点的矩阵rect = np.zeros((4, 2), dtype = "float32")# 按顺序找到对应坐标0123分别是 左上,右上,右下,左下# 计算左上,右下print("pts :\n ",pts)s = pts.sum(axis = 1)# 沿着指定轴计算第N维的总和print("s : \n",s)rect[0] = pts[np.argmin(s)]# 即pts[1]rect[2] = pts[np.argmax(s)]# 即pts[3]print("第一次rect : \n",rect)# 计算右上和左下diff = np.diff(pts, axis = 1)# 沿着指定轴计算第N维的离散差值print("diff : \n",diff)rect[1] = pts[np.argmin(diff)]# 即pts[0]rect[3] = pts[np.argmax(diff)]# 即pts[2]print("第二次rect :\n ",rect)return rect def 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],# 防止出错,-1[maxWidth - 1, maxHeight - 1],[0, maxHeight - 1]], dtype = "float32")# 计算变换矩阵 (平移+旋转+翻转),其中M = cv2.getPerspectiveTransform(rect, dst)# (原坐标,目标坐标)print("M:",M)print("M.shape:",M.shape)warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))# 返回变换后结果return warped 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:# 无w有h时r = height / float(h)# 新h与旧h的比例为rdim = (int(w * r), height)# 让w也乘以这个比例,得到新welse:r = width / float(w)dim = (width, int(h * r))resized = cv2.resize(image, dim, interpolation=inter)return resized if __name__ == "__main__":# 读取输入image = cv2.imread("receipt.jpg")# resize 坐标也会相同变化ratio = image.shape[0] / 500.0orig = image.copy()image = resize(orig, height = 500)# 同比例变化:h指定500,w也会跟着变化# 预处理gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)gray = cv2.GaussianBlur(gray, (5, 5), 0)edged = cv2.Canny(gray, 75, 200)# 展示预处理结果print("STEP 1: 边缘检测")cv2.imshow("Image", image)cv2.imshow("Edged", edged)cv2.waitKey(0)cv2.destroyAllWindows()# 轮廓检测cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[1]# cnts中可检测到许多个轮廓,取前5个最大面积的轮廓cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]# 遍历轮廓for c in cnts:# C表示输入的点集# 计算轮廓近似peri = cv2.arcLength(c, True)# epsilon表示从原始轮廓到近似轮廓的最大距离,它是一个准确度参数# True表示封闭的approx = cv2.approxPolyDP(c, 0.02 * peri, True)print("approx:\n",approx)print("approx.shape",approx.shape)# 4个点的时候就拿出来,screenCnt是这4个点的坐标if len(approx) == 4:# 近似轮廓得到4个点,意味着可能得到的是矩形screenCnt = approx# 并且最大的那个轮廓是很有可能图像的最大外围break


# 展示结果print("STEP 3: 变换")cv2.imshow("Original", resize(orig, height = 650))cv2.imshow("Scanned", resize(ref, height = 650))cv2.waitKey(0)

安装工具包

https://digi.bib.uni-mannheim.de/tesseract/
配置环境变量如E:\Program Files (x86)\Tesseract-OCR
tesseract -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()) 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)

总结

以上是生活随笔为你收集整理的OpenCV-文档扫描OCR识别-04的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。