步骤:边缘检测+计算轮廓+变换+OCR
def show(name, img):
cv2.imshow(name, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread('tip.png')
show('img', img)
img = cv2.resize(img, (500, int((500 * img.shape[0])/img.shape[1])))
# 预处理
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
# 边缘检测
edged = cv2.Canny(gray, 75, 200)
show('edged', edged)
# 轮廓处理
cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[0]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5] # reverse=True表示倒序(从大到小)
for c in cnts:
# 计算轮廓近似
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02*peri, True)
# 如果近似为一个矩形
if len(approx) == 4:
screenCnt = approx
break
cv2.drawContours(img, [screenCnt], -1, (0, 255, 0),2)
show('img', img)
# 透视变换
def four_point_transform(img, pts):
p = sorted(pts, key=lambda pts: pts[0, 1], reverse=False)
p = sorted(p, key=lambda p: p[0, 0], reverse=False)
tl, bl, tr, br = [i[0] for i in p]
# 计算边长度
wA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
wB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
mw = max(int(wB), int(wA))+1
hA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
hB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
mh = max(int(hB), int(hA))+1
# 变换后对应坐标位置
dst = np.array([
[0, 0],
[mw-20, 0],
[mw-20, mh-20],
[0, mh-20]
], dtype="float32")
# 计算变换矩阵
rect = np.zeros((4,2), dtype="float32")
rect[0] = tl.astype(float).tolist()
rect[1] = tr.astype(float).tolist()
rect[2] = br.astype(float).tolist()
rect[3] = bl.astype(float).tolist()
M = cv2.getPerspectiveTransform(rect, dst)
warped = cv2.warpPerspective(img, M, (mw, mh))
return warped
warped = four_point_transform(orig, screenCnt)
show('warped', warped)
# 二值处理
warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
ref = cv2.threshold(warped, 180, 255, cv2.THRESH_BINARY)[1]
show('ref', ref)
OCR识别
import pytesseract
from PIL import Image
cv2.imwrite("ocr.png", ref)
# 下载地址:https://github.com/UB-Mannheim/tesseract/wiki
# tesseract.exe的路径
pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files\Tesseract-OCR\\tesseract.exe'
# 指定训练集的路径
tessdata_dir_config = r'--tessdata-dir "C:\Program Files\Tesseract-OCR\tessdata"'
test =pytesseract.image_to_string(Image.open("ocr.png"))
print(test)
识别结果:
文章来源:https://www.toymoban.com/news/detail-799945.html
完整代码文章来源地址https://www.toymoban.com/news/detail-799945.html
import cv2 # opencv读取的格式是BGR
import matplotlib.pyplot as plt
import numpy as np
def show(name, img):
cv2.imshow(name, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread('tip.png')
# show('img', img)
ratio = img.shape[0] / 500.0
orig = img.copy()
img = cv2.resize(img, (500, int((500 * img.shape[0])/img.shape[1])))
# 预处理
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
# 边缘检测
edged = cv2.Canny(gray, 75, 200)
# show('edged', edged)
# 轮廓处理
cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[0]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5] # reverse=True表示倒序(从大到小)
for c in cnts:
# 计算轮廓近似
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02*peri, True)
# 如果近似为一个矩形
if len(approx) == 4:
screenCnt = approx
break
cv2.drawContours(img, [screenCnt], -1, (0, 255, 0), 2)
# show('img', img)
# 透视变换
def four_point_transform(img, pts):
p = sorted(pts, key=lambda pts: pts[0, 1], reverse=False)
p = sorted(p, key=lambda p: p[0, 0], reverse=False)
tl, bl, tr, br = [i[0] for i in p]
# 计算边长度
wA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
wB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
mw = max(int(wB), int(wA))+1
hA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
hB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
mh = max(int(hB), int(hA))+1
# 变换后对应坐标位置
dst = np.array([
[0, 0],
[mw-20, 0],
[mw-20, mh-20],
[0, mh-20]
], dtype="float32")
# 计算变换矩阵
rect = np.zeros((4,2), dtype="float32")
rect[0] = tl.astype(float).tolist()
rect[1] = tr.astype(float).tolist()
rect[2] = br.astype(float).tolist()
rect[3] = bl.astype(float).tolist()
M = cv2.getPerspectiveTransform(rect, dst)
warped = cv2.warpPerspective(img, M, (mw, mh))
return warped
warped = four_point_transform(orig, screenCnt)
# show('warped', warped)
# 二值处理
warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
ref = cv2.threshold(warped, 180, 255, cv2.THRESH_BINARY)[1]
show('ref', ref)
import pytesseract
from PIL import Image
cv2.imwrite("ocr.png", ref)
# 下载地址:https://github.com/UB-Mannheim/tesseract/wiki
# tesseract.exe的路径
pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files\Tesseract-OCR\\tesseract.exe'
# 指定训练集的路径
tessdata_dir_config = r'--tessdata-dir "C:\Program Files\Tesseract-OCR\tessdata"'
test =pytesseract.image_to_string(Image.open("ocr.png"))
print(test)
到了这里,关于OpenCV(九)--文字扫描OCR识别的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!