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

相关文章

使用Python3中的gettext模块翻译Python源码以支持多语言

使用Python3中的gettext模块翻译Python源码以支持多语言

你写了一个Python 3程序,还想要它适用于其他语言。你能复制全部代码库,然后刻意地检查每个.py文件,替换掉所有找到的文本字符串。但这意味着你有两份你代码的独立副本,每当你要做出个改...

利用Python实现命令行版的火车票查看器

接口设计 一个应用写出来最终是要给人使用的,哪怕只是给你自己使用。所以,首先应该想想你希望怎么使用它?让我们先给这个小应用起个名字吧,既然及查询票务信息,那就叫它tickets好了。我们...

学习python之编写简单乘法口诀表实现代码

实现代码一、 #!/usr/bin/python x,y=9,9 lst=[(x,y,str(y)+'X'+str(x)+'='+str(x*y)) fo...

整理Python 常用string函数(收藏)

字符串中字符大小写的变换 1. str.lower() //小写 >>> 'SkatE'.lower() 'skate' 2. str.upper() //大写 >...

python匹配两个短语之间的字符实例

如下所示: def ref_txt_demo(): f = open('1.txt', 'r') data = f.readlines() for line in data:...