tensorflow随笔-tf.nn.conv2d卷积运算(9)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010255642/article/details/83064654

图像模糊化的卷积
1.平均
在这里插入图片描述
在这里插入图片描述
右边的雪里红炒肉是被模糊化处理了

#!/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=("xlh.png",)
    imageData=getImageData(imageFn)
    testData=tf.constant(imageData)
    kernel=tf.constant(
            [
                   [[[1/9.]],[[1/9.]],[[1/9.]]],
                   [[[1/9.]],[[1/9.]],[[1/9.]]], 
                   [[[1/9.]],[[1/9.]],[[1/9.]]]
            ]
            )#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()

猜你喜欢

转载自blog.csdn.net/u010255642/article/details/83064654