[2c38ce]: / Dicom.py

Download this file

67 lines (56 with data), 2.3 kB

 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
import numpy as np
import pydicom as dicom
def getSegmentedPixelColor (value, intervals):
for i in range(len(intervals)):
if value >= intervals[i][0] and value <= intervals[i][1]:
return intervals[i][2]
return [0,0,0]
# Linear transformation : from [bt < pxvalue < wt] linear to [0 <pyvalue< 255] !important: has loss of information
def linearTransform(pxvalue, bt, wt):
if pxvalue < bt:
y=0
elif pxvalue > wt:
y=255
else:
y=pxvalue*255/(wt-bt)-255*bt/(wt-bt)
return y
# Linear transformation : convert to Hounsfield units (HU)
def getHuPixels (pixels_array, rows, cols, intercept, slope):
m = np.zeros((rows, cols), np.int16)
for i in range(rows):
for j in range(cols):
m[i][j] = pixels_array[i][j] * slope + intercept
return m
class Dicom:
def __init__(self, src):
self.__ds = dicom.dcmread(src)
self.__patientId = self.__ds.PatientID
self.__cols = self.__ds.Columns
self.__rows = self.__ds.Rows
self.__huPixelsArray = getHuPixels(self.__ds.pixel_array, self.__rows, self.__cols, self.__ds.RescaleIntercept, self.__ds.RescaleSlope)
def getPatientId (self):
return self.__patientId
def getPixelsArray (self):
return self.__huPixelsArray
def getRawPixelsArray (self):
return self.__ds.pixel_array
def getLinearRGB (self, bt, wt):
image = np.zeros((self.__rows, self.__cols, 3), np.uint8)
for i in range(self.__rows):
for j in range(self.__cols):
color = linearTransform(self.getPixelsArray()[i][j], bt, wt)
image[i][j] = (color,color,color)
return image
# Remember, open-cv uses BGR color system
def getSegmentedRGB (self, intervals = [(-3000,-1000,[0,0,0]),(-15,15,[255,0,128]),(20,50,[255,0,0]),(60,100,[0,0,255]),(101,3000,[255,255,255])]): # List of intervals to filter
image = np.zeros((self.__rows, self.__cols, 3), np.uint8)
for i in range(self.__rows):
for j in range(self.__cols):
image[i][j] = getSegmentedPixelColor(self.getPixelsArray()[i][j], intervals)
return image
def showHistogram (self):
import matplotlib.pyplot as plt
plt.hist(self.getPixelsArray().flatten(), bins=50, color='c')
plt.xlabel("Hounsfield Units (HU)")
plt.ylabel("Frequency")
plt.show()