Tensorflow 张量相对于元素邻域的条件值
我正在使用Tensorflow实现Canny算法(需要使用边界作为评估指标,但这是离题的)。其中一个步骤是计算“非最大抑制”,这包括将3x3区域中的中心元素归零,除非两个特定邻居较小。更多细节 如何使用Tensorflow实现此操作 我实际上使用的是Keras,但Tensorflow解决方案也可以使用,作为参考,我的代码如下所示:Tensorflow 张量相对于元素邻域的条件值,tensorflow,keras,canny-operator,Tensorflow,Keras,Canny Operator,我正在使用Tensorflow实现Canny算法(需要使用边界作为评估指标,但这是离题的)。其中一个步骤是计算“非最大抑制”,这包括将3x3区域中的中心元素归零,除非两个特定邻居较小。更多细节 如何使用Tensorflow实现此操作 我实际上使用的是Keras,但Tensorflow解决方案也可以使用,作为参考,我的代码如下所示: def canny(img): '''Canny border detection. The input should be a grayscale imag
def canny(img):
'''Canny border detection. The input should be a grayscale image.'''
gauss_kernel = np.array([[2, 4, 5, 4, 2],
[4, 9, 12, 9, 4],
[5, 12, 15, 12, 5],
[4, 9, 12, 9, 4],
[2, 4, 5, 4, 2]]).reshape(5, 5, 1, 1)
gauss_kernel = K.variable(1./159 * gauss_kernel)
Gx = K.variable(np.array([[-1., 0. ,1.],
[-2., 0., 2.],
[-1., 0., 1.]]).reshape(3, 3, 1, 1))
Gy = K.variable(np.array([[-1., -2., -1.],
[ 0., 0., 0.],
[ 1., 2., 1.]]).reshape(3, 3, 1, 1))
# Smooth image
smoothed = K.conv2d(img, gauss_kernel, padding='same')
# Derivative in x
Dx = K.conv2d(smoothed, Gx, padding='same')
# Derivative in y
Dy = K.conv2d(smoothed, Gy, padding='same')
# Take gradient strength
G = K.sqrt(K.square(Dx) + K.square(Dy))
# TODO: Non-maximum Suppression & Hysteresis Thresholding
return G
您可以使用卷积滤波器分离两个目标像素,并使它们与中心像素“同心” 例如,为了与两个目标像素进行比较,我们可以使用此过滤器,形状为
(3,3,1,2)
——一个输入通道,两个输出通道。每个通道将返回一个目标像素
过滤器在目标像素处应有1个。其余为零:
#taking two diagonal pixels
filter = np.zeros((3,3,1,2))
filter[0,0,0,0] = 1 #first pixel is top/left, passed to the first channel
filter[2,2,0,1] = 1 #second pixel is bottom/right, passed to the second channel
#which ones are really bottom or top, left or right depend on your preprocessing,
#but they should be consistent with the rest of your modeling
filter = K.variable(filter)
如果你选择顶部和底部,或者左侧和右侧,你可以制作更小的过滤器。不需要是3x3(也没问题),但只需要1x3或3x1:
filter1 = np.zeros((1,3,1,2)) #horizontal filter
filter2 = np.zeros((3,1,1,2)) #vertical filter
filter1[0,0,0,0] = 1 #left pixel - if filter is 3x3: [1,0,0,0]
filter1[0,2,0,1] = 1 #right pixel - if filter is 3x3: [1,2,0,1]
filter1 = K.variable(filter1)
filter2[0,0,0,0] = 1 #top pixel - if filter is 3x3: [0,1,0,0]
filter2[2,0,0,1] = 1 #bottom pxl - if filter is 3x3: [2,1,0,1]
filter2 = K.variable(filter2)
然后将这些作为卷积应用。您将获得一个通道用于一个像素,另一个通道用于另一个像素。然后,您可以比较它们,就好像它们都在同一个地方,只是在不同的通道中:
targetPixels = K.conv2d(originalImages, kernel=filter, padding='same')
#two channels telling if the center pixel is greater than the pixel in the channel
isGreater = K.greater(originalImages,targetPixels)
#merging the two channels, considering they're 0 for false and 1 for true
isGreater = K.cast(isGreater,K.floatx())
isGreater = isGreater[:,:,:,:1] * isGreater[:,:,:,1:]
#now, the center pixel will remain if isGreater = 1 at that position:
result = originalImages * isGreater
我尝试了一种不同的方法,但这个想法是完美的!非常感谢。