在 OpenCV 中检测不是一个连通分量的形状

Detecting shapes that are not one connected component in OpenCV

该代码应使用 OpenCV 检测字母和数字。问题是它无法检测到包含两个部分的字母,例如 i、j 或阿拉伯字母 ب،ت،ث͌ͬ͌خ،ض 等

这是我的代码:

image = cv2.imread('output.png')

height, width, depth = image.shape

# resizing the image to find spaces better
image = cv2.resize(image, dsize=(width * 5, height * 4), interpolation=cv2.INTER_CUBIC)
# grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

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

# dilation
kernel = np.ones((5, 5), np.uint8)
img_dilation = cv2.dilate(thresh, kernel, iterations=1)

# adding GaussianBlur
gsblur = cv2.GaussianBlur(img_dilation, (5, 5), 0)

# find contours
ctrs, hier = cv2.findContours(gsblur.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

m = list()
# sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])
pchl = list()
dp = image.copy()
for i, ctr in enumerate(sorted_ctrs):
    # Get bounding box
    x, y, w, h = cv2.boundingRect(ctr)
    cv2.rectangle(dp, (x - 10, y - 10), (x + w + 10, y + h + 10), (90, 0, 255), 9)

我需要更改什么才能检测具有多个部分的形状?

草书在线光学字符识别不是一件容易的事,不是从单个字符识别的角度来看,而是因为对单个字符进行适当的分割。

该领域的一种流行方法是识别每个连接项,隔离它们,并构建某种关联算法。基本上你会有一堆字符部分,有些是完整的 个字符有些不是。对于这些不完整的项目(不能单独归类为字符)检查该项目的邻域,以便用它周围的项目构建一个字符(可能是左或右,甚至有点上或下)。分类器的反馈对于分割任务至关重要。

您会在以下名称的文献中找到此方法:隐式过度分割Recognition-based 分割this and this papers, and for arabic characters more in this 文章中有更多详细信息。