一个图片有三个通道RGB,每个通道就是一层数据
以一个图片为例子,从图片数据,再由数据到图片转化过程,理解数据与图形以及表示的关系
from PIL import Image
#打开图片
im = Image.open('tuzi.jpg')
#导入像素
pix = im.load()
#获取宽度
width = im.size[0]
print '图片宽%s'%width
#获取高度
height = im.size[1]
print '图片高%s'%height
# tuzi保存每个像素点值
tuzi = []
for x in range(height):
for y in range(width):
r, g, b = pix[y, x]
# 每个点像素包含rgb三个通道
# 注意这里读取顺序,我是横着读取,加到数组中,
# 如果竖着读取会把图搞反了,可以自己脑补下。
tuzi+=[r,g,b]
我们将tuzi,reshape成图片的样子
tuziArr = np.array(tuzi).reshape([height,width,3])
from matplotlib.font_manager import FontProperties
def getChineseFont():
return FontProperties(fname='/System/Library/Fonts/PingFang.ttc')
%matplotlib inline
from PIL import Image
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['figure.figsize'] = (20.0, 20.0)
# channel表示图片通道,取某一个通道数据方法[:,:,x]
def imshow(channel, ax, picture_data):
tt = np.array([0]*rt.shape[0]*rt.shape[1]*3).reshape(
[rt.shape[0],rt.shape[1], 3])
if channel == 3:
# 全部通道
tt = picture_data
else:
tt[:,:,channel]=picture_data[:,:,channel]
tt_img = np.array(tt, dtype='uint8')
ax.imshow(tt_img)
#help(ax)
plt.figure()
N = 4
fig, axes = plt.subplots(1,N)
for i in range(N):
imshow(i, axes[i], tuziArr)
plt.show()
# 生成0-26序列,reshape成[1,3,3,3],大小3X3,3通道,每个点就
# 是RGB三个值如[0,1,2]代表三个点像素值,看到点就是下面排列
# (0,1,2) (3,4,5) (6,7,8)
# (9 10 11) (12 13 14) (15 16 17 )
# (18 19 20) (21 22 23) (24 25 26)
用numpy来操作一下
temp = np.array(xrange(27)).reshape([1,3,3,3])
print temp
取0行1列所有通道值
temp[0,0,1,:]
取第二个通道值
temp[0,:,:,1]
下面用tensorflow来运算卷积
- filter只输出一个通道情况
# 生成0-26序列,reshape成[1,3,3,3],大小3X3,3通道,每个点就
# 是RGB三个值如[0,1,2]代表三个点像素值
# (0,1,2) (3,4,5) (6,7,8)
# (9 10 11) (12 13 14) (15 16 17 )
# (18 19 20) (21 22 23) (24 25 26)
# 一个图片,3X3,通道3
input = tf.constant(np.array(xrange(27)).reshape([1,3,3,3]),
dtype = tf.float32)
# 高1,宽1,输入通道3,输出通道1
filter = tf.constant(np.array(xrange(3)).reshape([1,1,3,1]),
dtype=tf.float32)
op = tf.nn.conv2d(input, filter, strides = [1,1,1,1],padding ='VALID')
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
input,filter, result = sess.run([input,filter, op])
print '------filter------'
print filter
print '------第0个通道------'
print input[0,:,:,0]
print '-----卷积结果------'
print(result)
输出是filter扫过
# (0,1,2) (3,4,5) (6,7,8)
# (9 10 11) (12 13 14) (15 16 17 )
# (18 19 20) (21 22 23) (24 25 26)
运算结果
- 输出两个通道情况
input = tf.constant(np.array(xrange(27)).reshape([1,3,3,3]),
dtype = tf.float32)
# 高1,宽1,输入通道3,输出通道2
filter = tf.constant(np.array(xrange(6)).reshape([1,1,3,2]),
dtype=tf.float32)
op = tf.nn.conv2d(input, filter, strides = [1,1,1,1],padding ='VALID')
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
input,filter, result = sess.run([input,filter, op])
print '------filter------'
print filter
print '------第0个通道------'
print input[0,:,:,0]
print '-----卷积结果------'
print(result)
输出是2个通道的filter扫过
# (0,1,2) (3,4,5) (6,7,8)
# (9 10 11) (12 13 14) (15 16 17 )
# (18 19 20) (21 22 23) (24 25 26)
的结果
现在加深一个难度,之前的卷积核filter是[1,1,3,1]和[1,1,3,2]
现在使用卷积核[2,2,3,1]
input
0,1,2 | 3,4,5 | 6,7,8 |
---|---|---|
9,10,11 | 12,13,14 | 15,16,17 |
18,19,20 | 21,22,23 | 24,25,26 |
filter
0,1,2 | 3,4,5 |
---|---|
6,7,8 | 9,10,11 |
# 一个图片,3X3,通道3
input = tf.constant(np.array(xrange(27)).reshape([1,3,3,3]),
dtype = tf.float32)
# 高2,宽2,输入通道3,输出通道1
filter = tf.constant(np.array(xrange(12)).reshape([2,2,3,1]),
dtype=tf.float32)
op = tf.nn.conv2d(input, filter, strides = [1,1,1,1],padding ='VALID')
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
input,filter, result = sess.run([input,filter, op])
print '------filter------'
print filter
print '-----卷积结果------'
print(result)
659 | 857 |
---|---|
1253 | 1451 |
现在使用卷积核[2,2,3,2]
input = tf.constant(np.array(xrange(18)).reshape([1,3,3,2]),
dtype = tf.float32)
# 高2,宽2,输入通道3,输出通道1
filter = tf.constant(np.array(xrange(32)).reshape([2,2,2,4]),
dtype=tf.float32)
op = tf.nn.conv2d(input, filter, strides = [1,1,1,1],padding ='VALID')
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
input,filter, result = sess.run([input,filter, op])
print input
print '------filter------'
print filter[:,:,:,0]
#print '------第0个通道------'
#print input[0,:,:,0]
print '-----卷积结果------'
print(result)
# input
# (0,1) (2,3) (4,5)
# (6,7) (9,9) (10,11)
# (12,13)(14,15)(16,17)
# filter
# [(0,1,2,3) (4,5,6,7)] [(8,9,10,11) (12,13,14,15)]
# [(16,17,18,19)(20,21,22,23)] [(24,25,26,27)(28,29,30,31)]