【问题标题】:Extract patches from list of images using tensorflow使用 tensorflow 从图像列表中提取补丁
【发布时间】:2021-12-27 10:26:33
【问题描述】:

如果我们有图像列表,我们如何提取补丁。

示例:

def get_train_images():
    image_list = glob(FLAGS.train_path + '/*.jpg')
    #extract_patch

想做这样的事情: 例如,我只为 1 张图片执行此操作,但我想为 100 张图片执行相同的任务。

示例图片:

样本图像的输出图像:

我有一个图像列表,想从图像中提取补丁并将它们保存在另一个列表中。并且可以覆盖该列表。

【问题讨论】:

  • 什么是“补丁”?请包括样本输入和预期输出。你想用“补丁”做什么?

标签: python tensorflow tensorflow2.0


【解决方案1】:

这是一个列表中的两个图像的示例。为每个图像提取图像块,最终结果是每个图像包含 4 个块的数组,因此形状为 patched_images 的形状 (2, 4, 4, 3),其中 2 是样本数,4 是每个图像的块数, (4, 4, 3) 是每个补丁图像的形状。

import tensorflow as tf
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np

images = [tf.random.normal((16, 16, 3)), tf.random.normal((16, 16, 3))]

patched_images = []
for img in images:
  image = tf.expand_dims(np.array(img), 0)
  patches = tf.image.extract_patches(images=image,
                          sizes=[1, 4, 4, 1],
                          strides=[1, 4, 4, 1],
                          rates=[1, 1, 1, 1],
                          padding='VALID')
  patches = [tf.reshape(patches[0, i, i], (4, 4, 3)) for i in range(4)]
  patched_images.append(np.asarray(patches))

patched_images = np.asarray(patched_images)
print(patched_images.shape)

axes=[]
fig=plt.figure()
patched_image = patched_images[0] # plot patches of first image
for i in range(4):
    axes.append( fig.add_subplot(2, 2, i + 1) )
    subplot_title=("Patch "+str(i + 1))
    axes[-1].set_title(subplot_title)  
    plt.imshow(patched_image[i, :, :, :])
fig.tight_layout()    
plt.show()
(2, 4, 4, 4, 3)

如果您有不同的图像尺寸,并且仍然想提取 4x4 补丁而不管图像的大小,试试这个:

import tensorflow as tf
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np

images = [tf.random.normal((16, 16, 3)), tf.random.normal((24, 24, 3)), tf.random.normal((180, 180, 3))]

patched_images = []
for img in images:
  image = tf.expand_dims(np.array(img), 0)
  patches = tf.image.extract_patches(images=image,
                          sizes=[1, 4, 4, 1],
                          strides=[1, 4, 4, 1],
                          rates=[1, 1, 1, 1],
                          padding='VALID')
  patches = [tf.reshape(patches[0, i, i], (4, 4, 3)) for i in range(4)]
  patched_images.append(np.asarray(patches))

【讨论】:

  • 它也适用于不同的尺寸。
  • 在其他问题上需要帮助。我不明白。
  • 现在怎么了?
  • 我可以解释更多@docs
猜你喜欢
  • 2019-06-17
  • 1970-01-01
  • 2021-01-20
  • 2017-04-05
  • 2020-05-05
  • 2020-08-22
  • 2019-10-22
  • 2016-10-20
  • 2017-07-09
相关资源
最近更新 更多