Python 2.7 如何为3d卷积构造sobel滤波器?
在我的代码片段中,我想构造Sobel过滤器,它分别应用于图像(RGB)的每一层,最后粘在一起(同样是RGB,但经过过滤) 我不知道如何使用输入形状构建Sobel滤波器Python 2.7 如何为3d卷积构造sobel滤波器?,python-2.7,tensorflow,machine-learning,computer-vision,sobel,Python 2.7,Tensorflow,Machine Learning,Computer Vision,Sobel,在我的代码片段中,我想构造Sobel过滤器,它分别应用于图像(RGB)的每一层,最后粘在一起(同样是RGB,但经过过滤) 我不知道如何使用输入形状构建Sobel滤波器[滤波器深度、滤波器高度、滤波器宽度、输入通道、输出通道],在我的情况下: sobel_x_filter = tf.reshape(sobel_x, [1, 3, 3, 3, 3]) 整个代码如下所示: import numpy as np import tensorflow as tf import matplotlib.p
[滤波器深度、滤波器高度、滤波器宽度、输入通道、输出通道]
,在我的情况下:
sobel_x_filter = tf.reshape(sobel_x, [1, 3, 3, 3, 3])
整个代码如下所示:
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
im0 = plt.imread('../../data/im0.png') # already divided by 255
sobel_x = tf.constant([
[[[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]],
[[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]],
[[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]],
[[[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]],
[[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]],
[[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]],
[[[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]],
[[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]],
[[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]]], tf.float32) # is this correct?
sobel_x_filter = tf.reshape(sobel_x, [1, 3, 3, 3, 3])
image = tf.placeholder(tf.float32, shape=[496, 718, 3])
image_resized = tf.expand_dims(tf.expand_dims(image, 0), 0)
filters_x = tf.nn.conv3d(image_resized, filter=sobel_x_filter, strides=[1,1,1,1,1],
padding='SAME', data_format='NDHWC')
with tf.Session('') as sess:
sess.run([tf.global_variables_initializer(), tf.local_variables_initializer()])
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
feed_dict = {image: im0}
img = filters_x.eval(feed_dict=feed_dict)
plt.figure(0), plt.title('red'), plt.imshow(np.squeeze(img[...,0])),
plt.figure(1), plt.title('green'), plt.imshow(np.squeeze(img[...,1])),
plt.figure(2), plt.title('blue'), plt.imshow(np.squeeze(img[...,2]))
您可以使用:
使用
tf.nn.conv3d
:
im = tf.expand_dims(tf.transpose(image, [2, 0, 1]),0)
sobel_x = tf.constant([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], tf.float32)
sobel_x_filter = tf.reshape(sobel_x, [1, 3, 3, 1, 1])
conv = tf.transpose(tf.squeeze(tf.nn.conv3d(im[...,None], sobel_x_filter,
strides=[1,1,1,1,1],padding='SAME')), [1,2,0])
在我看来,你并不是在尝试对图像的每个通道应用3D卷积,而是2D卷积。确切地说,我是在尝试理解tensorlfow函数。不。我想用tf.nn。conv3d@j35t3r为tf.nn.conv3d添加了
im = tf.expand_dims(tf.transpose(image, [2, 0, 1]),0)
sobel_x = tf.constant([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], tf.float32)
sobel_x_filter = tf.reshape(sobel_x, [1, 3, 3, 1, 1])
conv = tf.transpose(tf.squeeze(tf.nn.conv3d(im[...,None], sobel_x_filter,
strides=[1,1,1,1,1],padding='SAME')), [1,2,0])