Python Tensorflow-为图像张量中的每个像素查找最大的3个相邻像素
我正在努力寻找像素附近最大的k像素。输入是Python Tensorflow-为图像张量中的每个像素查找最大的3个相邻像素,python,image-processing,tensorflow,machine-learning,computer-vision,Python,Image Processing,Tensorflow,Machine Learning,Computer Vision,我正在努力寻找像素附近最大的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]
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
类型数据。有什么诀窍或其他方法可以做到这一点吗
如果能得到任何帮助,我将不胜感激。先谢谢你 我想我找到了解决办法
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\u or
的帮助下,我将True
值收集到一个张量bool\u comb
中。我只使用一个测试阵列测试了它。所以我不确定它是否100%有效 使用tf.nn.top_k
时会得到什么样的输出?它只给出最大值的深度数,而不是完整的索引