簡體   English   中英

使用 OpenCV 優化 OCR 的各種亮度圖像

[英]Optimize various brightness image for OCR using OpenCV

我有以下類型的圖像:

示例 1 示例 2 示例 3 示例 4 示例 5 例 6 例 7 示例 8 示例 9

我想對它們進行預處理以獲得最佳 OCR 結果,但正如您所見,它們具有不同的亮度和不同的清晰度……是否可以進行一些“通用”調整以提取 OCR 文本並獲得最佳結果?

您可以使用簡單的 ocr為這些情況提供正確的結果。 這將適用於模糊和不模糊的情況。

import easyocr
import cv2
import numpy as np
from PIL import Image, ImageEnhance


def unsharp_mask(image, kernel_size=(5, 5), sigma=1.0, amount=1.0, threshold=0):
    """Return a sharpened version of the image, using an unsharp mask."""
    blurred = cv2.GaussianBlur(image, kernel_size, sigma)
    sharpened = float(amount + 1) * image - float(amount) * blurred
    sharpened = np.maximum(sharpened, np.zeros(sharpened.shape))
    sharpened = np.minimum(sharpened, 255 * np.ones(sharpened.shape))
    sharpened = sharpened.round().astype(np.uint8)
    if threshold > 0:
        low_contrast_mask = np.absolute(image - blurred) < threshold
        np.copyto(sharpened, image, where=low_contrast_mask)
    return sharpened

def increase_brightness(img, value):
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    h, s, v = cv2.split(hsv)

    lim = 255 - value
    v[v > lim] = 255
    v[v <= lim] += value

    final_hsv = cv2.merge((h, s, v))
    img = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
    return img

image = cv2.imread('if8nC.png')
sharpened = unsharp_mask(image)
imag = increase_brightness(sharpened, value=10) # 60 ->5qoOk.png #10 -> if8nC.png
cv2.imwrite('resize.png',imag)

reader = easyocr.Reader(['en'],gpu=False)
result = reader.readtext('resize.png')
for detection in result:
        print(detection)

您必須進行的唯一調整是將亮度值從 0 更改為 100。它適用於所有情況。 輸出是

([[1, 0], [282, 0], [282, 68], [1, 68]], 'Tvrdosin', 0.4517089309490733)

暫無
暫無

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

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