Guest User

Untitled

a guest
Feb 12th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. import pydicom as dicom
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4.  
  5.  
  6. def grouped(iterable, n):
  7. "s -> (s0,s1,s2,...sn-1), (sn,sn+1,sn+2,...s2n-1), (s2n,s2n+1,s2n+2,...s3n-1), ..."
  8. return zip(*[iter(iterable)] * n)
  9.  
  10.  
  11. def bytes2int(bytes_arr):
  12. return [y << 8 | x for x, y in grouped(bytes_arr.__iter__(), 2)]
  13.  
  14.  
  15. def mammo_convert(dcm, which_LUT=0):
  16. arr = dcm.pixel_array
  17. lut = dcm.VOILUTSequence[which_LUT]
  18. lut_data = bytes2int(lut.LUTData)
  19. first_mapped = bytes2int(lut.LUTDescriptor)[1]
  20. last_mapped = first_mapped + len(lut_data) - 1
  21.  
  22. ret = arr.copy()
  23. ret[ret > last_mapped] = last_mapped
  24. ret[ret < first_mapped] = first_mapped
  25. ret -= first_mapped
  26.  
  27. return np.array(lut_data)[ret]
  28.  
  29.  
  30. def mammo_convert2(dcm, which_window=0):
  31. arr = dcm.pixel_array
  32. wc = dcm.WindowCenter[which_window]
  33. ww = dcm.WindowWidth[which_window]
  34. out_range = 2 ** (dcm.BitsStored)
  35.  
  36. ret = out_range / (1 + np.exp((-4) * (arr.astype(np.float) - wc) / ww))
  37.  
  38. return ret
  39.  
  40.  
  41. dc = dicom.read_file(r'C:\temp\1.2.840.113619.2.255.224244498451.3934190211141740.243.dcm', force=True)
  42. dc.file_meta.TransferSyntaxUID = dicom.uid.ImplicitVRLittleEndian
  43. ret = mammo_convert(dc)
  44. ret2 = mammo_convert2(dc)
  45.  
  46. plt.imshow(ret - np.round(ret2), cmap='gray')
  47. plt.imshow(ret - ret2, cmap='gray')
Add Comment
Please, Sign In to add comment