-
Notifications
You must be signed in to change notification settings - Fork 0
/
dicom_viz.py
105 lines (86 loc) Β· 3.25 KB
/
dicom_viz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import shutil
import os
import cv2
import numpy as np
import dicom
import sys
from dicom import dataelem
import pickle
have_PIL = True
try:
import PIL.Image
import PIL.ImageOps as ops
except ImportError:
have_PIL = False
have_numpy = True
try:
import numpy as np
except ImportError:
have_numpy = False
def get_LUT_value(data, window, level):
"""Apply the RGB Look-Up Table for the given
data and window/level value."""
if not have_numpy:
raise ImportError("Numpy is not available."
"See http://numpy.scipy.org/"
"to download and install")
return np.piecewise(data,
[data <= (level - 0.5 - (window - 1) / 2),
data > (level - 0.5 + (window - 1) / 2)],
[0, 255, lambda data: ((data - (level - 0.5)) /
(window - 1) + 0.5) * (255 - 0)])
def get_PIL_image(dataset):
"""Get Image object from Python Imaging Library(PIL)"""
if not have_PIL:
raise ImportError("Python Imaging Library is not available. "
"See http://www.pythonware.com/products/pil/ "
"to download and install")
if ('PixelData' not in dataset):
raise TypeError("Cannot show image -- DICOM dataset does not have "
"pixel data")
# can only apply LUT if these window info exists
if ('WindowWidth' not in dataset) or ('WindowCenter' not in dataset):
bits = dataset.BitsAllocated
samples = dataset.SamplesPerPixel
if bits == 8 and samples == 1:
mode = "L"
elif bits == 8 and samples == 3:
mode = "RGB"
elif bits == 16:
# not sure about this -- PIL source says is 'experimental'
# and no documentation. Also, should bytes swap depending
# on endian of file and system??
mode = "I;16"
else:
raise TypeError("Don't know PIL mode for %d BitsAllocated "
"and %d SamplesPerPixel" % (bits, samples))
# PIL size = (width, height)
size = (dataset.Columns, dataset.Rows)
# Recommended to specify all details
# by http://www.pythonware.com/library/pil/handbook/image.htm
im = PIL.Image.frombuffer(mode, size, dataset.PixelData,
"raw", mode, 0, 1)
else:
image = get_LUT_value(dataset.pixel_array, dataset.WindowWidth,
dataset.WindowCenter)
# Convert mode to L since LUT has only 256 values:
# http://www.pythonware.com/library/pil/handbook/image.htm
im = PIL.Image.fromarray(image).convert('L')
return im
def show_PIL(dataset):
"""Display an image using the Python Imaging Library (PIL)"""
im = get_PIL_image(dataset)
im.show()
pil_image = im.convert('RGB')
open_cv_image = np.array(pil_image)
cv2.imshow(open_cv_image)
def read_seg(dcm_filename):
ds = dicom.read_file(dcm_filename)
img = ds[0x00690069].value
i = pickle.loads(img)
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.resizeWindow('image', 600, 600)
cv2.imshow('image', i)
cv2.waitKey(0)
cv2.destroyAllWindows()
read_seg(sys.argv[1])