张量相对于元素邻居的条件值
Posted
技术标签:
【中文标题】张量相对于元素邻居的条件值【英文标题】:Conditional value on tensor relative to element neighbors 【发布时间】:2017-10-03 21:23:24 【问题描述】:我正在使用 Tensorflow 实现 Canny 算法(这是使用边界作为评估指标所必需的,但这是题外话)。其中一个步骤是计算“非最大抑制”,这包括将 3x3 区域中的中心元素归零,除非两个特定的邻居更小。更多详情here.
如何使用 Tensorflow 实现此操作?
我实际上在使用 Keras,但 Tensorflow 解决方案也可以,作为参考,我的代码到目前为止看起来像这样:
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
【问题讨论】:
【参考方案1】:您可以使用卷积过滤器来分离两个目标像素并使它们与中心像素“同心”。
例如,为了比较两个目标像素,我们可以使用这个过滤器,形状为(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
【讨论】:
我尝试了一种不同的方法,但这个想法很完美!谢谢!以上是关于张量相对于元素邻居的条件值的主要内容,如果未能解决你的问题,请参考以下文章