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

时间:2017-10-27 09:26:35

标签: python opencv image-processing computer-vision mnist

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

enter image description here

二值化之后,我得到了这张图片

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)

enter image description here

然后在此图像上找到轮廓。

(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) 

我正在

enter image description here

我需要一种方法来过滤轮廓,以便它只选择字符。所以我可以找到边界框并提取roi。

我可以找到轮廓并根据区域的大小过滤它们,但源图像的分辨率不一致。这些图像来自移动相机。

同样,盒子的边框也断开了。我无法准确地检测到这些盒子。

编辑:

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

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)

enter image description here

1 个答案:

答案 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)

结果

res

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

来源:Extract ROI from image with Python and OpenCV和我的一些知识。

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