从Tensorflow中的张量提取随机不重叠的瞥见

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

我想用Tensorflow提取3个80 x 80的随机,不重叠的子图像。我该怎么办?下图应该给出一个想法。

Illustration of the problem

image tensorflow image-processing
1个回答
0
投票

我想我找到了解决方案,如果您有任何建议,请这样做。

  @tf.function

  def sample_img(img,frame_dim=(80,80),seed=42,n=3,padding='VALID'):
   if n > (img.shape[0] * img.shape[1]) // (frame_dim[0] * frame_dim[1]):
     padding = 'SAME' 

   patches = tf.image.extract_patches(tf.reshape(img,shape=(-1,*img.shape)),
                         [1,*frame_dim,1],
                         [1,*frame_dim,1],
                         [1,1,1,1],padding=padding)

   patches_res = tf.reshape(patches,shape=(-1,*frame_dim,img.shape[2]))

   ixs = tf.reshape(tf.range(patches_res.shape[0],dtype=tf.int64),shape=(1,-1))
   ixs_sampled = tf.random.uniform_candidate_sampler(ixs,
                                                  patches_res.shape[0],n,
                                           unique=True,range_max=patches_res.shape[0])

   ixs_sampled_res = tf.reshape(ixs_sampled.sampled_candidates,shape=(n,1))
© www.soinside.com 2019 - 2024. All rights reserved.