使用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模块识别滑动验证码的缺口,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

相关文章

Django添加favicon.ico图标的示例代码

所谓favicon,即Favorites Icon的缩写,让浏览器的收藏夹中除显示相应的标题外,还以图标的方式区别不同的网站。 默认情况下,浏览器访问一个网站的时候,同时还会向服务器请求...

python实现简单tftp(基于udp协议)

python实现简单tftp(基于udp协议)

本文实例为大家分享了python实现简单tftp的具体代码,供大家参考,具体内容如下 tftp是基于udp的协议 实现简单的tftp,首先要有tftp的协议图。 tft...

用Python实现web端用户登录和注册功能的教程

用Python实现web端用户登录和注册功能的教程

用户管理是绝大部分Web网站都需要解决的问题。用户管理涉及到用户注册和登录。 用户注册相对简单,我们可以先通过API把用户注册这个功能实现了: _RE_MD5 = re.compil...

Python做文本按行去重的实现方法

文本: 每行在promotion后面包含一些数字,如果这些数字是相同的,则认为是相同的行,对于相同的行,只保留一行。 思路: 根据字典和字符串切割。 建立一个空字典。 读入文本,并对每行...

Python编程实现从字典中提取子集的方法分析

本文实例讲述了Python编程实现从字典中提取子集的方法。分享给大家供大家参考,具体如下: 首先我们会想到使用字典推导式(dictionary comprehension)来解决这个问题...