【问题标题】:opencv python - remove small points noise in binarized imageopencv python - 去除二值化图像中的小点噪声
【发布时间】:2021-11-18 20:44:54
【问题描述】:

我正在做一个文档阅读器,它将其中的所有文本解析为谷歌电子表格,这个脚本应该可以节省我的工作时间,问题是二进制图像有很多噪音(文本周围的小点)这让 pytesseract 感到困惑。我怎样才能消除这种噪音?我用来二值化图像的代码是:

import pytesseract
import cv2
import numpy as np
import os
import re
import argparse

#binarization of images
def binarize(img):
    #convert image to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    #apply adaptive thresholding
    thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
    #return thresholded image
    return thresh

#construct argument parser
parser = argparse.ArgumentParser(description='Binarize image and parse text in image to string')
parser.add_argument('-i', '--image', help='path to image', required=True)
parser.add_argument('-o', '--output', help='path to output file', required=True)
args = parser.parse_args()

# load image
img = cv2.imread(args.image)

#binarization of image
thresh = binarize(img)


#show image
cv2.imshow('image', thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()

#save image
cv2.imwrite(args.output+'/imagen3.jpg', thresh)

我要清理的结果图像是:

如果我应用侵蚀,结果如下:

哪个比哪个都差

编辑:原图为:

【问题讨论】:

  • 在你的adaptiveThreshold,你试过不同的参数吗?您的结果看起来没有正确使用adaptiveThreshold
  • 我该如何改进它们?我需要用不同的 C 值发短信吗?但它不会把代码变成手动的吗?我的意思是如果图像有不同的阴影或类似的东西,我需要改变这个值吗?
  • 为什么要黑白图像?您正在寻找缩小图像的尺寸吗?
  • 是用pytesseract读取图片中的文字

标签: python opencv image-processing tesseract python-tesseract


【解决方案1】:

您只需要在 Python/OpenCV 中增加自适应阈值参数即可。

输入:

import cv2

# read image
img = cv2.imread("petrol.png")

# convert img to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# do adaptive threshold on gray image
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 21, 25)

# write results to disk
cv2.imwrite("petrol_threshold.png", thresh)

# display it
cv2.imshow("THRESHOLD", thresh)
cv2.waitKey(0)

结果:

【讨论】:

  • 我就是这个意思。
猜你喜欢
  • 2012-03-08
  • 1970-01-01
  • 1970-01-01
  • 2017-06-23
  • 2015-08-02
  • 2017-07-05
  • 1970-01-01
  • 1970-01-01
  • 2014-05-22
相关资源
最近更新 更多