python - 在 Opencv python 中使用 MSER 从图像中提取文本

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

我想使用 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 执行此操作的示例代码。

最佳答案

您可以使用找到的轮廓获得二值图像。只需将填充的轮廓绘制到白色的空 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)

注意:参见the official docs有关 drawContours 的更多信息

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

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

关于python - 在 Opencv python 中使用 MSER 从图像中提取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44185854/

相关文章:

python - 提取数据缺失的日期范围

Python - 类上的描述符用于*设置*属性

python - 重载 __getitem__ 接受另一个参数

python - 如何从图像中提取虚线文本?

c++ - OpenNI depth 图像交换深度显示

iphone - OpenCV基础教程

python - Pandas 群by : counting rows satisfying condition on other columns?

python - 从 Flask 以 CSV 形式返回 Numpy 数组

python - 我一直收到一个名为 'int' 的错误,对象没有属性 'replace'

python - cPickle 数据量非常大