功能:

  将文件夹下的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)-预处理篇的更多相关文章

  1. 使用TensorFlow的卷积神经网络识别手写数字(2)-训练篇

    import numpy as np import tensorflow as tf import matplotlib import matplotlib.pyplot as plt import ...

  2. 使用TensorFlow的卷积神经网络识别手写数字(3)-识别篇

    from PIL import Image import numpy as np import tensorflow as tf import time bShowAccuracy = True # ...

  3. Tensorflow搭建卷积神经网络识别手写英语字母

    更新记录: 2018年2月5日 初始文章版本 近几天需要进行英语手写体识别,查阅了很多资料,但是大多数资料都是针对MNIST数据集的,并且主要识别手写数字.为了满足实际的英文手写识别需求,需要从训练集 ...

  4. PyTorch基础——使用卷积神经网络识别手写数字

    一.介绍 实验内容 内容包括用 PyTorch 来实现一个卷积神经网络,从而实现手写数字识别任务. 除此之外,还对卷积神经网络的卷积核.特征图等进行了分析,引出了过滤器的概念,并简单示了卷积神经网络的 ...

  5. TensorFlow卷积神经网络实现手写数字识别以及可视化

    边学习边笔记 https://www.cnblogs.com/felixwang2/p/9190602.html # https://www.cnblogs.com/felixwang2/p/9190 ...

  6. 用BP人工神经网络识别手写数字

    http://wenku.baidu.com/link?url=HQ-5tZCXBQ3uwPZQECHkMCtursKIpglboBHq416N-q2WZupkNNH3Gv4vtEHyPULezDb5 ...

  7. 卷积神经网络CNN 手写数字识别

    1. 知识点准备 在了解 CNN 网络神经之前有两个概念要理解,第一是二维图像上卷积的概念,第二是 pooling 的概念. a. 卷积 关于卷积的概念和细节可以参考这里,卷积运算有两个非常重要特性, ...

  8. 第二节,TensorFlow 使用前馈神经网络实现手写数字识别

    一 感知器 感知器学习笔记:https://blog.csdn.net/liyuanbhu/article/details/51622695 感知器(Perceptron)是二分类的线性分类模型,其输 ...

  9. 用Keras搭建神经网络 简单模版(三)—— CNN 卷积神经网络(手写数字图片识别)

    # -*- coding: utf-8 -*- import numpy as np np.random.seed(1337) #for reproducibility再现性 from keras.d ...

随机推荐

  1. 解决git从remote clone后所有文件都改变的问题

    遇到2次这种情况了,git从remote clone项目代码后发现所有文件都要改变,因为权限改变了,可以通过git来设置忽略权限变化 git config --global core.fileMode ...

  2. B -- RE:从零开始的异世界生活 线段树

    http://www.ifrog.cc/acm/problem/1117?contest=1016&no=1 其实我是第一次这样用线段树. 首先把所有出现过的数字全部离散化.那么数字就是从[1 ...

  3. 漫谈Code Review的错误实践

    从刚开始工作时到现在,已经写了7年的代码,大部分代码都被人review过,自己也review了很多人的代码.在上一家公司的时候,我负责的一轮面试是专门进行Code Review的练习和经验谈. 通过在 ...

  4. Windows3

    windows安装后的配置 没有网络适配器, 将USB中的驱动精灵的安装程序安装在win上, 启动精灵, 提示无法连接到网络, 使用Android类型的手机中的QQ浏览器扫码下载 win会有一些开机自 ...

  5. Java基础学习_01 概述及环境配置

    一.概述 1.Java语言平台版本 1.1J2SE(Java 2 Platform Standard Edition)标准版 为开发普通桌面和商务应用程序提供的解决方案,该技术体系是其他两者的基础,可 ...

  6. 零基础逆向工程18_PE结构02_联合体_节表_PE加载过程

    联合体 特点 1.联合体的成员是共享内存空间的 2.联合体的内存空间大小是联合体成员中对内存空间大小要求最大的空间大小 3.联合体最多只有一个成员有效 节表数据结构说明 PE 加载 过程 FileBu ...

  7. elasticsearch查询方式

    1.query string a).GET /index/type/_search ===>>查询所有 b).GET /index/type/_search?q=filed:value&a ...

  8. Docker搭建

    环境基于CentOS64位,内核最好3.10. 1.下载安装 docker      dockersudo yum install docker-io (假如内核版本太低的话,会在下载安装Docker ...

  9. Python3+Selenium3+webdriver学习笔记11(cookie处理)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记11(cookie处理)'''from selenium im ...

  10. Autoit3 如何捕足控件

    以任务管理器为例,在命令行提示符下输入taskmgr.exe 接下来识别这个窗口上的控件 在AU3  中提供了一个捕捉控件的工具是Au3Info.exe 这里记录了控件的标题,控件的类型,控件的坐标和 ...