【问题标题】:Extract text from image using MSER in Opencv python在 Opencv python 中使用 MSER 从图像中提取文本
【发布时间】:2017-05-25 16:52:07
【问题描述】:

我想使用 mser 检测图像中的文本并删除所有非文本区域。使用下面的代码我能够检测到文本:

import cv2
import sys


mser = cv2.MSER_create()
img = cv2.imread('signboard.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
vis = img.copy()
regions, _ = mser.detectRegions(gray)
hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]
cv2.polylines(vis, hulls, 1, (0, 255, 0))
cv2.imshow('img', vis)
if cv2.waitKey(0) == 9:
    cv2.destroyAllWindows()

如何删除所有非文本区域并获得仅包含文本的二进制图像?我搜索了很多,但找不到任何使用 python 和 opencv 的示例代码。

【问题讨论】:

    标签: python python-3.x opencv image-processing mser


    【解决方案1】:

    您可以使用找到的轮廓获得二值图像。只需将填充的轮廓绘制为白色的空白 img。

    mask = np.zeros((img.shape[0], img.shape[1], 1), dtype=np.uint8)
    for contour in hulls:
        cv2.drawContours(mask, [contour], -1, (255, 255, 255), -1)
    

    注意有关 drawContours 的更多信息,请参阅 the official docs

    然后您可以使用它仅提取文本:

    text_only = cv2.bitwise_and(img, img, mask=mask)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-15
      • 2013-10-08
      • 1970-01-01
      • 2016-02-18
      • 1970-01-01
      • 2017-02-25
      • 2019-07-17
      • 1970-01-01
      相关资源
      最近更新 更多