V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
cxh116
V2EX  ›  问与答

请问这段代码是怎么实现图片的字符分割的? ( Python + opencv)

  •  
  •   cxh116 · 2019-07-30 13:11:13 +08:00 · 1331 次点击
    这是一个创建于 1748 天前的主题,其中的信息可能已经有所发展或是发生改变。

    https://github.com/xianyang-wong/Amazon-Captcha-Solver/blob/1dbae8f121e8004f387c6a9ea3df044eb2ba9924/amazon_captcha_solver.py#L21-L31

    21 到 31 行.

    def captcha_solver(path,threshold):
        image = cv2.imread(path)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        
        colmean = image.sum(axis=0)/70
        colmean_index = np.where(colmean < threshold)
        min_val = np.min(colmean_index)
        max_val = np.max(colmean_index)
        
        colmean_index = list(colmean_index)
        separators = []
        
        for i in np.arange(0,len(colmean_index[0]) - 1):
            if colmean_index[0][i] != colmean_index[0][i+1] - 1:
                separators.append(colmean_index[0][i])
    
    1 条回复    2019-07-31 10:24:11 +08:00
    qza1212
        1
    qza1212  
       2019-07-31 10:24:11 +08:00   ❤️ 1
    def captcha_solver(path,threshold):
    image = cv2.imread(path) // 读取图片
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) // 转灰度图

    colmean = image.sum(axis=0)/70 // 对每列取和然后除以 70
    colmean_index = np.where(colmean < threshold) // 找到所有小于阈值的列索引,这里索引已经从小到大排好序
    min_val = np.min(colmean_index) // 拿到最小索引
    max_val = np.max(colmean_index) // 拿到最大索引

    colmean_index = list(colmean_index) // mat 转 list
    separators = []

    for i in np.arange(0,len(colmean_index[0]) - 1): // 遍历 list
    if colmean_index[0][i] != colmean_index[0][i+1] - 1: // 其实就是找不相邻的列
    separators.append(colmean_index[0][i])

    整个算法比较简单,大概只能处理非自然场景下指定方向的连通域分割
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2726 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 06:07 · PVG 14:07 · LAX 23:07 · JFK 02:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.