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

2019-07-30 13:11:13 +08:00
 cxh116

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])
1338 次点击
所在节点    问与答
1 条回复
qza1212
2019-07-31 10:24:11 +08:00
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])

整个算法比较简单,大概只能处理非自然场景下指定方向的连通域分割

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/587465

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX