【问题标题】:Tensorflow - Finding biggest 3 neighbor pixels for each pixel in an image tensorTensorflow - 为图像张量中的每个像素找到最大的 3 个相邻像素
【发布时间】:2018-03-13 23:04:46
【问题描述】:

我正在努力定位像素附近最大的 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" 并为最大的 3 分配 "0"False休息。

我对函数tf.nn.top_k 有点困惑。我无法获得正确的索引。此外,我还没有尝试过,但它似乎不接受None 类型的数据。有什么技巧或其他方法吗?

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

【问题讨论】:

  • 使用tf.nn.top_k会得到什么样的输出?
  • 它只给出最大值的深度数而不是完整索引

标签: python image-processing tensorflow machine-learning computer-vision


【解决方案1】:

我想我找到了解决办法

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% 工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 2019-07-24
    • 2020-05-17
    • 2017-11-01
    • 1970-01-01
    • 2016-03-11
    • 2020-11-08
    相关资源
    最近更新 更多