zoukankan      html  css  js  c++  java
  • 文本检测-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('{} {} {} {}
    '.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)
    
    

    矩形效果


  • 相关阅读:
    Docker之Harbor
    idea 代码块编辑(批量列编辑)快捷键 -- idea version 2018 不常用
    mysql 去除表中重复的数据,保留id最小的数据信息
    打家劫舍(动态规划+滚动数组+取模运算优化)
    利用线程异步调用
    idea 2019激活码
    mysql导出PDM表结构并带有注释
    安装GO
    GO语言
    项目启动
  • 原文地址:https://www.cnblogs.com/xuehuiping/p/14597282.html
Copyright © 2011-2022 走看看