Tuesday, 23 April 2019

Data Augmentation of DICOM images into PNG using Keras ImageDataGenerator

import os
import dicom
from pathlib import Path
from PIL import Image
from keras.preprocessing.image import ImageDataGenerator,img_to_array, load_img
datagen = ImageDataGenerator(rotation_range=90,width_shift_range=0.2,height_shift_range=0.2,shear_range=0.2,zoom_range=0.2,horizontal_flip=True,vertical_flip=True,fill_mode='nearest')
data_dir_list = os.listdir("Full Path of DICOM Images Dataset")
for input_path in data_dir_list:
  try:
    input = "Full Path of DICOM Images Dataset"+input_path
    if os.path.exists(input):
      scan = dicom.read_file(input)
      image = scan.pixel_array
      img = Image.fromarray(image)
      #im2 = img.resize((95, 126), Image.NEAREST)
      y = img_to_array(img)
      y = y.reshape((1,) + y.shape)
      i = 0
      print(input_path)
      prefix= input_path.replace(".dcm","")
      for batch in datagen.flow(y,save_to_dir='Full Path of Output Folder', save_prefix=prefix, save_format='png'):
        i += 1
        if i > 7:
         
            break
  except FileNotFoundError:
    continue

Data Augmentation of DICOM images into PNG using Keras ImageDataGenerator Rating: 4.5 Diposkan Oleh: Khanx