使用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版本坑:md5例子(python2与python3中md5区别)

起步 对于一些字符,python2和python3的md5加密出来是不一样的. # python2.7 pwd = "xxx" + chr(163) + "fj" checkcode...

python中的随机函数random的用法示例

一、random模块简介 Python标准库中的random函数,可以生成随机浮点数、整数、字符串,甚至帮助你随机选择列表序列中的一个元素,打乱一组数据等。 二、random模块重要函数...

Python3数字求和的实例

以下实例为通过用户输入两个数字,并计算两个数字之和: # -*- coding: UTF-8 -*- # Filename : test.py # author by : www...

详解Python如何生成词云的方法

详解Python如何生成词云的方法

前言 今天教大家用wrodcloud模块来生成词云,我读取了一篇小说并生成了词云,先看一下效果图: 效果图一: 效果图二: 根据效果图分析的还是比较准确的,小说中的主人公就是“程...

Python3 安装PyQt5及exe打包图文教程

Python3 安装PyQt5及exe打包图文教程

环境: Python 3.6.4 + Pycharm Professional 2017.3.3 + PyQt5 + PyQt5-tools ① Python 3 安装 Python 3...