opencv处理验证码python代码
# -*- coding: utf-8 -*-
# @Time : 2019-02-11 09:39
# @Author : cxa
# @File : bgr2gry.py
# @Software: PyCharm
import cv2
import pathlib
import numpy as np
import time
import os
file_path = pathlib.Path.cwd().joinpath("picture/1.png")
char_path = pathlib.Path.cwd().joinpath("char_path")
def get_rect_box(contours):
ws = []
valid_contours = []
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
if w < 7:
continue
valid_contours.append(contour)
ws.append(w)
w_min = min(ws)
w_max = max(ws)
result = []
if len(valid_contours) == 4:
for contour in valid_contours:
x, y, w, h = cv2.boundingRect(contour)
box = np.int0([[x, y], [x + w, y], [x + w, y + h], [x, y + h]])
result.append(box)
elif len(valid_contours) == 3:
for contour in valid_contours:
x, y, w, h = cv2.boundingRect(contour)
if w == w_max:
box_left = np.int0([[x, y], [x + w / 2, y], [x + w / 2, y + h], [x, y + h]])
box_right = np.int0([[x + w / 2, y], [x + w, y], [x + w, y + h], [x + w / 2, y + h]])
result.append(box_left)
result.append(box_right)
else:
box = np.int0([[x, y], [x + w, y], [x + w, y + h], [x, y + h]])
result.append(box)
elif len(valid_contours) == 2:
for contour in valid_contours:
x, y, w, h = cv2.boundingRect(contour)
if w == w_max and w_max >= w_min * 2:
box_left = np.int0([[x, y], [x + w / 3, y], [x + w / 3, y + h], [x, y + h]])
box_mid = np.int0([[x + w / 3, y], [x + w * 2 / 3, y], [x + w * 2 / 3, y + h], [x + w / 3, y + h]])
box_right = np.int0([[x + w * 2 / 3, y], [x + w, y], [x + w, y + h], [x + w * 2 / 3, y + h]])
result.append(box_left)
result.append(box_mid)
result.append(box_right)
elif w_max < w_min * 2:
box_left = np.int0([[x, y], [x + w / 2, y], [x + w / 2, y + h], [x, y + h]])
box_right = np.int0([[x + w / 2, y], [x + w, y], [x + w, y + h], [x + w / 2, y + h]])
result.append(box_left)
result.append(box_right)
else:
box = np.int0([[x, y], [x + w, y], [x + w, y + h], [x, y + h]])
result.append(box)
elif len(valid_contours) == 1:
contour = valid_contours[0]
x, y, w, h = cv2.boundingRect(contour)
box0 = np.int0([[x, y], [x + w / 4, y], [x + w / 4, y + h], [x, y + h]])
box1 = np.int0([[x + w / 4, y], [x + w * 2 / 4, y], [x + w * 2 / 4, y + h], [x + w / 4, y + h]])
box2 = np.int0([[x + w * 2 / 4, y], [x + w * 3 / 4, y], [x + w * 3 / 4, y + h], [x + w * 2 / 4, y + h]])
box3 = np.int0([[x + w * 3 / 4, y], [x + w, y], [x + w, y + h], [x + w * 3 / 4, y + h]])
result.extend([box0, box1, box2, box3])
elif len(valid_contours) > 4:
for contour in valid_contours:
x, y, w, h = cv2.boundingRect(contour)
box = np.int0([[x, y], [x + w, y], [x + w, y + h], [x, y + h]])
result.append(box)
result = sorted(result, key=lambda x: x[0][0])
return result
# 干扰线降噪
def interference_line(img, img_name):
h, w = img.shape[:2]
# !!!opencv矩阵点是反的
# img[1,2] 1:图片的高度,2:图片的宽度
for y in range(1, w - 1):
for x in range(1, h - 1):
count = 0
if img[x, y - 1] > 245:
count = count + 1
if img[x, y + 1] > 245:
count = count + 1
if img[x - 1, y] > 245:
count = count + 1
if img[x + 1, y] > 245:
count = count + 1
if count > 2:
img[x, y] = 255
cv2.imwrite(str(img_name), img)
return img
im = cv2.imread(str(file_path))
im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) # 将图片转成灰度图
# 将图片做二值化处理
ret, im_inv = cv2.threshold(im_gray, 127, 255, cv2.THRESH_BINARY_INV)
# 高斯模糊对图片进行降噪
kernel = 1 / 16 * np.array([[1, 2, 1], [2, 4, 2], [1, 2, 1]])
im_blur = cv2.filter2D(im_inv, -1, kernel)
# 再做一轮二值化处理
ret, binary = cv2.threshold(im_blur, 127, 255, cv2.THRESH_BINARY)
f_path2 = pathlib.Path.cwd().joinpath("picture/2.png")
# 去干扰线
last_im = interference_line(binary, f_path2)
# 识别
# 切割图片
# contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# boxes=get_rect_box(contours)
# for box in boxes:
# cv2.drawContours(im, [box], 0, (0,0,255),2)
# roi = binary[box[0][1]:box[3][1], box[0][0]:box[1][0]]
# roistd = cv2.resize(roi, (30, 30))
# timestamp = int(time.time() * 1e6)
# filename = "{}.jpg".format(timestamp)
# filepath = os.path.join(char_path, filename)
# cv2.imwrite(filepath, roistd)
# cv2.drawContours(im, contours, -1, (0, 0, 255), 3)
# cv2.imshow('IMG', im)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
opencv处理验证码python代码的更多相关文章
- Python 代码实现验证码识别
Python 代码实现验证码识别 测试开发社区 1周前 源 / j_hao104 一.探讨 识别图形验证码可以说是做爬虫的必修课,涉及到计算机图形学,机器学习,机器视觉,人工智能等等高深领域…… ...
- python代码 构建验证码
1.python代码编写 (随机验证码): #coding: utf-8 import Image, ImageDraw, ImageFont, ImageFilter import string, ...
- Python代码样例列表
扫描左上角二维码,关注公众账号 数字货币量化投资,回复“1279”,获取以下600个Python经典例子源码 ├─algorithm│ Python用户推荐系统曼哈顿算法实现.py│ ...
- 30行Python代码实现人脸检测
参考OpenCV自带的例子,30行Python代码实现人脸检测,不得不说,Python这个语言的优势太明显了,几乎把所有复杂的细节都屏蔽了,虽然效率较差,不过在调用OpenCV的模块时,因为模块都是C ...
- python+opencv中最近出现的一些变化( OpenCV 官方的 Python tutorial目前好像还没有改过来?) 记一次全景图像的拼接
最近在学习过程中发现opencv有了很多变动, OpenCV 官方的 Python tutorial目前好像还没有改过来,导致大家在学习上面都出现了一些问题,现在做一个小小的罗列,希望对大家有用 做的 ...
- Python面向对象编程扑克牌发牌程序,另含大量Python代码!
1. 题目 编写程序, 4名牌手打牌,计算机随机将52张牌(不含大小鬼)发给4名牌手,在屏幕上显示每位牌手的牌. 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不 ...
- 基于深度学习的人脸性别识别系统(含UI界面,Python代码)
摘要:人脸性别识别是人脸识别领域的一个热门方向,本文详细介绍基于深度学习的人脸性别识别系统,在介绍算法原理的同时,给出Python的实现代码以及PyQt的UI界面.在界面中可以选择人脸图片.视频进行检 ...
- 可爱的豆子——使用Beans思想让Python代码更易维护
title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...
- if __name__== "__main__" 的意思(作用)python代码复用
if __name__== "__main__" 的意思(作用)python代码复用 转自:大步's Blog http://www.dabu.info/if-__-name__ ...
随机推荐
- 信用评分卡 (part 3of 7)
python信用评分卡(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_camp ...
- PL/SQL Developper导入导出数据库的方法及说明
导出步骤 1 tools ->export user object 选择选项,导出.sql文件. 2 tools ->export tables-> Oracle Export 选择 ...
- eclipse+tomcat+maven+springmvc+mybatis+mysql集成WebService插件(Axis2+CXF)
$1 环境介绍 $1.1 Eclipse Java EE IDE for Web Developers:Neon.2 Release (4.6.2) $1.2 Maven:3.3.9 $1.3 Spr ...
- ACM-ICPC 2018 徐州赛区网络预赛 I Characters with Hash(模拟)
https://nanti.jisuanke.com/t/31461 题意 一个hash规则,每个字母映射成一个两位数,求给的字符串最后的编码位数,要求去除最终结果的前导零 分析 按题意模拟就是了 # ...
- Part-Six
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.
- HTML第二耍 列表标签
先复习下上一节 <!doctype html> <html> <head> <meta charset="utf-8"> <t ...
- Terminal 快捷键帮助
Terminal 快捷键帮助 Terminal 快捷键帮助 移动光标至行首 CTRL+A 第一次按时,移动光标至行首: 再次按时,回到原有位置 CTRL+X 移动光标至行尾 CTRL+E 光标向左移动 ...
- docker搭建lnmp环境
1.搭建lnmp网站平台 1.创建mysql数据库容器 docker run -it -d --name lnmp_mysql -p 3308:3306 -e MYSQL_ROOT_PASSWORD= ...
- 如何使用xss带cookie
参考连接:https://pentesterlab.com/exercises/xss_and_mysql_file/course 打开测试网站: 然后在192.168.1.46机器上使用socat, ...
- luogu 1484\1792 种树 奇怪的贪心可反悔
1484 种树 此版本是线性的,那么根据链表维护即可: 构建新点,点的左右分别是原整个区间的前驱及后继,再正常维护即可 注意两个版本的维护有所不同 第二个版本的维护直接将左右两点删除 1792 种树2 ...