使用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 ...
随机推荐
- Python中输出字体的颜色设置
1.实现过程 终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无关.控制字符颜色的转义序列是以ESC开头,即用\033来完成 2.书写过程 开头部分: \033[显示方式;前 ...
- Spring 顾问
1.名称匹配方法切入点顾问 接口:ISomeService public interface ISomeService { public void doSome(); public void doSe ...
- IntelJ IDEA创建简单Java工程
1. 打开IDEA J 2017开发工具,如下图: 2.点击“Create New Project”,将打开以下图: 3.选择 “Java”,选择“Next”,打开下图: 4.“Create pro ...
- B/S架构 C/S架构 SOA架构
一.什么是C/S和B/S 第一.什么是C/S结构.C/S (Client/Server)结构,即大家熟知的客户机和服务器结构.它是软件系统体系结构,通过它可以充分利用两端硬件环境的优势,将任务合理分配 ...
- Myeclipse发布第一个jsp页面及web project部署到tomcat上的几种方法
菜鸟日记: 1:new web project: 2:fix the visiting path of the tomcat,打开在安装目录下conf目录中的server.xml,在</Hos ...
- 192.168.28.168:3000打开网页无法调试localhost为前缀的接口
最近我在IIS上发布.net Core API的时候发现 当我用localhost去打开我的web项目时 并且,ajax调用的接口前缀为localhost时候,运行正确 但是当我用192.168.28 ...
- Swing---WindowConstants
Java桌面开发过程中,很多人都写过类似下面的代码. import javax.swing.JFrame; public class SimpleFrame { public static void ...
- Beginning Python Chapter 1 Notes
James Payne(American)编写的<Beginning Python>中文译作<Python入门经典>,堪称是Python的经典著作. 当然安装Python是很简 ...
- SIGGRAPH 2017:深度学习与计算机图形学的碰撞
每年由美国计算机协会(Association of Computing Machinery,简称ACM)计算机图形专业组举办的年会SIGGRAPH,是全球最负盛名的图形学和交互技术盛会.今年已经是这场 ...
- PostgreSQL: epoch 新纪元时间的使用
新纪元时间 Epoch 是以 1970-01-01 00:00:00 UTC 为标准的时间,将目标时间与 1970-01-01 00:00:00时间的差值以秒来计算 ,单位是秒,可以是负值; 有些应用 ...