In Masterarbeit:"Anomalie-Detektion in Zellbildern zur Anwendung der Leukämieerkennung" verwendete CSI Methode.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

prepare_data.py 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import csv
  2. import os
  3. from PIL import Image
  4. from torchvision import transforms
  5. from torchvision.utils import save_image
  6. import torch
  7. def transform_image(img_in, target_dir, transformation, suffix):
  8. """
  9. Transforms an image according to provided transformation.
  10. Parameters:
  11. img_in (path): Image to transform
  12. target_dir (path): Destination path
  13. transformation (callable): Transformation to be applied
  14. suffix (str): Suffix of resulting image.
  15. Returns:
  16. binary_sum (str): Binary string of the sum of a and b
  17. """
  18. if suffix == 'rot':
  19. im = Image.open(img_in)
  20. im = im.rotate(270)
  21. tensor = transforms.ToTensor()(im)
  22. save_image(tensor, target_dir + os.sep + suffix + '.jpg')
  23. elif suffix == 'sobel':
  24. im = Image.open(img_in)
  25. tensor = transforms.ToTensor()(im)
  26. sobel_filter = torch.tensor([[1., 2., 1.], [0., 0., 0.], [-1., -2., -1.]])
  27. f = sobel_filter.expand(1, 3, 3, 3)
  28. tensor = torch.conv2d(tensor, f, stride=1, padding=1 )
  29. save_image(tensor, target_dir + os.sep + suffix + '.jpg')
  30. elif suffix == 'noise':
  31. im = Image.open(img_in)
  32. tensor = transforms.ToTensor()(im)
  33. tensor = tensor + (torch.randn(tensor.size()) * 0.2 + 0)
  34. save_image(tensor, target_dir + os.sep + suffix + '.jpg')
  35. elif suffix == 'cutout':
  36. print("asd")
  37. else:
  38. im = Image.open(img_in)
  39. im_trans = transformation(im)
  40. im_trans.save(target_dir + os.sep + suffix + '.jpg')
  41. def sort_and_rename_images(excel_path: str):
  42. """Renames images and sorts them according to csv."""
  43. base_dir = excel_path.rsplit(os.sep, 1)[0]
  44. dir_all = base_dir + os.sep + 'all'
  45. if not os.path.isdir(dir_all):
  46. os.mkdir(dir_all)
  47. dir_hem = base_dir + os.sep + 'hem'
  48. if not os.path.isdir(dir_hem):
  49. os.mkdir(dir_hem)
  50. with open(excel_path, mode='r') as file:
  51. csv_file = csv.reader(file)
  52. for lines in csv_file:
  53. print(lines)
  54. if lines[2] == '1':
  55. os.rename(base_dir + os.sep + lines[1], dir_all + os.sep + lines[0])
  56. elif lines[2] == '0':
  57. os.rename(base_dir + os.sep + lines[1], dir_hem + os.sep + lines[0])
  58. def drop_color_channels(source_dir, target_dir, rgb):
  59. """Rotates all images in in source dir."""
  60. if rgb == 0:
  61. suffix = "red_only"
  62. drop_1 = 1
  63. drop_2 = 2
  64. elif rgb == 1:
  65. suffix = "green_only"
  66. drop_1 = 0
  67. drop_2 = 2
  68. elif rgb == 2:
  69. suffix = "blue_only"
  70. drop_1 = 0
  71. drop_2 = 1
  72. elif rgb == 3:
  73. suffix = "no_red"
  74. drop_1 = 0
  75. elif rgb == 4:
  76. suffix = "no_green"
  77. drop_1 = 1
  78. elif rgb == 5:
  79. suffix = "no_blue"
  80. drop_1 = 2
  81. else:
  82. suffix = ""
  83. print("Invalid RGB-channel")
  84. if suffix != "":
  85. dirs = os.listdir(source_dir)
  86. for item in dirs:
  87. if os.path.isfile(source_dir + os.sep + item):
  88. im = Image.open(source_dir + os.sep + item)
  89. tensor = transforms.ToTensor()(im)
  90. tensor[drop_1, :, :] = 0
  91. if rgb < 3:
  92. tensor[drop_2, :, :] = 0
  93. save_image(tensor, target_dir + os.sep + item, 'bmp')
  94. def rotate_images(target_dir, source_dir, rotate, theta):
  95. """Rotates all images in in source dir."""
  96. dirs = os.listdir(source_dir)
  97. for item in dirs:
  98. if os.path.isfile(source_dir + os.sep + item):
  99. for i in range(0, rotate):
  100. im = Image.open(source_dir + os.sep + item)
  101. im = im.rotate(i*theta)
  102. tensor = transforms.ToTensor()(im)
  103. save_image(tensor, target_dir + os.sep + str(i) + '_' + item, 'bmp')
  104. def grayscale_image(source_dir, target_dir):
  105. """Grayscale transforms all images in path."""
  106. t = transforms.Grayscale()
  107. dirs = os.listdir(source_dir)
  108. if not os.path.isdir(target_dir):
  109. os.mkdir(target_dir)
  110. for item in dirs:
  111. if os.path.isfile(source_dir + os.sep + item):
  112. im = Image.open(source_dir + os.sep + item).convert('RGB')
  113. im_resize = t(im)
  114. tensor = transforms.ToTensor()(im_resize)
  115. padding = torch.zeros(1, tensor.shape[1], tensor.shape[2])
  116. tensor = torch.cat((tensor, padding), 0)
  117. im_resize.save(target_dir + os.sep + item, 'bmp')
  118. def resize(source_dir):
  119. """Rotates all images in in source dir."""
  120. t = transforms.Compose([transforms.Resize((128, 128))])
  121. dirs = os.listdir(source_dir)
  122. target_dir = source_dir + os.sep + 'resized'
  123. if not os.path.isdir(target_dir):
  124. os.mkdir(target_dir)
  125. for item in dirs:
  126. if os.path.isfile(source_dir + os.sep + item):
  127. im = Image.open(source_dir + os.sep + item)
  128. im_resize = t(im)
  129. im_resize.save(source_dir + os.sep + 'resized' + os.sep + item, 'bmp')
  130. def crop_image(source_dir):
  131. """Center Crops all images in path."""
  132. t = transforms.CenterCrop((224, 224))
  133. dirs = os.listdir(source_dir)
  134. target_dir = source_dir + os.sep + 'cropped'
  135. if not os.path.isdir(target_dir):
  136. os.mkdir(target_dir)
  137. for item in dirs:
  138. if os.path.isfile(source_dir + os.sep + item):
  139. im = Image.open(source_dir + os.sep + item)
  140. im_resize = t(im, )
  141. im_resize.save(source_dir + os.sep + 'cropped' + os.sep + item, 'bmp')
  142. def mk_dirs(target_dir):
  143. dir_0 = target_dir + r"\fold_0"
  144. dir_1 = target_dir + r"\fold_1"
  145. dir_2 = target_dir + r"\fold_2"
  146. dir_3 = target_dir + r"\phase2"
  147. dir_4 = target_dir + r"\phase3"
  148. dir_0_all = dir_0 + r"\all"
  149. dir_0_hem = dir_0 + r"\hem"
  150. dir_1_all = dir_1 + r"\all"
  151. dir_1_hem = dir_1 + r"\hem"
  152. dir_2_all = dir_2 + r"\all"
  153. dir_2_hem = dir_2 + r"\hem"
  154. if not os.path.isdir(dir_0):
  155. os.mkdir(dir_0)
  156. if not os.path.isdir(dir_1):
  157. os.mkdir(dir_1)
  158. if not os.path.isdir(dir_2):
  159. os.mkdir(dir_2)
  160. if not os.path.isdir(dir_3):
  161. os.mkdir(dir_3)
  162. if not os.path.isdir(dir_4):
  163. os.mkdir(dir_4)
  164. if not os.path.isdir(dir_0_all):
  165. os.mkdir(dir_0_all)
  166. if not os.path.isdir(dir_0_hem):
  167. os.mkdir(dir_0_hem)
  168. if not os.path.isdir(dir_1_all):
  169. os.mkdir(dir_1_all)
  170. if not os.path.isdir(dir_1_hem):
  171. os.mkdir(dir_1_hem)
  172. if not os.path.isdir(dir_2_all):
  173. os.mkdir(dir_2_all)
  174. if not os.path.isdir(dir_2_hem):
  175. os.mkdir(dir_2_hem)
  176. return dir_0_all, dir_0_hem, dir_1_all, dir_1_hem, dir_2_all, dir_2_hem, dir_3, dir_4