使用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 ...
随机推荐
- 洛谷P4238【模板】多项式求逆
洛谷P4238 多项式求逆:http://blog.miskcoo.com/2015/05/polynomial-inverse 注意:直接在点值表达下做$B(x) \equiv 2B'(x) - A ...
- Core2.0 项目到2.1
Core2.0 项目到2.1 https://www.cnblogs.com/FlyLolo/p/ASPNETCore2_10.html .NET Core 2.1 终于发布了, 赶紧升级一下. 一. ...
- 【转】Maven项目中将配置文件打包到jar包中
参考博客:http://blog.csdn.net/ciedecem/article/details/10382275 问题: 项目中需要用到从文件中加载json数据,如图放在conf目录下. 程序中 ...
- 零基础逆向工程17_PE结构01_PE头解析_手动
PE文件的两种状态 1.在硬盘中 节省硬盘空间 硬盘对齐 内存对齐 2.在内存中 3.PE磁盘文件与内存映像结构图 PE文件为什么要分节 -- 手动解析:PE文件 分析软件:飞鸽传书http://ww ...
- web APP到底和跨平台APP开发有什么区别?
什么是web app? WebApp是指基于Web的系统和应用,其作用是向广大的最终用户发布一组复杂的内容和功能. 说的浅显易懂点儿就是因为移动互联网特别火爆,很多企业公司也都想拥有一个属于自己的ap ...
- python中__file__
用__file__ 来获得脚本所在的路径,比如文件在/root下 cat tee #!/usr/bin/env pythonprint __file__ #得到相对路径tee ...
- 干货|java缓存技术详解
一.缓存是什么? 请点击此处输入图片描述 Cache ①高速缓冲存储器,其中复制了频繁使用的数据以利于快速访问. ②位于速度相差较大的两种硬件/软件之间,用于协调两者数据传输速度差异的结构 二.缓存有 ...
- 大家一起和snailren学java-(一)对象导论
OOP,是java语言的特性.面向对象思想贯穿整个java开发. 那什么是面向对象呢?什么是对象? 在面向对象设计语言看来,万事万物都为对象.生活中的一个物体,有自己的属性,有自己的活动.比如一辆汽车 ...
- JavaScript_10_错误
Try...catch... throw <!DOCTYPE html> <html> <head> <title></title> < ...
- 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector
题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her s ...