卷积实现+对一幅图片的操作 ==sobel算子

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

import matplotlib.pyplot as plt
import matplotlib.image as mping
import numpy as np
import tensorflow as tf
mying=mping.imread('img.jpg')
plt.imshow(mying)
plt.axis('off')
plt.show()
print(mying.shape)
full=np.reshape(mying,[1,768,1024,3])
inputfull=tf.Variable(tf.constant(1.0,shape=[1,768,1024,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]))
op=tf.nn.conv2d(inputfull,filter,strides=[1,1,1,1],padding='SAME')
o=tf.cast(((op-tf.reduce_min(op))/(tf.reduce_max(op)-tf.reduce_min(op)))*255,tf.uint8)
with tf.Session()as sess:
    sess.run(tf.global_variables_initializer())
    t,f=sess.run([o,filter],feed_dict={inputfull:full})
    t=np.reshape(t,[768,1024])
    plt.imshow(t,cmap='Greys_r')
    plt.axis('off')
    plt.show()

原图:

卷积之后:

猜你喜欢

转载自blog.csdn.net/Dian1pei2xiao3/article/details/82345367