使用Python的OpenCV模块识别滑动验证码的缺口(推荐)

yipeiwu_com5年前Python基础

最近终于找到一个好的方法,使用Python的OpenCV模块识别滑动验证码的缺口,可以将滑动验证码中的缺口识别出来了。

 

测试使用如下两张图片:

 

target.jpg

 

template.png

现在想要通过“template.png”在“target.jpg”中找到对应的缺口,代码实现如下:

# encoding=utf8

import cv2
import numpy as np

def show(name):
 cv2.imshow('Show', name)
 cv2.waitKey(0)
 cv2.destroyAllWindows()

def main():
 otemp = 'template.png'
 oblk = 'target.jpg'
 target = cv2.imread(otemp, 0)
 template = cv2.imread(oblk, 0)
 w, h = target.shape[::-1]
 temp = 'temp.jpg'
 targ = 'targ.jpg'
 cv2.imwrite(temp, template)
 cv2.imwrite(targ, target)
 target = cv2.imread(targ)
 target = cv2.cvtColor(target, cv2.COLOR_BGR2GRAY)
 target = abs(255 - target)
 cv2.imwrite(targ, target)
 target = cv2.imread(targ)
 template = cv2.imread(temp)
 result = cv2.matchTemplate(target, template, cv2.TM_CCOEFF_NORMED)
 x, y = np.unravel_index(result.argmax(), result.shape)
 # 展示圈出来的区域
 cv2.rectangle(template, (y, x), (y + w, x + h), (7, 249, 151), 2)
 show(template)
if __name__ == '__main__':

    main()运行结果见本文最上面,通过运行结果可以知道,已经正确的找到了缺口位置。

总结

以上所述是小编给大家介绍的使用Python的OpenCV模块识别滑动验证码的缺口,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

相关文章

python实现网站微信登录的示例代码

最近微信登录开放公测,为了方便微信用户使用,我们的产品也决定加上微信登录功能,然后就有了这篇笔记。 根据需求选择相应的登录方式 python实现网站微信登录的示例代码 微信现在提供两种...

详细解析Python中的变量的数据类型

详细解析Python中的变量的数据类型

 变量是只不过保留的内存位置用来存储值。这意味着,当创建一个变量,那么它在内存中保留一些空间。 根据一个变量的数据类型,解释器分配内存,并决定如何可以被存储在所保留的内存中。因...

简单介绍Ruby中的CGI编程

Ruby 是一门通用的语言,不仅仅是一门应用于WEB开发的语言,但 Ruby 在WEB应用及WEB工具中的开发是最常见的。 使用Ruby您不仅可以编写自己的SMTP服务器,FTP程序,或...

python3连接mysql获取ansible动态inventory脚本

Ansible Inventory  介绍 Ansible Inventory 是包含静态 Inventory 和动态 Inventory 两部分的,静态 Inventory...

Python3多目标赋值及共享引用注意事项

Python中多目标赋值即将等号左边所有的变量名都赋值给右边的对象,完成赋值操作,比如将三个变量同时赋值给一个字符串。 a = b = c = 'Python' print(a) p...