【问题标题】:How to segment characters and words from images into contours如何将图像中的字符和单词分割成轮廓
【发布时间】:2019-11-04 00:37:29
【问题描述】:

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

我试图更改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)

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

【问题讨论】:

  • I have few images of words which i want to segment into individual character images 的可能重复问题除了其他输入图像之外,这个问题与您之前的问题有很大不同吗?
  • @HansHirse 这个程序完全不同....
  • @Ani,你要获取单个字符还是整个单词?例如,narasimha 或每个单独的字母。
  • @nathancy 我想要单个字符,例如 n、a、r 等...
  • @nathancy 您的代码不适用于少数图像,我想要一个动态代码让我们说如果我有任何新图像在这种情况下我不需要更改原始代码.它应该适用于所有字符大小的图像。

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


【解决方案1】:

如果您想提取单个字符或整个单词,由于您的问题并不完全清楚,因此这里有两种方法。

单个字符

这里的主要思想是

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

Canny 边缘检测使用cv2.Canny()

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

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

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)

全词

现在如果你想提取整个单词,你必须稍微修改一下策略

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

Canny 边缘检测

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

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

提取的投资回报率

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

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)

【讨论】:

    猜你喜欢
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 2017-08-15
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 2021-06-19
    相关资源
    最近更新 更多