# -*- 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代码的更多相关文章

  1. Python 代码实现验证码识别

    Python 代码实现验证码识别 测试开发社区  1周前 源 /  j_hao104 一.探讨 识别图形验证码可以说是做爬虫的必修课,涉及到计算机图形学,机器学习,机器视觉,人工智能等等高深领域…… ...

  2. python代码 构建验证码

    1.python代码编写 (随机验证码): #coding: utf-8 import Image, ImageDraw, ImageFont, ImageFilter import string, ...

  3. Python代码样例列表

    扫描左上角二维码,关注公众账号 数字货币量化投资,回复“1279”,获取以下600个Python经典例子源码 ├─algorithm│       Python用户推荐系统曼哈顿算法实现.py│    ...

  4. 30行Python代码实现人脸检测

    参考OpenCV自带的例子,30行Python代码实现人脸检测,不得不说,Python这个语言的优势太明显了,几乎把所有复杂的细节都屏蔽了,虽然效率较差,不过在调用OpenCV的模块时,因为模块都是C ...

  5. python+opencv中最近出现的一些变化( OpenCV 官方的 Python tutorial目前好像还没有改过来?) 记一次全景图像的拼接

    最近在学习过程中发现opencv有了很多变动, OpenCV 官方的 Python tutorial目前好像还没有改过来,导致大家在学习上面都出现了一些问题,现在做一个小小的罗列,希望对大家有用 做的 ...

  6. Python面向对象编程扑克牌发牌程序,另含大量Python代码!

    1. 题目 编写程序, 4名牌手打牌,计算机随机将52张牌(不含大小鬼)发给4名牌手,在屏幕上显示每位牌手的牌. 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不 ...

  7. 基于深度学习的人脸性别识别系统(含UI界面,Python代码)

    摘要:人脸性别识别是人脸识别领域的一个热门方向,本文详细介绍基于深度学习的人脸性别识别系统,在介绍算法原理的同时,给出Python的实现代码以及PyQt的UI界面.在界面中可以选择人脸图片.视频进行检 ...

  8. 可爱的豆子——使用Beans思想让Python代码更易维护

    title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...

  9. if __name__== "__main__" 的意思(作用)python代码复用

    if __name__== "__main__" 的意思(作用)python代码复用 转自:大步's Blog  http://www.dabu.info/if-__-name__ ...

随机推荐

  1. linux环境下遇到的所有问题

    启动redis # 进去到src目录下,指定配置文件启动 ./redis-server ../redis.conf 设置外网访问 更改redis.conf 文件 bind 127.0.0.1 prot ...

  2. Kafka权威指南 读书笔记之(一)初识Kafka

    发布与订阅消息系统 数据(消息)的发送者(发布者)不会直接把消息发送给接收者,这是发布与订阅消息系统的一个特点.发布者以某种方式对消息进行分类,接收者(订阅者)订阅它们, 以便接收特定类型的消息.发布 ...

  3. ubuntu linux下建立stm32开发环境: 程序烧录 openocd+openjtag

    原文出处: http://blog.csdn.net/embbnux/article/details/17619621 之前建立stm32开发环境,程序也已经编译好生成main.bin,接下来就是要把 ...

  4. jmeter(高并发测试)

    1.首先jmeter需要JDK8以上得运行环境 2.下载jmeter,官方网址:http://jmeter.apache.org/download_jmeter.cgi 3.安装jmeter.jmet ...

  5. jenkins笔记

    java -jar jenkins.war -httpPort=9090 以9090端口启动jekins的web应用(内置jetty)

  6. golang实现tcp编程

    实现简单的tcp服务 package main import ( "fmt" "net" ) func main() { fmt.Println("服 ...

  7. js中html拼接

    https://i.cnblogs.com/EditPosts.aspx?postid=10620765&update=1

  8. 队列 Queue 与 生产者消费模型

    队列:先进先出 # from multiprocessing import Queue # Q = Queue(4) # Q.put('a') # Q.put('b') # Q.put('b') # ...

  9. vue加载本地json文件

    背景:做地区跟行业级联下拉选择,因为想做成可以搜索的,所以必须一次加载数据,后台有做memcache缓存,但因为数据量大,还是比较费时间,所以做成本地文件,简单记录一下 准备数据,放到static下 ...

  10. Iterator迭代器

    java.util.Iterator 迭代器iterator,是一个接口,不能够直接使用,需要使用Iterator接口的实现类对象,而获取实现类的的对象的方式为: Collection接口中有一个方法 ...