使用TensorFlow的卷积神经网络识别手写数字(1)-预处理篇
功能:
将文件夹下的20*20像素黑白图片,根据重心位置绘制到28*28图片上,然后保存。经过预处理的图片有利于数字的准确识别。参见MNIST对图片的要求。
此处可下载已处理好的图片:
https://files.cnblogs.com/files/hatemath/20-pixel-numbers.zip
https://files.cnblogs.com/files/hatemath/28-pixel-numbers.zip
# encoding: utf-8
import os from PIL import Image
import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib.cm as cm srcDir = '20-pixel-numbers'
dstDir = '28-pixel-numbers' #显示图片
def showImg(image):
plt.imshow(image,cmap=cm.binary)
plt.show() #按比例调整图片大小
def resizeImage(image,width=None,height=None,inter=cv2.INTER_AREA): #获取图像尺寸
(h,w) = image.shape[:2]
if width is None and height is None:
return image #高度算缩放比例 if(w > h):
newsize = (width,round(h / (w/width)))
else:
newsize = (round(w/ (h/height)), height) #print(newsize) # 缩放图像
newimage = cv2.resize(image, newsize, interpolation=inter)
return newimage #创建新的黑色图片
def createBianryImage(bg=(0,0,0), width=28, height=28): channels = 1 image = np.zeros((width,height,channels),np.uint8)#生成一个空灰度图像
#cv2.rectangle(image,(0,0),(width,height),bg,1, -1) return image.reshape(width, height) #两个不同大小的图片合并
def mergeImage(bg, fg, x, y):
bgH, bgW = bg.shape[:2]
fgH, fgW = fg.shape[:2] for i in range(fgH):
for j in range(fgW):
if(y+i < bgH and x+j < bgW):
#print('xx', y+i, x+j)
bg[y+i, x+j] = fg[i,j] # 这里可以处理每个像素点 return bg # 求像素重心。传入二值图像,其中白色点算重量,黑色点为空
def getBarycentre(image): h, w = image.shape[:2] sumWeightW = 0
sumWeightH = 0 count = 0 for i in range(h):
for j in range(w):
if(image[i,j] > 128):
sumWeightW += j
sumWeightH += i
count += 1 if(count == 0):
count = 1 print('getBarycentre: ', round(sumWeightW/count), round(sumWeightH/count) )
return (round(sumWeightW/count), round(sumWeightH/count)) def getFileList(strDir, strType='.png'):
lstSrcFiles = [] files = os.listdir(strDir)
for file in files:
if os.path.splitext(file)[1] == strType:
lstSrcFiles.append(file) return lstSrcFiles # 读取指定目录下的图片文件,图片为黑白格式,长、宽的最大值为20像素。
lstSrcFiles = getFileList(srcDir)
print (lstSrcFiles) for file in lstSrcFiles:
binary = cv2.imread(srcDir + '/' + file, cv2.IMREAD_GRAYSCALE) # 求像素重心
bcW, bcH = getBarycentre(binary) # 叠加到28x28的黑色图片上
xOffset = round(28/2 - bcW)
yOffset = round(28/2 - bcH) print('offset', xOffset, yOffset) # 另存为
cv2.imwrite(dstDir + '/' + file,
mergeImage(createBianryImage(), binary, xOffset, yOffset))
#binary)
使用TensorFlow的卷积神经网络识别手写数字(1)-预处理篇的更多相关文章
- 使用TensorFlow的卷积神经网络识别手写数字(2)-训练篇
import numpy as np import tensorflow as tf import matplotlib import matplotlib.pyplot as plt import ...
- 使用TensorFlow的卷积神经网络识别手写数字(3)-识别篇
from PIL import Image import numpy as np import tensorflow as tf import time bShowAccuracy = True # ...
- Tensorflow搭建卷积神经网络识别手写英语字母
更新记录: 2018年2月5日 初始文章版本 近几天需要进行英语手写体识别,查阅了很多资料,但是大多数资料都是针对MNIST数据集的,并且主要识别手写数字.为了满足实际的英文手写识别需求,需要从训练集 ...
- PyTorch基础——使用卷积神经网络识别手写数字
一.介绍 实验内容 内容包括用 PyTorch 来实现一个卷积神经网络,从而实现手写数字识别任务. 除此之外,还对卷积神经网络的卷积核.特征图等进行了分析,引出了过滤器的概念,并简单示了卷积神经网络的 ...
- TensorFlow卷积神经网络实现手写数字识别以及可视化
边学习边笔记 https://www.cnblogs.com/felixwang2/p/9190602.html # https://www.cnblogs.com/felixwang2/p/9190 ...
- 用BP人工神经网络识别手写数字
http://wenku.baidu.com/link?url=HQ-5tZCXBQ3uwPZQECHkMCtursKIpglboBHq416N-q2WZupkNNH3Gv4vtEHyPULezDb5 ...
- 卷积神经网络CNN 手写数字识别
1. 知识点准备 在了解 CNN 网络神经之前有两个概念要理解,第一是二维图像上卷积的概念,第二是 pooling 的概念. a. 卷积 关于卷积的概念和细节可以参考这里,卷积运算有两个非常重要特性, ...
- 第二节,TensorFlow 使用前馈神经网络实现手写数字识别
一 感知器 感知器学习笔记:https://blog.csdn.net/liyuanbhu/article/details/51622695 感知器(Perceptron)是二分类的线性分类模型,其输 ...
- 用Keras搭建神经网络 简单模版(三)—— CNN 卷积神经网络(手写数字图片识别)
# -*- coding: utf-8 -*- import numpy as np np.random.seed(1337) #for reproducibility再现性 from keras.d ...
随机推荐
- Codeforces Beta Round #96 (Div. 2) E. Logo Turtle dp
http://codeforces.com/contest/133/problem/E 题目就是给定一段序列,要求那个乌龟要走完整段序列,其中T就是掉头,F就是向前一步,然后开始在原点,起始方向随意, ...
- 51NOD 区间的价值 V2
http://www.51nod.com/contest/problem.html#!problemId=1674 因为题目要求的只是& 和 | 这两个运算.而这两个运算产生的值是有限的. & ...
- thymeleaf中th:attr用法以及相关的thymeleaf基本表达式
额,有人写的很好,我直接搬了 thymeleaf中th:attr用法 1.写死的单个属性值添加 th:attr="class=btn" 2.写死的多个属性值添加 th:attr=& ...
- Java中的switch语句——通过示例学习Java编程(8)
作者:CHAITANYA SINGH 来源:https://www.koofun.com//pro/kfpostsdetail?kfpostsid=19 当我们在代码逻辑中有多个选项,而且需要为每个选 ...
- uvm_reg_predictor——寄存器模型(十一)
保存寄存器的值 观察DUT寄存器值的变化. //---------------------------------------------------------------------------- ...
- 本号讯 | 永不消失的协作“空间站”开课;微软推出微软云Azure文档网站
8月29日,针对企业常面临的“协同办公”困难,开展以“还有这种操作?永不消失的协作'空间站'”为主题的协同办公培训课. 课程内容包含:在Office 365环境中,如何利用Teams与Groups等功 ...
- SharePoint Server 2016 WEB 网站浏览器支持
SharePoint Server 2016支持多种常用的Web浏览器,如Internet Explorer,Google Chrome,Mozilla Firefox,Apple Safari和Mi ...
- 用指针的方式实现,重写strrchr函数的功能
char *strchrTest(char * ptr,char c); Action(){ char str[]={"thisisadog"}; char c='s'; lr_o ...
- Android商城开发系列(四)——butterknife的使用
在上一篇博客:Android商城开发系列(三)——使用Fragment+RadioButton实现商城底部导航栏实现商城的底部导航栏时,里面用到了butterknife,今天来讲解一下的butterk ...
- Windows系统下查看文件编码类型
这是一个程序员的最基本的技能,原谅我到现在才去了解 以前只知道window操作系统下文件大部分默认编码是ANSI,中文版是GBK编码 如果想要查看或者修改文件编码的话有两种方式 一:用记事本打开文件, ...