一啰劲、安裝
二法牲、使用
from PIL import Image
import pytesseract
img = cv2.imread('aaa.png')
text = pytesseract.image_to_string(img, lang="chi_sim")
print(text)
去除噪點(diǎn)
from PIL import Image
import pytesseract
import cv2
img = cv2.imread('aaa.png')
grayimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # # 轉(zhuǎn)為灰度圖
_, bwimg = cv2.threshold(grayimg, 127, 255, cv2.THRESH_BINARY) # 二值化
contours, _ = cv2.findContours(bwing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 尋找輪廓
# 方法一
# 選出面積較小區(qū)域
noise = []
for contour in contours:
area = cv2.contourArea(contour)
if area <= 40:
noise.append(contour)
cv2.fillPoly(bwimg, noise, 0) # 刪除點(diǎn)填充為 黑色
# 方法 二
kernel = np.ones((3, 11), np.uint8)
dilation = cv2.dilate(bwimg, kernel, iterations=1) # 膨脹
# 膨脹后連在一起
contours, _ = cv2.findContours(dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 尋找輪廓
text_area = max(contours, key=lambda x: cv2.contourArea(x))
# 最小外接矩陣
rect = cv2.minAreaRect(text_area)
box = cv2.boxPoints(rect)
box = np.int0(box)
# 矩陣外填充為黑色
stencil = np.zeros(bwimg.shape).astype(bwimg.dtype)
color = [255, 255, 255]
cv2.fillPoly(stencil, [box], color)
result = cv2.bitwise_and(bwimg, stencil)
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者