由于卷积神经网络主要用于图像的分类识别,所以本文中的数据预处理是对图像数据进行操作。之前卷积神经网络是对手写数字数据做识别,而所有的数字都被看成是28×28像素的灰度图被输入到网络中进行训练所以为了能直接使用网上deep learning教程中的卷积神经网络代码,这里对所有的数据也均是处理成28×28像素的灰度图进行处理,将图片作为输入实际上是将图片对应的二维矩阵输入到卷积神经网络模型中进行训练。
1.基于opencv的将图像转换成二维数组的代码:
import cv
import cPickle
import os, sys
from stat import *
#import Image
import pylab
from PIL import Image
import numpy
PicPathNameList = []
PicWidthList = []
PicHeightList = []
def WalkTree(top, callback):
for f in os.listdir(top):
pathname = os.path.join(top, f)
mode = os.stat(pathname)[ST_MODE]
if S_ISDIR(mode):
WalkTree(pathname, callback)
elif S_ISREG(mode):
callback(pathname)
else:
print 'Skipping %s' % pathname
def GetPicInfo(file):
global PicPathNameList
global PicWidthList
global PicHeightList
try:
image = Image.open(file)
PicPathNameList.append(file)
PicWidthList.append(image.size[0])
PicHeightList.append(image.size[1])
except IOError:
pass
def JReduce(image,m,n):
H = 28
W = 28
size = (W,H)
iJReduce = cv.CreateImage(size,image.depth,image.nChannels)
for i in range(H):
for j in range(W):
x1 = int(i/m)
x2 = int((i+1)/m)
y1 = int(j/n)
y2 = int((j+1)/n)
sum = [0,0,0]
for k in range(x1,x2):
for l in range(y1,y2):
sum[0] = sum[0]+image[k,l][0]
sum[1] = sum[1]+image[k,l][1]
sum[2] = sum[2]+image[k,l][2]
num = (x2-x1)*(y2-y1)
iJReduce[i,j] = (sum[
版权声明:本文为wj164原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。