文本检测-1-MSER

MSER全称叫做最大稳定极值区域(MSER-Maximally Stable Extremal Regions),该算法是2002提出的,主要是基于分水岭的思想来做图像中斑点的检测。

# -*- encoding: utf-8 -*-
"""
@date: 2021/3/30 3:57 下午
@author: xuehuiping
"""
# https://my.oschina.net/u/876354/blog/3054322
# MSER
import cv2

# 读取图片
imagePath = '/Users/xuehuiping/git/fapiao_tax_code/demo_images/test2.png'
imgname = imagePath.split('/')[-1].split('.')[0]
imgname = 'result/' + imgname
print(imgname)
img = cv2.imread(imagePath)

# 灰度化
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
vis = img.copy()
orig = img.copy()

# 调用 MSER 算法
mser = cv2.MSER_create()
regions, _ = mser.detectRegions(gray)  # 获取文本区域
hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]  # 绘制文本区域
cv2.polylines(img, hulls, 1, (0, 255, 0))
cv2.imwrite(imgname + '_mser.jpg', img)
print(len(hulls))

# 将不规则检测框处理成矩形框
temp = []
with open(imgname + '_vis.txt', 'w') as f:
    for c in hulls:
        x, y, w, h = cv2.boundingRect(c)
        line = '{} {} {} {}'.format(x, y, w, h)
        if line in temp:
            continue
        if w > 100 or h > 100:
            continue
        temp.append(line)
        f.write('{} {} {} {}\n'.format(x, y, x + w, y + h))  # 左上角、右下角
        cv2.rectangle(vis, (x, y), (x + w, y + h), (255, 255, 0), 1)
cv2.imwrite(imgname + '_vis.jpg', vis)

矩形效果


posted on 2021-03-30 16:05  宋岳庭  阅读(511)  评论(0编辑  收藏  举报