简体   繁体   English

如何从图像中仅提取字符?

[英]How to extract only characters from image?

I have this type of image from that I only want to extract the characters. 我有这种类型的图像,我只想提取字符。

在此输入图像描述

After binarization, I am getting this image 二值化之后,我得到了这张图片

img = cv2.imread('the_image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 9)

在此输入图像描述

Then find contours on this image. 然后在此图像上找到轮廓。

(im2, cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
for contour in cnts[:2000]:
    x, y, w, h = cv2.boundingRect(contour)
    aspect_ratio = h/w
    area = cv2.contourArea(contour)
    cv2.drawContours(img, [contour], -1, (0, 255, 0), 2) 

I am getting 我正进入(状态

在此输入图像描述

I need a way to filter the contours so that it selects only the characters. 我需要一种方法来过滤轮廓,以便它只选择字符。 So I can find the bounding boxes and extract roi. 所以我可以找到边界框并提取roi。

I can find contours and filter them based on the size of areas, but the resolution of the source images are not consistent. 我可以找到轮廓并根据区域的大小过滤它们,但源图像的分辨率不一致。 These images are taken from mobile cameras. 这些图像来自移动相机。

Also as the borders of the boxes are disconnected. 此外,框的边框是断开的。 I can't accurately detect the boxes. 我无法准确检测到这些盒子。

Edit: 编辑:

If I deselect boxes which has an aspect ratio less than 0.4. 如果我取消选择宽高比小于0.4的盒子。 Then it works up to some extent. 然后它在某种程度上起作用。 But I don't know if it will work or not for different resolution of images. 但我不知道它是否适用于不同分辨率的图像。

for contour in cnts[:2000]:
    x, y, w, h = cv2.boundingRect(contour)
    aspect_ratio = h/w
    area = cv2.contourArea(contour)

    if aspect_ratio < 0.4:
        continue
    print(aspect_ratio)
    cv2.drawContours(img, [contour], -1, (0, 255, 0), 2)

在此输入图像描述

Not so difficult... 没那么难......

import cv2

img = cv2.imread('img.jpg')

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

ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU)
cv2.imshow('thresh', thresh)

im2, ctrs, hier = cv2.findContours(thresh.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])

for i, ctr in enumerate(sorted_ctrs):
    x, y, w, h = cv2.boundingRect(ctr)

    roi = img[y:y + h, x:x + w]

    area = w*h

    if 250 < area < 900:
        rect = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
        cv2.imshow('rect', rect)

cv2.waitKey(0)

Result 结果

水库

You can tweak the code like you want (here it can save ROI using original image; for eventually OCR recognition you have to save them in binary format - better methods than sorting by area are available) 你可以调整你想要的代码(这里它可以使用原始图像保存ROI;最终OCR识别你必须以二进制格式保存它们 - 比按区域排序更好的方法可用)

Source: Extract ROI from image with Python and OpenCV and some of my knowledge. 来源: 使用Python和OpenCV从图像中提取ROI以及我的一些知识。

Just kidding, take a look at my questions/answers. 开个玩笑,看看我的问题/答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM