import cv2
import _pickle as cPickle
import numpy as np
import gzip
#
# obj = {"a": 1, "b": 2, "c": 3}
# pickle.dump(obj, open("tmp.txt", "wb"))
# obj2 = pickle.load(open("tmp.txt", "rb"))
# print(obj2)
# print(obj2 == obj)
def vectorized_result(j):
e = np.zeros((10, 1))
e[j] = 1.0
return e
#
mnist = gzip.open('./data/mnist.pkl.gz', 'rb')
training_data, classification_data, test_data = cPickle.load(mnist,encoding='bytes')
mnist.close()
tr_d, va_d, te_d = training_data, classification_data, test_data
# print(tr_d[0].shape) # (50000, 784) 说明每一行的维度是784(图像的像素)
# for x in tr_d[0]: 表示每一行 进行转换为 np.reshape(x, (784, 1))
training_inputs = [np.reshape(x, (784, 1)) for x in tr_d[0]]
# 转换后的维度: (50000, 784, 1)
# print(np.array(training_inputs).shape)
# training_results是一个list 转换为np.array后 为:(50000, 10, 1)
training_results = [vectorized_result(y) for y in tr_d[1]]
# print(type(tr_d[1])) # <class 'numpy.ndarray'>
# for y in tr_d[1]:
# print(vectorized_result(y))
# print(type(vectorized_result(y)))
# print(vectorized_result(y).shape)
# print(training_results)
# print(np.array(training_results).shape)
# training_inputs: (50000, 784, 1) list类型
# training_results: (50000, 10, 1) list类型
training_data = zip(training_inputs, training_results)
# print(training_data) # <zip object at 0x0000027701EB4BC8>
# print(type(va_d[0])) # <class 'numpy.ndarray'>
# print(va_d[0].shape) # (10000, 784)
validation_inputs = [np.reshape(x, (784, 1)) for x in va_d[0]]
# print(np.array(validation_inputs).shape) # shape: (10000, 784, 1)
# print(va_d[1].shape) # (10000,)
# validation_inputs: (10000, 784, 1) va_d[1]:(10000,)
validation_data = zip(validation_inputs, va_d[1])
# print(validation_data) # <zip object at 0x0000016AB0AC5C48>
# print(type(te_d[0])) # <class 'numpy.ndarray'>
# print(te_d[0].shape) # (10000, 784)
test_inputs = [np.reshape(x, (784, 1)) for x in te_d[0]]
# print(np.array(test_inputs).shape) # list类型 shape: (10000, 784, 1)
#
# print(te_d[1].shape) # np.array() (10000,)
# test_data = zip(test_inputs, te_d[1]) # <zip object at 0x0000027701EB4BC8>
tr, val, test = training_data, validation_data, test_data
# ANN网络的建立
# cv2.ml.ANN_MLP_create函数
ann = cv2.ml.ANN_MLP_create()
# 设置网络的层数
ann.setLayerSizes(np.array([784, 20, 10]))
# 设置ANN_MLP_RPROP方法
ann.setTrainMethod(cv2.ml.ANN_MLP_RPROP)
# cv2.ml.ANN_MLP_SIGMOID_SYM: 设置sigmod为激活函数
ann.setActivationFunction(cv2.ml.ANN_MLP_SIGMOID_SYM)
# 设置终止条件ann.setTermCriteria
ann.setTermCriteria(( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 100, 1 ))
samples = 100
counter = 0
# 训练集部分
# 使用for循环来处理类型:zip object at 0x0000027701EB4BC8>
for img in tr:
if (counter > samples):
break
if (counter % 10 == 0):
print("Trained %d/%d" % (counter, samples))
counter += 1
data, digit = img
# #
# print(type(data)) # np.array
# print(data.shape) # shape: (784, 1)
# print(digit.shape) # (10, 1)
# 输入:
# np.array([data.ravel()], dtype=np.float32) (784,)
# np.array([data.ravel()], dtype=np.float32) (10,)
ann.train(np.array([data.ravel()], dtype=np.float32), cv2.ml.ROW_SAMPLE,
np.array([digit.ravel()], dtype=np.float32))
# print(digit.shape) # (10, 1)
# print(digit.ravel().shape) # (10,)
# r1 在 r2 内
def inside(r1, r2):
x1,y1,w1,h1 = r1
x2,y2,w2,h2 = r2
if (x1 > x2) and (y1 > y2) and (x1+w1 < x2+w2) and (y1+h1< y2 + h2):
return True
else:
return False
#### 测试部分实现
font = cv2.FONT_HERSHEY_SIMPLEX
path = "./images/numbers.jpg" # 是一个包含多个数字的图片
img = cv2.imread(path, cv2.IMREAD_UNCHANGED)
bw = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
bw = cv2.GaussianBlur(bw, (7,7), 0)
ret, thbw = cv2.threshold(bw, 135, 255, cv2.THRESH_BINARY_INV)
thbw = cv2.erode(thbw, np.ones((2,2), np.uint8), iterations = 2)
# cv2.imshow('img',thbw)
# cv2.waitKey(0)
image, cntrs, hier = cv2.findContours(thbw.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
rectangles = []
# 数字的提取
# 每一个选中的几何区域
for c in cntrs:
r = x, y, w, h = cv2.boundingRect(c)
a = cv2.contourArea(c)
# 整幅img图像大小
b = (img.shape[0] - 3) * (img.shape[1] - 3)
# 滤除一些矩形
is_inside = False
# rectangles存储的有效矩形
# 打算要不要存储的矩形r
for q in rectangles:
if inside(r, q):
is_inside = True
break
#
if not is_inside:
if not a == b:
rectangles.append(r)
# 将选择的矩形区域变成正方形区域
def wrap_digit(rect):
# x, y, w, h
x, y, w, h = rect
padding = 5
hcenter = x + w/2
vcenter = y + h/2
roi = None
if (h > w):
w = h
x = hcenter - (w/2)
else:
h = w
y = vcenter - (h/2)
return (x-padding, y-padding, w+padding, h+padding)
# ann: 训练好的网络模型 sample:ROI正方形区域
def predict(ann, sample):
resized = sample.copy()
rows, cols = resized.shape
if (rows != 28 or cols != 28) and rows * cols > 0:
resized = cv2.resize(resized, (28, 28), interpolation=cv2.INTER_LINEAR)
# ann.predict resized.ravel()--> (784,)
return ann.predict(np.array([resized.ravel()], dtype=np.float32))
/*
开发不易,整理也不易,如需要详细的说明文档和程序,以及完整的数据集,训练好的模型,或者进一步开发,
可加作者新联系方式咨询,WX:Q3101759565,QQ:3101759565
*/
没有合适的资源?快使用搜索试试~ 我知道了~
Opencv实现程序-部分12
共4个文件
png:3个
txt:1个
需积分: 5 0 下载量 24 浏览量
2024-03-17
11:56:07
上传
评论
收藏 772KB ZIP 举报
温馨提示
最近整理了Opencv的学习内容实现代码,每个代码都在本机上运行通过的,通过这些程序,可以实现某些经典的功能,也可以对Opencv有个熟悉学习的过程. (1)图片的读取和写入功能实现代码; (2)对像素值的赋值实现代码; (3)Img.itemset的功能实现代码; (4)对图像通道的操作功能实现代码; (5)对图像ROI区域进行操作功能实现代码; (6)Img.imread属性返回操作功能实现代码; (7)视频图像的读取操作功能实现代码; (8)视频图像的读取和写入功能实现代码; (9)对鼠标和键盘的检测和画图功能实现代码; (10)对摄像头(电脑摄像头/USB摄像头)的操作功能实现代码; (11)对摄像头采集视频进行图片捕获以及反转处理保存功能实现代码; (12)对读取的图片进行滤波变换处理功能实现代码; (13)对读取图片进行中值滤波、Laplacian变换、分解各通道并进行融合功能实现代码; (14)对摄像头捕获实时图像进行灰度变换以及Laplacian变换实时显示功能实现代码; (15)实现一个类对设定卷积核进行图像滤波处理功能实现代码; ..........
资源推荐
资源详情
资源评论
收起资源包目录
12.zip (4个子文件)
12
视频图像的读取操作功能实现代码.png 263KB
图片的读取和写入功能实现代码.png 356KB
视频图像的读取和写入功能实现代码.png 162KB
使用基于Opencv_ANN智能神经网络实现数字识别,并在一张含有多个数字自动识别并一一标注的实现代码.txt 5KB
共 4 条
- 1
资源评论
迪哥_AI_人工智能
- 粉丝: 539
- 资源: 128
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功