我正在尝试使用MSER
in实现从 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)
但是这种方法为同一个字母提供了多个轮廓,并且在某些地方,多个单词被放入一个轮廓中。这是一个例如:原始图像:
添加轮廓后:
这里第一个 T 周围有多个轮廓,两个 rs 组合成一个轮廓。我该如何防止呢?