import matplotlib.pyplot as plt
import matplotlib.image as mping
import numpy as np
import tensorflow as tf
img = mping.imread("lena-flipped.bmp")
plt.imshow(img)
plt.axis("off")
plt.show()
print(img.shape)
image = np.reshape(img,[1,512,512,3])
input_image = tf.Variable(tf.constant(1.0,shape=[1,512,512,3]))
filter = tf.Variable(tf.constant([[-1.0,-1.0,-1.0],[0,0,0],[1.0,1.0,1.0],
[-2.0,-2.0,-2.0], [0,0,0], [2.0,2.0,2.0],
[-1.0,-1.0,-1.0], [0,0,0], [1.0,1.0,1.0]],
shape=[3,3,3,1]))
conv1 =tf.nn.conv2d(input_image,filter=filter,strides=[1,1,1,1],padding="SAME")
# 图片归一化
norm = tf.cast(((conv1-tf.reduce_min(conv1))/(tf.reduce_max(conv1)-tf.reduce_min(conv1))), tf.float32)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
output,_ = sess.run([norm,filter],feed_dict={input_image:image})
t = np.squeeze(output,axis=[0,3])
plt.imshow(t,cmap="Greys_r")
plt.axis("off")
plt.show()
提取前:
提取后: