如何将图像中的字符和单词分割为轮廓

时间:2019-06-21 07:22:36

标签: python opencv image-processing contour image-segmentation

我有一些轮廓图,我想对其进行分割,这基本上意味着我想将轮廓图中的所有字符保存为单独的图像。但是我得到了一些噪声图像以及所需的输出。我想知道如何在不影响所需输出的情况下删除所有噪声图像。

我试图更改wh的值,以便可以最大程度地减少噪声并仅将字符作为分段图像。

def imageSegmentation(fldr):
        for file in fldr:
            for f in os.listdir(file):
                im = cv2.imread(file+f)
                #print(f)
                imgray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
                ret, thresh = cv2.threshold(imgray, 127, 255, 0)
                contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
                con_img=cv2.drawContours(im, contours, -1, (0,0,0), 1)
                #cv2.imshow("Contour_Image",con_img)
                #cv2.waitKey(0)
                #cv2.destroyAllWindows()

                newfolder=file+"\\contour\\"+f+"\\"
                os.makedirs(newfolder, exist_ok=True)
                fname=os.path.splitext(f)[0]
                cv2.imwrite((newfolder+fname+".png"),con_img)
                #cv2.imshow("con_img",con_img)
                #cv2.waitKey()
                #cv2.destroyAllWindows()

                newfolder2=file+"\\seg\\"+fname+"\\"
                os.makedirs(newfolder2,exist_ok=True)
                sorted_ctrs = sorted(contours, key=lambda cntr: cv2.boundingRect(cntr)[0])

                for i, cntr in enumerate(sorted_ctrs):
                    # Get bounding box
                    x, y, w, h = cv2.boundingRect(cntr)

                    # Getting ROI
                    roi = im[y:y + h, x:x + w]
                    #roi=~roi

                    if w > 9 and h > 27:
                        cv2.imwrite(newfolder2+"{}.png".format(i), roi)

我想知道如何在输出文件夹中仅获取正确的字符图像(不包括噪点图像)。我添加了一些输入轮廓图像,需要将其分割成单个字符。

1

2

3

4

5

6

1 个答案:

答案 0 :(得分:1)

由于您要提取单个字符还是整个单词的问题尚不完全清楚,因此可以同时使用这两种方法。

单个字符

这里的主要思想是

  • 将图像转换为灰度和高斯模糊
  • 执行Canny边缘检测
  • 找到轮廓
  • 遍历轮廓并使用最小面积过滤
  • 获取边界框并提取ROI

使用cv2.Canny()

的Canny边缘检测

enter image description here

现在,我们使用cv2.findContours()遍历轮廓并使用cv2.contourArea()进行过滤,然后绘制边界框 enter image description here

这是您其他一些输入图像的结果

enter image description here enter image description here

enter image description here enter image description here

import cv2

image = cv2.imread('1.png')
original = image.copy()

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
canny = cv2.Canny(blur, 120, 255, 1)

cnts = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

min_area = 100
image_number = 0
for c in cnts:
    area = cv2.contourArea(c)
    if area > min_area:
        x,y,w,h = cv2.boundingRect(c)
        cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
        ROI = original[y:y+h, x:x+w]
        cv2.imwrite("ROI_{}.png".format(image_number), ROI)
        image_number += 1

cv2.imshow('blur', blur)
cv2.imshow('canny', canny)
cv2.imshow('image', image)
cv2.waitKey(0)

整个单词

现在,如果要提取整个单词,则必须稍微修改策略

  • 将图像转换为灰度和高斯模糊
  • 执行Canny边缘检测
  • 扩张以获得单个轮廓
  • 找到轮廓
  • 遍历轮廓并使用最小面积过滤
  • 获取边界框并提取ROI

Canny边缘检测

enter image description here

使用cv2.dilate()进行扩张以连接轮廓

enter image description here

找到边界框并使用轮廓区域进行过滤

enter image description here

提取的投资回报率

enter image description here

注意:如果要查找整个单词,则可能必须更改最小面积值,因为它取决于所分析的图像。

import cv2

image = cv2.imread('1.png')
original = image.copy()

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
canny = cv2.Canny(blur, 120, 255, 1)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9,9))
dilate = cv2.dilate(canny, kernel, iterations=5)
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

min_area = 5000
image_number = 0
for c in cnts:
    area = cv2.contourArea(c)
    if area > min_area:
        x,y,w,h = cv2.boundingRect(c)
        cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
        ROI = original[y:y+h, x:x+w]
        cv2.imwrite("ROI_{}.png".format(image_number), ROI)
        image_number += 1

cv2.imshow('blur', blur)
cv2.imshow('dilate', dilate)
cv2.imshow('canny', canny)
cv2.imshow('image', image)
cv2.waitKey(0)