簡體   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