简体   繁体   English

使用 tensorflow 从图像列表中提取补丁

[英]Extract patches from list of images using tensorflow

How we can extract patches if we have a list of images.如果我们有图像列表,我们如何提取补丁。

example:例子:

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

Want to do something like this: For example, I did this for only 1 image, but I wanted to do the same task for 100 images.想做这样的事情:例如,我只为 1 张图像执行此操作,但我想为 100 张图像执行相同的任务。

sample image(s):示例图像: 示例图像

output image of sample image(s):示例图像的 output 图像: 输出图像

I have a list of images and want to extract patches from an image and save them in another list.我有一个图像列表,想从图像中提取补丁并将它们保存在另一个列表中。 And that list can be overwritten.并且该列表可以被覆盖。

Here is an example of two images in a list.这是列表中两个图像的示例。 The image patches are extracted for each image, and the end result is an array of 4 patches per image, hence the shape (2, 4, 4, 3) of patched_images , where 2 is the number of samples, 4 is the number of patches per image, and (4, 4, 3) is the shape of each patch image.为每个图像提取图像块,最终结果是每个图像有 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)

在此处输入图像描述

If you have different image sizes and still want to extract 4x4 patches regardless of the size of the images, try this:如果您有不同的图像大小并且仍然想提取 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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM