文档扫描OCR识别-1(python)
工具包導入
import numpy as np
import cv2
1
2
函數(shù)設定
#?四邊形坐標求解
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 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],
[maxWidth - 1, maxHeight - 1],
[0, maxHeight - 1]], dtype="float32")
#?計算變換矩陣 ?透視變換?--?二維升三維再降維 ?齊次坐標?:?用?N+1?維來代表?N?維坐標?[kx,ky,k]
M = cv2.getPerspectiveTransform(rect, dst)
warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
#?返回變換后結(jié)果
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 image
if width is None:
r = height / float(h)
dim = (int(w * r), height)
else:
r = width / float(w)
dim = (width, int(h * r))
resized = cv2.resize(image, dim, interpolation=inter)
return resized
讀取輸入
image = cv2.imread('images/receipt.jpg')
1
邊緣檢測
ratio = image.shape[0] / 500.0
# image.shape[0],?圖片垂直尺寸
# image.shape[1],?圖片水平尺寸
# image.shape[2],?圖片通道數(shù)
orig = image.copy()
image = resize(orig, height=500) ?#?等比例縮放
#?預處理
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0) ?#?去除噪音點
edged = cv2.Canny(gray, 75, 200) ?#?邊緣檢測
#?展示預處理結(jié)果
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 = sorted(cnts, key=cv2.contourArea, reverse=True)[:5] ?#?選取前五個?,?最大的輪廓
#?遍歷輪廓
for c in cnts:
#?計算輪廓近似
peri = cv2.arcLength(c, True) ?# retval=cv.arcLength(curve, closed) retval?返回值,輪廓的周長?closed?曲線是是否閉合
# C?表示輸入的點集
# epsilon?表示從原始輪廓到近似輪廓的最大距離,外匯跟單gendan5.com它是一個準確度參數(shù)
# True?表示封閉的
approx = cv2.approxPolyDP(c, 0.02 * peri, True) ?#?輪廓?,?輪廓精度?,?越小可能是多邊形?,?越大可能是矩形
# 4?個點的時候就拿出來
if len(approx) == 4:
screenCnt = approx
# print(screenCnt) ?#?四個點的坐標
break
#?展示結(jié)果
print("STEP 2:?獲取輪廓?")
cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2)
cv2.imshow("Outline", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
變換
#?透視變換
warped = four_point_transform(orig, screenCnt.reshape(4, 2) * ratio)
#?二值處理
warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
ref = cv2.threshold(warped, 100, 255, cv2.THRESH_BINARY)[1]
cv2.imwrite('scan.jpg', ref)
#?展示結(jié)果
print("STEP 3:?變換?")
cv2.imshow("Original", resize(orig, height = 650))
cv2.imshow("Scanned", resize(ref, height = 650))
cv2.waitKey(0)
總結(jié)
以上是生活随笔為你收集整理的文档扫描OCR识别-1(python)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Sqlserver系统数据库和用户数据库
- 下一篇: MySQL中的pid与socket是什么