Tensorflow - 为图像张量中的每个像素查找最大3个相邻像素

问题描述 投票:0回答:1

我正在努力寻找像素附近的最大k像素位置。输入是nonetype动态图像张量。

versions:
-tensorflow 1.2-gpu
-python 3.5

为了提取图像张量中每个像素的邻居,我创建了一个眼睛滤镜:

w = np.eye(9).reshape((3, 3, 1, 9))
weights=tf.constant(w,tf.float32)
pixel_determ= tf.nn.conv2d(patches_batch, weights, strides=[1, 1, 1, 1], padding='SAME') #shape=(8, 183, 275, 9)

得到的张量将具有9的深度,其包括邻居和中心像素值本身。

我需要做的是为图像的每个像素找到沿深度的3个最大值(位置必须保留)并为最大3分配True"1",其余分配"0"False

我有点混淆功能tf.nn.top_k。我无法得到正确的指数。此外,我还没有尝试,但似乎它不接受None类型数据。有没有任何技巧或其他方式这样做?

如果能得到任何帮助,我将非常感激。先感谢您。

python image-processing tensorflow machine-learning computer-vision
1个回答
0
投票

我想我找到了解决方案

def biggest_k_indices(mat, k):
    _, indices_mat =tf.nn.top_k(mat, tf.shape(mat)[3], sorted=False)
    _, indices_k =tf.nn.top_k(mat, k, sorted=False)
    index= []
    eq =[]

    for i in range(k):
        index.append(tf.expand_dims(indices_k[:,:,:,i],-1))
        eq.append(tf.equal(indices_mat,index[i]))

    bool_comb =tf.logical_or(eq[0],eq[1])
    if (k==2):
        index.clear() 
        eq.clear()
        return bool_comb

    for i in eq[2:]:
        bool_comb=tf.logical_or(bool_comb,i)

    index.clear()
    eq.clear()
    return bool_comb

在这个函数中,我在循环中逐个比较张量的索引和k个最大索引。然后在tf.logical_or的帮助下,我将True值收集到单个张量bool_comb中。我只测试了一个测试阵列。所以我不确定它是否会100%有效。

© www.soinside.com 2019 - 2024. All rights reserved.