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__ ...
随机推荐
- JAVA核心技术I---JAVA基础知识(单例模式和final关键字)
一:单例模式 C++设计模式中提及,不再赘述设计模式---对象性能模式之单例模式(Singleton) public class single{ static single Instance=new ...
- C#设计模式(5)——建造者模式
1.建造者模式介绍 在软件开发中,有时我们要创建一个复杂的对象,这个对象由几个子部件按一定的步骤组合而成,这时候我们就可以使用建造者模式了.说到建造者我们首先想到的是盖房子,盖房子简单的说有三个步骤: ...
- Mysql数据库进阶之(分表分库,主从分离)
前言:数据库的优化是一个程序员的分水岭,作为小白我也得去提前学习这方面的数据的 (一) 三范式和逆范式 听起范式这个迟非常专业我来举个简单的栗子: 第一范式就是: 把能够关联的每条数据都拆分成一个 ...
- JS盒模型
JS盒模型 ***** 1.width | height parseInt(getComputedStyle(ele, null).getPropertyValue('width')) parseIn ...
- SSM框架的搭建和测试(Spring+Spring MVC+MyBatis)
Spring MVC:MVC框架,通过Model-View-Controller模式很好的将数据,业务与展现进行分离. MyBatis:数据持久层框架 我这里使用的是MyEclipse 2016 CI ...
- Linux centos 防火墙篇
防火墙的关闭 service iptables stop 永久关闭 chkconfug iptables off 查看状态 service iptables status
- mysql日期时间函数(常用的)
mysql> SELECT NOW(); #返回(打印)当前日期和时间+---------------------+| NOW() |+---------------------+| 2017 ...
- JVM学习(一)简介
一.java程序编译到运行大概流程 1.Source Code Files为.java文件 2.通过编译产生可执行的字节码. 3.通过jvm得到机器可以执行的机器码 4.操作系统运行机器码,并与硬件进 ...
- Myschool乱码问题 和mysql 备份还原
show variables like 'character_set%'; alter table users modify username ) character set gbk; alter t ...
- json数据的处理和转化(loads/load/dump/dumps)
import requests import json url='https://movie.douban.com/j/search_subjects?type=movie&tag=%E7%8 ...