一、安装
二、使用
from PIL import Image
import pytesseract
img = cv2.imread('aaa.png')
text = pytesseract.image_to_string(img, lang="chi_sim")
print(text)
去除噪点
from PIL import Image
import pytesseract
import cv2
img = cv2.imread('aaa.png')
grayimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # # 转为灰度图
_, bwimg = cv2.threshold(grayimg, 127, 255, cv2.THRESH_BINARY) # 二值化
contours, _ = cv2.findContours(bwing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 寻找轮廓
# 方法一
# 选出面积较小区域
noise = []
for contour in contours:
area = cv2.contourArea(contour)
if area <= 40:
noise.append(contour)
cv2.fillPoly(bwimg, noise, 0) # 删除点填充为 黑色
# 方法 二
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)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。