2

MSERinを使用して、Python で画像からの文字抽出を実装しようとしていopencvます。これは今までの私のコードです:

import cv2
import numpy as np

# create MSER object
mser = cv2.MSER_create()
# convert image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# detect the regions
regions,_ = mser.detectRegions(gray)
# find convex hulls of the regions
hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]
# initialize threshold area of the contours
ThresholdContourArea = 10000
# initialize empty list for the characters and their locations
char = []
loc =[]
# get the character part of the image and it's location if the area of contour less than threshold
for contour in hulls:
    if cv2.contourArea(contour) > ThresholdContourArea:
        continue
    # get the bounding rectangle around the contour
    bound_rect = cv2.boundingRect(contour)
    loc.append(bound_rect)
    det_char = gray[bound_rect[1]:bound_rect[1]+bound_rect[3],bound_rect[0]:bound_rect[0]+bound_rect[2]]
    char.append(det_char)

しかし、この方法では同じ文字に対して複数の輪郭が得られ、場所によっては複数の単語が 1 つの輪郭に配置されます。ここに例があります:元の画像:

ここに画像の説明を入力

輪郭を追加した後:

ここに画像の説明を入力

ここでは、最初の T の周りに複数の輪郭があり、2 つの rs が 1 つの輪郭に結合されています。どうすればそれを防ぐことができますか?

4

1 に答える 1