簡體   English   中英

OCR - 如何使用 python 識別方框內的數字?

[英]OCR - How to recognize numbers inside square boxes using python?

光學字符識別 (ocr) 的一個問題是,當數字位於方框內時,它無法正確識別數字。 此處討論了 tesseract 的一個失敗示例: Tesseract - How can I identify numbers in box? 我在這里用 paddleocr 進行測試: https ://www.paddlepaddle.org.cn/hub/scene/ocr 你也可以快速嘗試這個 api,對於這個輸入圖像:

在此處輸入圖像描述 它什么也沒返回。。

當我再次嘗試這樣的圖像時: 在此處輸入圖像描述

它成功返回所有數字。大多數情況下,這些數字識別(打印和手寫)在正方形框內時失敗。為了識別正方形框內的數字,我們需要將這些所謂的盒子圖像中的數字轉換為圖像中的數字刪除所有方形框。 我有一些如下圖:

在此處輸入圖像描述 在此處輸入圖像描述

在此處輸入圖像描述 在此處輸入圖像描述

看,數字外面的完整方形框不完全可見,只有部分方形框可見。我想將這些圖像轉換為圖像,通過刪除方形框或方形框的某些部分,我將只有數字之后出現在這些圖像中,希望數字/數字識別將起作用。 我試過這段代碼:

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('/content/21.png')
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
linek = np.zeros((11,11),dtype=np.uint8)
linek[...,5]=1
x=cv2.morphologyEx(gray, cv2.MORPH_OPEN, linek ,iterations=800)
gray-=x
plt.imshow(gray)
cv2.imwrite('21_output.jpg', gray)

輸出 :

在此處輸入圖像描述

也試過這段代碼:

import cv2
import numpy as np
import matplotlib.pyplot as plt

#https://stackoverflow.com/questions/57961119/how-to-remove-all-the-detected-lines-from-the-original-image-using-python

image = cv2.imread('/content/17.png')
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Remove vertical
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,10))
detected_lines = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(detected_lines, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(image, [c], -1, (255,255,255), 2)

image = thresh - detected_lines
plt.imshow( image)

輸出 :

在此處輸入圖像描述

不幸的是,它無法完全刪除不需要的行。當它刪除不需要的行時,它也會刪除部分原始數字/數字。 如何刪除圖像中每個數字之外的那些完整或不完整的方框? 提前致謝。

下面的代碼對我來說做得不錯,但它對超參數敏感:

import cv2
import imutils
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import pyplot as plt
  

def square_number_box_denoiser(image_path="/content/9.png",is_resize = False, resize_width = 768):
    '''
    ref : https://pretagteam.com/question/removing-horizontal-lines-in-image-opencv-python-matplotlib

    Args : 
      image_path (str) : path of the image containing numbers/digits inside square box
      is_resize (int) : whether to resize the input image or not? default : False
      resize_width (int) : resizable image width for resizing the image by maintaining aspect ratio. default : 768 

    '''
    img=cv2.imread(image_path)
    if(is_resize):
      print("resizing...")
      img = imutils.resize(img, width=resize_width)
    image = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
    gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

    # Remove horizontal
    horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25,1))
    detected_lines = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
    cnts = cv2.findContours(detected_lines, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    for c in cnts:
        cv2.drawContours(image, [c], -1, (255,255,255), 2)

    # Repair image
    repair_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,6))
    result = 255 - cv2.morphologyEx(255 - image, cv2.MORPH_CLOSE, repair_kernel, iterations=2)

    # create figure
    fig = plt.figure(figsize=(20, 20))
    # setting values to rows and column variables
    rows = 3
    columns = 3

    fig.add_subplot(rows,  columns, 1)
    plt.imshow(img)
    fig.add_subplot(rows,  columns, 2)
    plt.imshow(thresh)
    fig.add_subplot(rows,  columns, 3)
    plt.imshow(detected_lines)
    fig.add_subplot(rows,  columns, 4)
    plt.imshow(image)
    fig.add_subplot(rows,  columns, 5)
    plt.imshow(result)
    result = cv2.rotate(result,cv2.ROTATE_90_COUNTERCLOCKWISE)
    fig.add_subplot(rows,  columns, 6)
    plt.imshow(result)
    cv2.imwrite("result.jpg", result)

    plt.show()

輸出: 在此處輸入圖像描述

無需調整大小:

在此處輸入圖像描述

在此處輸入圖像描述

在此處輸入圖像描述

在此處輸入圖像描述

768 調整大小:

在此處輸入圖像描述

在此處輸入圖像描述

在此處輸入圖像描述

在此處輸入圖像描述

暫無
暫無

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

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