图片卷积运算

作者: 阿发贝塔伽马 | 来源:发表于2018-05-12 18:04 被阅读15次

一个图片有三个通道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)]

相关文章

  • DFT与卷积

    利用DFT可以大大加快卷积运算的速度。卷积定理:空间域的卷积运算可以转换为频域的乘法运算。具体实现是将图片转换到频...

  • 图片卷积运算

    一个图片有三个通道RGB,每个通道就是一层数据以一个图片为例子,从图片数据,再由数据到图片转化过程,理解数据与图形...

  • 2020-02-17

    互相关运算与卷积运算 卷积层得名于卷积运算,但卷积层中用到的并非卷积运算而是互相关运算。我们将核数组上下翻转、左右...

  • 第二次打卡 Task05

    一、卷积神经网络基础 卷积层得名于卷积运算,但卷积层中用到的并非卷积运算而是互相关运算。我们将核数组上下翻转、左右...

  • Task7 卷积神经网络

    任务: 卷积运算的定义、动机(稀疏权重、参数共享、等变表示)。一维卷积运算和二维卷积运算。 反卷积(tf.nn.c...

  • Tensorflow学习笔记--CNN

    输入向量----------------------》与卷积核卷积运算----------------------...

  • CNN中卷积核与卷积运算的前向推导与推导过程

    CNN神经网络算法是常用的模式识别算法,该算法通过卷积运算将图片特征存储到多个卷积核中,卷积核通过算法的反向传输一...

  • 卷积神经网络

    知识点 卷积运算图像处理中的卷积运算与数学定义中的卷积不太一样,更准确的是指数学中的互相关运算。输入二矩阵通过每个...

  • 使用视觉化方法揭露卷积网络是如何从图片中抽取规律信息的

    我们在前几节介绍过卷积网络的运算原理,以及通过代码实践,体验到了卷积网络对图片信息抽取的有效性。现在一个问题在于,...

  • 卷积运算

    卷积运算 1 No zero padding, unit strides (无填充,单位滑动) 输入特征图大小 i...

网友评论

    本文标题:图片卷积运算

    本文链接:https://www.haomeiwen.com/subject/zlucdftx.html