[英]Text blur after thresholding using opencv
我正在使用tesseract
OCR 進行一些轉換以從圖像中捕獲文本,但是,這樣做后,我的文本在應用了一些閾值效果后變得模糊,所以我需要一些幫助,一點幫助。
這是我的代碼:
import cv2
import pytesseract as pyt
import numpy as np
pyt.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
image = cv2.imread('vacunacion.jpg')
gris = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gris, 90, 255, cv2.THRESH_BINARY_INV)[1]
imagen_detalle = pyt.image_to_string(thresh, lang='eng',config='--psm 6')
print(imagen_detalle)
cv2.imshow('thresh', thresh)
cv2.imwrite('thresh.jpg', thresh)
cv2.waitKey()
我的輸出:
2
> PLAN NACIONAL DE VACUNACION
CONTRA COVID-19 Hooia>—
| DOSIS APLICADAS: 191.480
CORTE 4:00 P.M. MARZO - 03 - 2021
SE AMDRES ay
“a 7 women {A GUNIRA (1345)
twas jf Ae d &
paunaanma Goer anda 760 — 2) ©? maaan
CARTABENA (3.457) BOLI (2500) — OESAN 018)
SURES) a ‘3 NORTE DE SANTANDER (25131
CORDOBA (2968) wo 6 SAATANDER (5.936)
ANTIONUIA (24.8563 TS - BOVAGA (5883),
CUNDALAMARCA (12.0251 TY ‘ ARAUCA C8)
SALDAS (23151 EN, f PS yionann 2
RAMLDA C320 wey Pee casa 1227
oan oe ts
Touma 5.080) BK eae 8 WETA (2444)
ALLE DEL cqUGA (20,160) ~=% se GUAINLA 592)
“m8 Se aN +S UAE (3023
‘ante ca o eco yqurtsig2an
puna 2635) ae SO BARUETAISES!
a
rum fe ‘AMAZDAAS (10548)
Oa
Fuente: Ministerio de Salud y Protecciin Saciat - Datos procesados 03 de marzo - 2021
應用劑量的數量在圖像中不能很好地欣賞,因為太模糊了,任何技術都可以在這里應用嗎?
Tesseract 可以使用文本周圍的漸變作為其檢測的一部分,因此我建議您盡可能避免使用閾值,因為它會從圖像中移除漸變(如fmw42所述,抗鋸齒)。
相反,在這里我建議在對圖像進行灰度化后反轉圖像,然后如有必要,您可以降低亮度以使更多的灰色文本更黑,並增加對比度以使灰色背景更白一些。 如果您確實需要調整亮度和/或對比度,我建議使用cv2.convertScaleAbs來有效地做到這一點並避免 integer 溢出問題。
您應用了簡單閾值處理並沒有達到預期的結果。 缺少的兩個部分是:
閾值化的另一種方法是采用二進制掩碼並應用一些morphological-transformation 。 然后您需要顯示每個檢測到的文本區域,將圖像居中並應用 ocr。 使用這種方法,您可以可視化並查看問題所在。
1.二進制掩碼
2. 擴張
我們應用了膨脹和bitwise_and來更准確地檢測文本。
我發現上述結果(其中一些顯示)使用英語。 當前的語言對我來說是陌生的。 因此,您需要為語言配置 tesseract
代碼:
# Load the library
import cv2
import numpy as np
import pytesseract
# Load the image
img = cv2.imread("u1niV.jpg")
# Convert to HSV color-space
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# Get binary mask
msk = cv2.inRange(hsv, np.array([0, 0, 181]), np.array([160, 255, 255]))
# Extract features
krn = cv2.getStructuringElement(cv2.MORPH_RECT, (5 ,3))
dlt = cv2.dilate(msk, krn, iterations=5)
res = 255 - cv2.bitwise_and(dlt, msk)
# OCR detection
d = pytesseract.image_to_data(res, output_type=pytesseract.Output.DICT)
# Get ROI part from the detection
n_boxes = len(d['level'])
# For each detected part
for i in range(n_boxes):
# Get the localized region
(x, y, w, h) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i])
# Draw rectangle to the detected region
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 5)
# Crop the region
cropped = res[y:y+h, x:x+w]
# Center the region
cropped = cv2.copyMakeBorder(cropped, 50, 50, 50, 50, cv2.BORDER_CONSTANT, value=255)
# OCR the region
txt = pytesseract.image_to_string(cropped)
print(txt)
# Display
cv2.imshow("cropped", cropped)
cv2.waitKey(0)
# Display
cv2.imshow("res", res)
cv2.waitKey(0)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.