python - 试图从颗粒状图像中提取文本
问题描述
我一直在尝试从颗粒状图像中提取文本,这是原始图像
这是我用来尝试处理此图像的代码
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('img_gray',img_gray)
cv2.waitKey(0)
#img_bin = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 21, 15) # 21 and 15 need to be set for image12
img_bin = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 27, 15) # 21 and 15 need to be set for image12
cv2.imshow('img_bin',img_bin)
cv2.waitKey(0)
fig, axs = plt.subplots(3)
axs[0].imshow(img_gray, cmap="gray")
axs[1].imshow(img_bin, cmap="gray")
# Merge dots into characters using erosion
kernel = np.ones((5, 5), np.uint8)
#kernel = np.ones((15, 15), np.uint8)
img_eroded = cv2.erode(img_bin, kernel, iterations=1)
axs[2].imshow(img_eroded, cmap="gray")
cv2.imshow('img_eroded',img_bin)
cv2.waitKey(0)
fig.show()
# Obtain string using psm 8 (treat the image as a single word)
ocr_string = pytesseract.image_to_string(img_eroded, lang= 'eng', config="--psm 6")
return ocr_string
这是将背景转为灰色后的灰色图像 img_gray
这是应用自适应阈值后的图像 在 此处输入图像描述
这是侵蚀后的图像 在 此处输入图像描述
在最终图像 ( img_eroded
) 中,实际文本周围仍有很多点,这可能导致image_to_string
函数抛出一些垃圾值。有没有办法进一步处理这个图像,或者改进现有的代码来提取文本Pac=2665.7W
解决方案
推荐阅读
- flutter - 由于依赖项不支持 null 安全性,因此无法以可靠的 null 安全性运行
- php - PHP PDFMerger - 未捕获的错误:找不到类“PDFMerger\exception”
- python - 我如何将我的 matplotlib 动画图与烧瓶 web ui 集成
- vue.js - 用玩笑替换默认导出对象
- java - 在Java中,如果对象包含相同对象类型的列表,如何对其进行递归排序
- php - 如果值为空,则移动设备上的滑块图像更改选择桌面图像
- python - imshow 图片太小
- android - Google Android ML Kit 在对象检测期间仅提供类别
- python - 将时间戳转换为日期并仅将日期保留在 pandas 列内的列表中
- reactjs - 导入不起作用并且找不到 React 文件