簡體   English   中英

opencv 閾值處理和 pytesseract

[英]opencv thresholding and pytesseract

目前我正在嘗試開發一些簡單的計算機視覺代碼來讀取我在使命召喚游戲中的擊殺數量並將其作為整數保存到數組中。 該代碼每秒對我的屏幕進行截圖,並使用 opencv 對圖像進行閾值處理並將其輸入到 pytesseract 中。 盡管數字保持不變,但背景噪聲會極大地改變圖像並強制執行大量空輸入。 如果它錯過了一些輸入,但它錯過了所有數字的 %50 或更多,我就可以了。 如果有人對具有不同背景的單個數字圖像進行閾值處理有任何技巧,那將是一個巨大的幫助。

'''

pytesseract.pytesseract.tesseract_cmd = r'C:/Program Files/Tesseract-OCR/tesseract'

pyautogui.screenshot('pictures/Kill.png', region = (1822, 48, 30, 23))

img = cv2.imread('pictures/Kill.png')

img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

ret, thresh1 = cv2.threshold(img, 255, 255, cv2.THRESH_TRUNC)

cv2.imwrite('pictures/killthresh1.png',thresh1)

ret, thresh1 = cv2.threshold(img, 180, 255, cv2.THRESH_BINARY)

thresh1 = cv2.bitwise_not(thresh1)

cv2.imwrite('pictures/Killthresh2.png', thresh1)

custom_config = r'-l eng --oem 3 --psm 7 -c 

tessedit_char_whitelist="1234567890" '

killnumber = pytesseract.image_to_string(thresh1, config = custom_config)

'''

原始 pyautogui 截圖

TRUNC 閾值

二進制閾值

注意:這些圖像產生了“NULL”結果,我不知道為什么

讀取圖片后, img = cv2.imread('pictures/Kill.png')

在原始 pyautogui 屏幕截圖上應用adaptive-threshold

在此處輸入圖片說明

現在閱讀:

txt = pytesseract.image_to_string(thr, config="--psm 7")
print(txt)

結果:

3

代碼:


import cv2
import pytesseract

img = cv2.imread("0wHAy.png")
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thr = cv2.adaptiveThreshold(gry, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                            cv2.THRESH_BINARY_INV, 21, 9)
txt = pytesseract.image_to_string(thr, config="--psm 7")
print(txt)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM