使用凸包体坐标提取字符-opencv-python [英] Extract Characters using convex Hull coordinates - opencv - python

查看:145
本文介绍了使用凸包体坐标提取字符-opencv-python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的人物形象:

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

Using following code I could get contours and convex hull, then I could draw convex for each character.

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)

结果如下:

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

I could get hull coordinates like this (for one character):

[[[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坐标来分隔每个字符. 分离后,图像会像

Now I want to separate each character using convexHull coordinates. After separating, images would be like,

. .

我要使用convexHull坐标的主要原因是可以分割在垂直图像空间中重叠的字符.您可以通过使用以下图像来理解我的意思:

The main reason I want to use convexHull coordinates is then I can segment characters which were overlapped in vertical image space. You can understand what I have meant by using following image:

由于大多数图像都包含上述字符,因此我无法准确地细分字符.所以我想使用convexHull坐标来分割字符.

I can't segment characters accurately since most of the images contain characters like above. So I want to segment characters using convexHull coordinates.

推荐答案

  • 获得一个字符的凸包后,我找到了对应的轮廓并将其填充.
  • 我用原始图像遮盖了每个单独的轮廓,以获得片段
  • 这是代码:

    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()保存每个单独的细分.

    Now you can save each of the individual segments using cv2.imwrite().

    以下是一些分段字符:

    这篇关于使用凸包体坐标提取字符-opencv-python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆