Diff of /read_data.py [000000] .. [4c12f9]

Switch to unified view

a b/read_data.py
1
"""
2
This file is to load the input image and convert to numpy array.
3
"""
4
import SimpleITK as sitk
5
import matplotlib.pyplot as plt
6
7
class LoadData:
8
    """
9
    This class is designed to load "one" input image.
10
    """
11
    def __init__(self, path, name):
12
        """
13
        :param path: image derectory
14
        :param name: image name
15
        """
16
        self.img_path = path + name
17
        self.image = None
18
        self.slices = None
19
20
    def loaddata(self):
21
        """
22
        Load image with given image path.
23
        :return: None
24
        """
25
        self.image = sitk.ReadImage(self.img_path)
26
27
    def tileimage(self, index1, index2):
28
        """
29
        Tile the 3D image into two selected slices for showing.
30
        :param index1: selected slice 1
31
        :param index2: selected slice 2
32
        :return: None
33
        """
34
        self.slices = sitk.Tile(self.image[:, :, index1],
35
                                self.image[:, :, index2],
36
                                (2, 1, 0))
37
38
    def sitk_show(self, title=None, margin=0.0, dpi=40):
39
        """
40
        Show the tiled 2D images.
41
        :param title: Title
42
        :param margin: Margin
43
        :param dpi: ???
44
        :return: None
45
        """
46
        nda = sitk.GetArrayFromImage(self.slices)
47
        figsize = (1 + margin) * nda.shape[0] / dpi, (1 + margin) * nda.shape[1] / dpi
48
        extent = (0, nda.shape[1], nda.shape[0], 0)
49
        fig = plt.figure(figsize=figsize, dpi=dpi)
50
        ax = fig.add_axes([margin, margin, 1 - 2 * margin, 1 - 2 * margin])
51
52
        plt.set_cmap("gray")
53
        ax.imshow(nda, extent=extent, interpolation=None)
54
        if title:
55
            plt.title(title)
56
        plt.show()
57
58
59
def main():
60
    data_path = "resource/"
61
    img_name = "lola11-01.mhd"
62
63
    # Slice index to visualize with 'sitk_show'
64
    idxSlice1 = 26
65
    idxSlice2 = 50
66
67
    # int label to assign to the segmented gray matter
68
    labelGrayMatter = 1
69
70
    data = LoadData(data_path, img_name)
71
    data.loaddata()
72
    print "after read image..."
73
    data.tileimage(idxSlice1, idxSlice2)
74
    data.sitk_show()
75
76
    print "after showing image..."
77
    image_array = sitk.GetArrayFromImage(data.image)
78
    print "the shape of image is ", image_array.shape
79
80
    # output = sitk.DiscreteGaussianFilter(input, 1.0, 5)
81
    # sitk.Show(image)
82
83
if __name__ == "__main__":
84
    main()