scan.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import pydicom
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import os
  5. import csv
  6. table = {}
  7. header = "Proband,SE,Image,min,max,minD,maxD,cont_val,blur_val,art_val,plan_val"
  8. print(header)
  9. with open("overview.csv") as f:
  10. for row in csv.reader(f, delimiter=";"):
  11. if len(row) >= 20:
  12. k = f"{row[7]}/{row[8]}"
  13. table[k] = f"{row[16]},{row[17]},{row[18]},{row[19]}"
  14. def testPicture(fileName, patientNr, seNr, n, last=None):
  15. if not os.path.exists(fileName):
  16. return None, None, None, None, None
  17. a = None
  18. b = None
  19. c = None
  20. d = None
  21. img = pydicom.dcmread(fileName)
  22. if last is not None:
  23. ab = np.abs(img.pixel_array)
  24. a = np.max(ab)
  25. for row in ab:
  26. for x in row:
  27. if b is None or b == 0:
  28. b = x
  29. if x > 0:
  30. b = min(b, x)
  31. diff = np.abs(img.pixel_array - last)
  32. c = np.max(diff)
  33. for row in diff:
  34. for x in row:
  35. if d is None or d == 0:
  36. d = x
  37. if x > 0:
  38. d = min(d, x)
  39. last = img.pixel_array
  40. return last, a, b, c, d
  41. def testSe(path, patientNr, seNr):
  42. path = f"{path}/SE00000{seNr}"
  43. last = None
  44. for n in range(1000):
  45. m = f"{n}"
  46. while len(m) < 4:
  47. m = f"0{m}"
  48. name = f"{path}/{patientNr}_{m}.dcm"
  49. last, mx, mi, mxD, miD = testPicture(name, patientNr, seNr, n, last)
  50. if last is None:
  51. break
  52. if mx is not None:
  53. v = ""
  54. if path in table:
  55. v = table[path]
  56. print(f"{patientNr},{seNr},{n},{mi},{mx},{miD},{mxD},{v}")
  57. def testPatient(patientNr):
  58. path = f"Proband {patientNr}"
  59. for n in [1,2,3,4,5,6,7,8]:
  60. testSe(path, patientNr, n)
  61. for patientNr in ["02", "03", "04", "05", "06", "07", "08", "09", "10", "11"]:
  62. testPatient(patientNr)