tensorflow隨筆-tf.nn.conv2d卷積運算(10)
阿新 • • 發佈:2018-12-15
運動模糊
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 2 13:23:27 2018
@author: myhaspl
@email:[email protected]
tf.nn.conv2d處理影象
運動模糊
"""
import tensorflow as tf
from PIL import Image
import numpy as np
g=tf.Graph()
with g.as_default():
def getImageData(fileNameList) :
imageData=[]
for fn in fileNameList:
testImage = Image.open(fn).convert('L')
testImage.show()
imageData.append(np.array(testImage)[:,:,None])
return np.array(imageData,dtype=np.float32)
imageFn=("dog.png",)
imageData= getImageData(imageFn)
testData=tf.constant(imageData)
kernel=tf.constant(np.array(
[
[[[1.]],[[0.]],[[0.]]],
[[[0.]],[[1.]],[[0.]]],
[[[0.]],[[0.]],[[1.]]]
])/3.
,dtype=tf.float32)#3*3*1*1
y=tf.cast(tf. nn.conv2d(testData,kernel,strides=[1,1,1,1],padding="SAME"), dtype=tf.int32)
init_op = tf.global_variables_initializer()
with tf.Session(graph=g) as sess:
print testData.get_shape()
print kernel.get_shape()
resultData=sess.run(y)[0]
resultData=resultData.reshape(resultData.shape[0],resultData.shape[1])
resulImage=Image.fromarray(np.uint8(resultData),mode='L')
resulImage.show()
print y.get_shape()