使用凸壳坐标提取字符 - opencv - python

Extract Characters using convex Hull coordinates - opencv - python

我有这样的人物图片:

使用下面的代码我可以获得轮廓和凸包,然后我可以为每个字符绘制凸包。

import cv2
img = cv2.imread('test.png', -1)

ret, threshed_img = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY),
                        127, 255, cv2.THRESH_BINARY)
image, contours, hier = cv2.findContours(threshed_img, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
for cnt in contours:
    # get convex hull
    hull = cv2.convexHull(cnt)
    cv2.drawContours(img, [hull], -1, (0, 0, 255), 1)
    print(hull)
cv2.imwrite("contours.jpg", img)

结果如下:

我可以获得这样的船体坐标(对于一个字符):

[[[546 134]]
 [[534 149]]
 [[532 151]]
 [[527 153]]
 [[523 154]]
 [[522 154]]
 [[520 109]]
 [[521 107]]
 [[524 106]]
 [[533 106]]
 [[539 111]]
 [[543 117]]
 [[546 122]]]

现在我想使用 convexHull 坐标分隔每个字符。 分离后,图像会像,

。 . .

我想使用 convexHull 坐标的主要原因是我可以分割垂直图像 space 中重叠的字符。您可以使用下图理解我的意思:

我无法准确分割字符,因为大多数图像都包含上述字符。所以我想使用 convexHull 坐标分割字符。

  • 得到一个字符的凸包后,找到对应的轮廓并填充
  • 我用原始图像屏蔽了每个单独的归档轮廓以获得片段

代码如下:

import cv2
import numpy as np

img = cv2.imread(r'C:\Users\selwyn77\Desktop\letters.png', -1)
img2 = img.copy()
cv2.imshow("original.jpg", img)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

ret, threshed_img = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
image, contours, hier = cv2.findContours(threshed_img, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

#--- Black image to be used to draw individual convex hull ---
black = np.zeros_like(img)
cv2.imshow("black.jpg", black)

contours = sorted(contours, key=lambda ctr: cv2.boundingRect(ctr)[0]) #added by OP : this sorts contours left to right, so images come in order

for cnt in contours:
    hull = cv2.convexHull(cnt)

    img3 = img.copy()
    black2 = black.copy()

    #--- Here is where I am filling the contour after finding the convex hull ---
    cv2.drawContours(black2, [hull], -1, (255, 255, 255), -1)
    g2 = cv2.cvtColor(black2, cv2.COLOR_BGR2GRAY)
    r, t2 = cv2.threshold(g2, 127, 255, cv2.THRESH_BINARY)
    cv2.imshow("t2.jpg", t2)

    masked = cv2.bitwise_and(img2, img2, mask = t2)    
    cv2.imshow("masked.jpg", masked)

    print(len(hull))
    cv2.waitKey(0)

cv2.destroyAllWindows()

现在您可以使用 cv2.imwrite().

保存每个单独的片段

这里是一些分段字符: