med.py 2.92 KB
Newer Older
Sugon_ldc's avatar
Sugon_ldc committed
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
106
107
108
109
110
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import numpy as np
import cv2

from eiseg import logger


def has_sitk():
    try:
        import SimpleITK

        return True
    except ImportError:
        return False


if has_sitk():
    import SimpleITK as sitk


def dcm_reader(path):
    logger.debug(f"opening medical image {path}")
    reader = sitk.ImageSeriesReader()
    reader.SetFileNames([path])
    image = reader.Execute()
    img = sitk.GetArrayFromImage(image)
    logger.debug(f"scan shape is {img.shape}")
    if len(img.shape) == 4:
        img = img[0]
    # WHC
    img = np.transpose(img, [1, 2, 0])
    return img.astype(np.int32)


def windowlize(scan, ww, wc):
    wl = wc - ww / 2
    wh = wc + ww / 2
    res = scan.copy()
    res = res.astype(np.float32)
    res = np.clip(res, wl, wh)
    res = (res - wl) / ww * 255
    res = res.astype(np.uint8)
    # print("++", res.shape)
    # for idx in range(res.shape[-1]):
    # TODO: 支持3d或者改调用
    res = cv2.cvtColor(res, cv2.COLOR_GRAY2BGR)

    return res


# def open_nii(niiimg_path):
#     if IPT_SITK == True:
#         sitk_image = sitk.ReadImage(niiimg_path)
#         return _nii2arr(sitk_image)
#     else:
#         raise ImportError("can't import SimpleITK!")

#
# def _nii2arr(sitk_image):
#     if IPT_SITK == True:
#         img = sitk.GetArrayFromImage(sitk_image).transpose((1, 2, 0))
#         return img
#     else:
#         raise ImportError("can't import SimpleITK!")
#
#
# def slice_img(img, index):
#     if index == 0:
#         return sample_norm(
#             cv2.merge(
#                 [
#                     np.uint16(img[:, :, index]),
#                     np.uint16(img[:, :, index]),
#                     np.uint16(img[:, :, index + 1]),
#                 ]
#             )
#         )
#     elif index == img.shape[2] - 1:
#         return sample_norm(
#             cv2.merge(
#                 [
#                     np.uint16(img[:, :, index - 1]),
#                     np.uint16(img[:, :, index]),
#                     np.uint16(img[:, :, index]),
#                 ]
#             )
#         )
#     else:
#         return sample_norm(
#             cv2.merge(
#                 [
#                     np.uint16(img[:, :, index - 1]),
#                     np.uint16(img[:, :, index]),
#                     np.uint16(img[:, :, index + 1]),
#                 ]
#             )
#         )