python 3.x – Generate an Image Dataset from a Single Image

python 3.x – Generate an Image Dataset from a Single Image

Changing the shape of your inner structures while safely keeping all possible characteristics seems non-trivial to me. There are however a number of simple transformation you could do to create an augmented dataset such as:

  • Mirroring: Horizontally, vertically, diagonally – will keep all of your line characteristics
  • Rotation: Normally you would also do some rotations, but this will obviously change the orientation of your lines which you want to preserve, so this does not apply in your case
  • Shearing: Might still apply and work nicely to add some robustness, as long as you dont overdo it and end up bending your features too much

Other than that you might also want to add some noise to your image, or transformed versions of it as listed above, such as Gaussian noise or salt and pepper noise.

You could also play around with the color values, e.g. by slighly shifting the saturation of different hue values in HSV space.

You can combine any of those methods in different combinations, if you try all possible permutations with different amount/type of noise you will get quite a big dataset.

One approach is using kerass ImageDataGenerator

    1. Decide how many samples you want? Assume 5.
    • total_number = 5
    1. Initialize ImageDataGenerator class. For instance
    • data_gen = ImageDataGenerator(rescale=1. / 255, shear_range=0.2,
                                     zoom_range=0.2, horizontal_flip=True)
      
    1. Turn your image to the tensor.
    • img = load_img(xIzEG.png, grayscale=False)  #¬†You can also create gray-images.
      arr = img_to_array(img)
      tensor_img = arr.reshape((1, ) + arr.shape)
      
    1. Create a folder you want to store the result, i.e. populated, then Populate
    • for i, _ in enumerate(data_gen.flow(x=tensor_img,
                                          batch_size=1,
                                          save_to_dir=populated,
                                          save_prefix=generated,
                                          save_format=.png)):
      if i > total_number:
          break
      

Now, if you look at your populated folder:

enter enter enter enter enter

Code


from keras.preprocessing.image import load_img, img_to_array
from keras.preprocessing.image import ImageDataGenerator

# Total Generated number
total_number = 5

data_gen = ImageDataGenerator(rescale=1. / 255, shear_range=0.2,
                              zoom_range=0.2, horizontal_flip=True)

# Create image to tensor
img = load_img(xIzEG.png, grayscale=False)
arr = img_to_array(img)
tensor_image = arr.reshape((1, ) + arr.shape)

for i, _ in enumerate(data_gen.flow(x=tensor_image,
                                    batch_size=1,
                                    save_to_dir=populated,
                                    save_prefix=generated,
                                    save_format=.png)):
    if i > total_number:
        break

python 3.x – Generate an Image Dataset from a Single Image

Leave a Reply

Your email address will not be published.