如何为3D卷积构造Sobel滤波器? [英] How to construct a sobel filter for a 3d convolution?

查看:223
本文介绍了如何为3D卷积构造Sobel滤波器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码片段中,我想构造一个Sobel过滤器,该过滤器将分别应用于图像(RGB)的每一层,最后粘在一起(再次为rgb,但已过滤)。

In my code snippet, I want to construct Sobel filter which is applied to each layer of an image (RGB) separately and in the end stuck (again rgb, but filtered) together.

我不知道如何构造输入形状的Sobel过滤器[filter_depth,filter_height,filter_width,in_channels,out_channesl] ,就我而言:

I do not know how to construct the Sobel filter with input shape [filter_depth, filter_height, filter_width, in_channels, out_channesl], that is in my case:

 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.depthwise_conv2d

You can use tf.nn.depthwise_conv2d:

sobel_x = tf.constant([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], tf.float32)
kernel = tf.tile(sobel_x[...,None],[1,1,3])[...,None]
conv = tf.nn.depthwise_conv2d(image[None,...], kernel,strides=[1,1,1,1],padding='SAME')






使用 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卷积构造Sobel滤波器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆