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

yipeiwu_com6年前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函数与方法的区别总结

(1)函数的分类: 内置函数:python内嵌的一些函数。 匿名函数:一行代码实现一个函数功能。 递归函数 自定义函数:根据自己的需求,来进行定义函数。 (2)方法的分类: 普通方法:直...

Python编程实现蚁群算法详解

Python编程实现蚁群算法详解

简介 蚁群算法(ant colony optimization, ACO),又称蚂蚁算法,是一种用来在图中寻找优化路径的机率型算法。它由Marco Dorigo于1992年在他的博士论文...

详解如何将python3.6软件的py文件打包成exe程序

详解如何将python3.6软件的py文件打包成exe程序

在我们完成一个Python项目或一个程序时,希望将Python的py文件打包成在Windows系统下直接可以运行的exe程序。在浏览网上的资料来看,有利用pyinstaller和cx_F...

Python自动发送邮件的方法实例总结

Python自动发送邮件的方法实例总结

本文实例讲述了Python自动发送邮件的方法。分享给大家供大家参考,具体如下: python发邮件需要掌握两个模块的用法,smtplib和email,这俩模块是python自带的,只需i...

python使用chardet判断字符串编码的方法

本文实例讲述了python使用chardet判断字符串编码的方法。分享给大家供大家参考。具体分析如下: 最近利用python抓取一些网上的数据,遇到了编码的问题。非常头痛,总结一下用到的...