Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. """
  2. pre-processing of images of plankton.
  3. The amount of images are counted in the folder_location, so it is known which shape the 3d array should be.
  4. A 3d array (amount_of_images, 428, 428) is then created and is filled with the grayscale data of the images.
  5.  
  6. Author: Lin Chang @ l.z.chang@st.hanze.nl
  7. """
  8.  
  9. import numpy as np
  10. import glob
  11. import sys
  12. import os
  13. import time
  14. from matplotlib import pyplot as plt
  15. import skimage.transform
  16. import skimage.io
  17.  
  18.  
  19. def count_files(location, folder_name):
  20. """Count the amount of images to create an 3d array with."""
  21. total_images = 0
  22.  
  23. for folder in os.listdir(location):
  24. if folder.endswith(folder_name):
  25. total_images += (len(os.listdir(location + folder)))
  26.  
  27. return total_images
  28.  
  29.  
  30. def create_array_with_images(folder, folder_name):
  31. """Create an 3d Array and put the data of a single image in every row."""
  32.  
  33. # Count the amount of files and create an array with amount_of_files * image_length * image_width
  34. total_images = count_files(folder, folder_name)
  35. array3d = np.ndarray([total_images, 428, 428])
  36.  
  37. counter = 0
  38.  
  39. for item in glob.glob(folder + "*/"):
  40. if item.endswith(folder_name + "/"):
  41. for file in glob.glob(item + "*"):
  42. # Create an empty array to paste the image on
  43. wall = np.zeros((428, 428), dtype=np.float)
  44.  
  45. # Read image in as array in grayscale
  46. img = skimage.io.imread(file, as_grey=True)
  47. img[img == 255] = 0
  48.  
  49. # Calculate the middle point of the image to paste on
  50. x = (wall.shape[0] - img.shape[0]) // 2
  51. y = (wall.shape[1] - img.shape[1]) // 2
  52. wall[x:x + img.shape[0], y:y + img.shape[1]] = img
  53.  
  54. # Normalize to values between -0.5 and 0.5
  55. # normalized = (wall - 255 / 2) / 255 # weird output after augmentation
  56. normalized = wall / 255
  57. array3d[counter] = normalized
  58.  
  59. counter += 1
  60.  
  61. return array3d
  62.  
  63.  
  64. def main(argv=None):
  65. if sys.argv is None:
  66. sys.argv = argv
  67.  
  68. folder_location = "/commons/student/2017-2018/Thema11/Lin_Sylt/"
  69.  
  70. start = time.time()
  71.  
  72. train_data = create_array_with_images(folder_location, "train+")
  73. valid_data = create_array_with_images(folder_location, "valid+")
  74. test_data = create_array_with_images(folder_location, "test+")
  75.  
  76. # Reshape into 2d array if needed
  77.  
  78. a = np.reshape(train_data, (train_data.shape[0], train_data.shape[1] * train_data.shape[2]))
  79.  
  80. print("took: {:.2f} seconds".format(time.time() - start))
  81.  
  82. res = skimage.transform.rotate(valid_data[26], 45)
  83. plt.imshow(res)
  84. # plt.imshow(valid_data[26])
  85. plt.show()
  86.  
  87. return 0
  88.  
  89.  
  90. if __name__ == "__main__":
  91. sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement