繁体   English   中英

如何在python中使用tesseract仅从图像中获取数字?

[英]How can I get only the numbers from the image using tesseract in python?

这是原图

原图

def extract_number(video_name):
  global read
  img = cv2.imread(video_name)
  HSV_image = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  h, s, v = cv2.split(HSV_image)
  s = cv2.GaussianBlur(s, (1, 1), 0)
  thresh = cv2.threshold(
      s, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
  cv2.imwrite('{}.png'.format(video_name), thresh)
  kernel = cv2.getStructuringElement(cv2.MORPH_RECT, ksize=(1, 2))
  thresh = cv2.dilate(thresh, kernel)
  txt = image_to_string(thresh, config='--oem 1 --psm 13')
  return txt

这是我用上面的代码实现的最好的

最好的尝试

我会让它比那简单得多:

def extract_number(your_image):
  # open the image
  img = cv2.imread(your_image)

  # turn it to a binary image, no need to save it.
  [ret, binary_image] = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY) 

  # turn it into text (you are probably going to have issues depending on the image)
  txt = image_to_string(binary_image, config='--oem 1 --psm 13')
  return txt

将其转换为二值图像的结果: 在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM