prepareImages.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import pydicom
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import os
  5. import csv
  6. import math
  7. import wavelet
  8. import network
  9. import keras
  10. import tensorflow as tf
  11. import random
  12. actions = [ lambda img: wavelet.rotate(img, 1)
  13. , lambda img: wavelet.rotate(img, 2)
  14. , lambda img: wavelet.rotate(img, 3)
  15. , lambda img: wavelet.rotate(img, 4)
  16. , lambda img: wavelet.rotate(img, 5)
  17. , lambda img: wavelet.rotate(img, 6)
  18. , lambda img: wavelet.rotate(img, 7)
  19. ]
  20. genData = []
  21. def blur(img):
  22. blured = img.copy()
  23. w = img.shape[1]
  24. h = img.shape[0]
  25. mx = math.sqrt(8)
  26. m = [(x, y, mx - math.sqrt((x*x) + (y*y))) for y in [-1,0,1] for x in [-1,0,1] ]
  27. s = sum([x[2] for x in m])
  28. for y in range(1, h - 1):
  29. for x in range(1, w - 1):
  30. v = sum([w * img[y + a, x + b] for a, b, w in m]) / s
  31. blured[y, x] = v
  32. return blured
  33. def histogram(fileName, img):
  34. h = [0 for _ in range(4096)]
  35. for row in img:
  36. for v in row:
  37. h[max(0, min(4095, v))] += 1
  38. h = np.array([float(v) for v in h])
  39. np.save(fileName, h, allow_pickle=False)
  40. pos = 0
  41. first = True
  42. with open("mimx.csv") as f:
  43. for row in csv.reader(f, delimiter=","):
  44. if first or len(row) < 9:
  45. first = False
  46. continue
  47. n = f"{row[2]}"
  48. while len(n) < 4:
  49. n = f"0{n}"
  50. fileName = f"Proband {row[0]}/SE00000{row[1]}/{row[0]}_{n}.dcm"
  51. print(f"{fileName}", end="\r")
  52. y = np.array([float(row[7]), float(row[8]), float(row[9]), float(row[10])])
  53. #np.save(f"{fileName}_labels.npy", y, allow_pickle=False)
  54. img = pydicom.dcmread(fileName).pixel_array
  55. #images = []
  56. #for action in actions:
  57. # w = wavelet.refine(action(img))
  58. # images.append(1.0 * w.reshape((512 * 512,)))
  59. #
  60. #images = np.array(images)
  61. #np.save(f"{fileName}_images.npy", images, allow_pickle=False)
  62. #np.save(f"{fileName}_plainImage.npy", img, allow_pickle=False)
  63. #w = wavelet.refine(img)
  64. #np.save(f"{fileName}_wavelet.npy", img, allow_pickle=False)
  65. #histogram(f"{fileName}_histogram.npy", img)
  66. blured = blur(img)
  67. np.save(f"{fileName}_blured.npy", blured, allow_pickle=False)
  68. histogram(f"{fileName}_blured_histo_diff.npy", np.abs(img - blured))
  69. print()
  70. print("done")