Как выполнить сегментацию цифр для рукописных номеров счетов и кодов сортировки в OPENCV?

Есть ли простой способ сегментации цифр из файлов, как показано на скриншоте ниже?

Я хочу использовать OpenCV для этого, так как это библиотека, которую я использую для остальной обработки, но другие предложения приветствуются.

Бумажная форма: БУМАЖНАЯ ФОРМА

Текстовое поле: Текстовое поле


person Alexandru M    schedule 14.03.2019    source источник


Ответы (2)


Простой метод OpenCV Contours здесь не работает, потому что цифры присутствуют в каком-то шаблоне блока, поэтому вам нужно сначала обнаружить блоки загляните в этот блог

person Atinesh    schedule 03.07.2019

Возьмите этот фрагмент кода и адаптируйте его для своей задачи. В вашей ситуации не сложно:

import cv2
import numpy as np

# import image
image = cv2.imread('C:\\Users\\PC\\Desktop\\roi.png')

# grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('gray', gray)

# binary
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
cv2.imshow('threshold', thresh)

# dilation
kernel = np.ones((10, 1), np.uint8)
img_dilation = cv2.dilate(thresh, kernel, iterations=1)
cv2.imshow('dilated', img_dilation)

# find contours
# cv2.findCountours() function changed from OpenCV3 to OpenCV4: now it have only two parameters instead of 3
cv2MajorVersion = cv2.__version__.split(".")[0]
# check for contours on thresh
if int(cv2MajorVersion) >= 4:
    ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
else:
    im2, ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])

for i, ctr in enumerate(sorted_ctrs):
    # Get bounding box
    x, y, w, h = cv2.boundingRect(ctr)

    # Getting ROI
    roi = image[y:y + h, x:x + w]

    # show ROI
    # cv2.imshow('segment no:'+str(i),roi)
    cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

    if w > 15 and h > 15:
        cv2.imwrite('C:\\Users\\PC\\Desktop\\output\\{}.png'.format(i), roi)

cv2.imshow('marked areas', image)
cv2.waitKey(0)

Источник: https://lucians.dev/extract-roi-from-image-with-python-and-opencv

person lucians    schedule 28.09.2020