如何仅从图像中提取字符? [英] How to extract only characters from image?

查看:112
本文介绍了如何仅从图像中提取字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种类型的图像,我只想提取字符.

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) 

我得到

我需要一种过滤轮廓的方法,以便仅选择字符.这样我就可以找到边界框并提取roi.

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.

我可以找到轮廓并根据区域大小对其进行过滤,但是源图像的分辨率不一致.这些图像是从移动相机拍摄的.

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.

如果我取消选择宽高比小于0.4的框.然后它在某种程度上起作用.但是我不知道它是否适用于不同分辨率的图像.

If I deselect boxes which has an aspect ratio less than 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)

推荐答案

不是那么困难...

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)

结果

您可以根据需要调整代码(这里可以使用原始图像节省ROI;要最终实现OCR识别,您必须将其保存为二进制格式-比按区域排序更好的方法可用)

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)

来源:使用Python和OpenCV从图像中提取ROI 和我的一些知识.

Source: Extract ROI from image with Python and OpenCV and some of my knowledge.

开个玩笑,看看我的问题/答案.

Just kidding, take a look at my questions/answers.

这篇关于如何仅从图像中提取字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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