File size: 13,780 Bytes
5ccd75a | 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | # Copyright 2020 MONAI Consortium
# 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.
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Sequence, Tuple, Union
import numpy as np
from monai.data.utils import correct_nifti_header_if_necessary
from monai.utils import ensure_tuple, optional_import
from .utils import is_supported_format
if TYPE_CHECKING:
import itk # type: ignore
import nibabel as nib
from itk import Image # type: ignore
from nibabel.nifti1 import Nifti1Image
else:
itk, _ = optional_import("itk", allow_namespace_pkg=True)
Image, _ = optional_import("itk", allow_namespace_pkg=True, name="Image")
nib, _ = optional_import("nibabel")
Nifti1Image, _ = optional_import("nibabel.nifti1", name="Nifti1Image")
class ImageReader(ABC):
"""Abstract class to define interface APIs to load image files.
users need to call `read` to load image and then use `get_data`
to get the image data and properties from meta data.
"""
@abstractmethod
def verify_suffix(self, filename: Union[Sequence[str], str]) -> bool:
"""
Verify whether the specified file or files format is supported by current reader.
Args:
filename: file name or a list of file names to read.
if a list of files, verify all the subffixes.
"""
raise NotImplementedError(f"Subclass {self.__class__.__name__} must implement this method.")
@abstractmethod
def read(self, data: Union[Sequence[str], str, Any], **kwargs) -> Union[Sequence[Any], Any]:
"""
Read image data from specified file or files, or set a loaded image.
Note that it returns the raw data, so different readers return different image data type.
Args:
data: file name or a list of file names to read, or a loaded image object.
kwargs: additional args for 3rd party reader API.
"""
raise NotImplementedError(f"Subclass {self.__class__.__name__} must implement this method.")
@abstractmethod
def get_data(self) -> Tuple[np.ndarray, Dict]:
"""
Extract data array and meta data from loaded image and return them.
This function must return 2 objects, first is numpy array of image data, second is dict of meta data.
"""
raise NotImplementedError(f"Subclass {self.__class__.__name__} must implement this method.")
class ITKReader(ImageReader):
"""
Load medical images based on ITK library.
All the supported image formats can be found:
https://github.com/InsightSoftwareConsortium/ITK/tree/master/Modules/IO
Args:
c_order_axis_indexing: if `True`, the numpy array will have C-order indexing.
this is the reverse of how indices are specified in ITK, i.e. k,j,i versus i,j,k.
however C-order indexing is expected by most algorithms in numpy / scipy.
"""
def __init__(self, c_order_axis_indexing: bool = False):
super().__init__()
self._img: Optional[Sequence[Image]] = None
self.c_order_axis_indexing = c_order_axis_indexing
def verify_suffix(self, filename: Union[Sequence[str], str]) -> bool:
"""
Verify whether the specified file or files format is supported by ITK reader.
Args:
filename: file name or a list of file names to read.
if a list of files, verify all the subffixes.
"""
return True
def read(self, data: Union[Sequence[str], str, Image], **kwargs):
"""
Read image data from specified file or files, or set a `itk.Image` object.
Note that the returned object is ITK image object or list of ITK image objects.
`self._img` is always a list, even only has 1 image.
Args:
data: file name or a list of file names to read, or a `itk.Image` object for the usage that
the image data is already in memory and no need to read from file again.
kwargs: additional args for `itk.imread` API. more details about available args:
https://github.com/InsightSoftwareConsortium/ITK/blob/master/Wrapping/Generators/Python/itkExtras.py
"""
self._img = list()
if isinstance(data, Image):
self._img.append(data)
return data
filenames: Sequence[str] = ensure_tuple(data)
for name in filenames:
self._img.append(itk.imread(name, **kwargs))
return self._img if len(filenames) > 1 else self._img[0]
def get_data(self):
"""
Extract data array and meta data from loaded image and return them.
This function returns 2 objects, first is numpy array of image data, second is dict of meta data.
It constructs `affine`, `original_affine`, and `spatial_shape` and stores in meta dict.
If loading a list of files, stack them together and add a new dimension as first dimension,
and use the meta data of the first image to represent the stacked result.
"""
img_array: List[np.ndarray] = list()
compatible_meta: Dict = None
if self._img is None:
raise RuntimeError("please call read() first then use get_data().")
for img in self._img:
header = self._get_meta_dict(img)
header["original_affine"] = self._get_affine(img)
header["affine"] = header["original_affine"].copy()
header["spatial_shape"] = self._get_spatial_shape(img)
img_array.append(self._get_array_data(img))
if compatible_meta is None:
compatible_meta = header
else:
if not np.allclose(header["affine"], compatible_meta["affine"]):
raise RuntimeError("affine matrix of all images should be same.")
if not np.allclose(header["spatial_shape"], compatible_meta["spatial_shape"]):
raise RuntimeError("spatial_shape of all images should be same.")
img_array_ = np.stack(img_array, axis=0) if len(img_array) > 1 else img_array[0]
return img_array_, compatible_meta
def _get_meta_dict(self, img: Image) -> Dict:
"""
Get all the meta data of the image and convert to dict type.
Args:
img: a ITK image object loaded from a image file.
"""
img_meta_dict = img.GetMetaDataDictionary()
meta_dict = dict()
for key in img_meta_dict.GetKeys():
# ignore deprecated, legacy members that cause issues
if key.startswith("ITK_original_"):
continue
meta_dict[key] = img_meta_dict[key]
meta_dict["origin"] = np.asarray(img.GetOrigin())
meta_dict["spacing"] = np.asarray(img.GetSpacing())
meta_dict["direction"] = itk.array_from_matrix(img.GetDirection())
return meta_dict
def _get_affine(self, img: Image) -> np.ndarray:
"""
Get or construct the affine matrix of the image, it can be used to correct
spacing, orientation or execute spatial transforms.
Construct Affine matrix based on direction, spacing, origin information.
Refer to: https://github.com/RSIP-Vision/medio
Args:
img: a ITK image object loaded from a image file.
"""
direction = itk.array_from_matrix(img.GetDirection())
spacing = np.asarray(img.GetSpacing())
origin = np.asarray(img.GetOrigin())
direction = np.asarray(direction)
affine = np.eye(direction.shape[0] + 1)
affine[(slice(-1), slice(-1))] = direction @ np.diag(spacing)
affine[(slice(-1), -1)] = origin
return affine
def _get_spatial_shape(self, img: Image) -> Sequence:
"""
Get the spatial shape of image data, it doesn't contain the channel dim.
Args:
img: a ITK image object loaded from a image file.
"""
return list(itk.size(img))
def _get_array_data(self, img: Image) -> np.ndarray:
"""
Get the raw array data of the image, converted to Numpy array.
Args:
img: a ITK image object loaded from a image file.
"""
return itk.array_view_from_image(img, keep_axes=not self.c_order_axis_indexing)
class NibabelReader(ImageReader):
"""
Load NIfTI format images based on Nibabel library.
Args:
as_closest_canonical: if True, load the image as closest to canonical axis format.
"""
def __init__(self, as_closest_canonical: bool = False):
super().__init__()
self._img: Optional[Sequence[Nifti1Image]] = None
self.as_closest_canonical = as_closest_canonical
def verify_suffix(self, filename: Union[Sequence[str], str]) -> bool:
"""
Verify whether the specified file or files format is supported by Nibabel reader.
Args:
filename: file name or a list of file names to read.
if a list of files, verify all the subffixes.
"""
suffixes: Sequence[str] = ["nii", "nii.gz"]
return is_supported_format(filename, suffixes)
def read(self, data: Union[Sequence[str], str, Nifti1Image], **kwargs):
"""
Read image data from specified file or files, or set a Nibabel Image object.
Note that the returned object is Nibabel image object or list of Nibabel image objects.
`self._img` is always a list, even only has 1 image.
Args:
data: file name or a list of file names to read.
kwargs: additional args for `nibabel.load` API. more details about available args:
https://github.com/nipy/nibabel/blob/master/nibabel/loadsave.py
"""
self._img = list()
if isinstance(data, Nifti1Image):
self._img.append(data)
return data
filenames: Sequence[str] = ensure_tuple(data)
for name in filenames:
img = nib.load(name, **kwargs)
img = correct_nifti_header_if_necessary(img)
self._img.append(img)
return self._img if len(filenames) > 1 else self._img[0]
def get_data(self):
"""
Extract data array and meta data from loaded image and return them.
This function returns 2 objects, first is numpy array of image data, second is dict of meta data.
It constructs `affine`, `original_affine`, and `spatial_shape` and stores in meta dict.
If loading a list of files, stack them together and add a new dimension as first dimension,
and use the meta data of the first image to represent the stacked result.
"""
img_array: List[np.ndarray] = list()
compatible_meta: Dict = None
if self._img is None:
raise RuntimeError("please call read() first then use get_data().")
for img in self._img:
header = self._get_meta_dict(img)
header["original_affine"] = self._get_affine(img)
header["affine"] = header["original_affine"].copy()
if self.as_closest_canonical:
img = nib.as_closest_canonical(img)
header["affine"] = self._get_affine(img)
header["as_closest_canonical"] = self.as_closest_canonical
header["spatial_shape"] = self._get_spatial_shape(img)
img_array.append(self._get_array_data(img))
if compatible_meta is None:
compatible_meta = header
else:
if not np.allclose(header["affine"], compatible_meta["affine"]):
raise RuntimeError("affine matrix of all images should be same.")
if not np.allclose(header["spatial_shape"], compatible_meta["spatial_shape"]):
raise RuntimeError("spatial_shape of all images should be same.")
img_array_ = np.stack(img_array, axis=0) if len(img_array) > 1 else img_array[0]
return img_array_, compatible_meta
def _get_meta_dict(self, img: Nifti1Image) -> Dict:
"""
Get the all the meta data of the image and convert to dict type.
Args:
img: a Nibabel image object loaded from a image file.
"""
return dict(img.header)
def _get_affine(self, img: Nifti1Image) -> np.ndarray:
"""
Get the affine matrix of the image, it can be used to correct
spacing, orientation or execute spatial transforms.
Args:
img: a Nibabel image object loaded from a image file.
"""
return img.affine
def _get_spatial_shape(self, img: Nifti1Image) -> Sequence:
"""
Get the spatial shape of image data, it doesn't contain the channel dim.
Args:
img: a Nibabel image object loaded from a image file.
"""
ndim = img.header["dim"][0]
spatial_rank = min(ndim, 3)
return list(img.header["dim"][1 : spatial_rank + 1])
def _get_array_data(self, img: Nifti1Image) -> np.ndarray:
"""
Get the raw array data of the image, converted to Numpy array.
Args:
img: a Nibabel image object loaded from a image file.
"""
return np.asarray(img.dataobj)
|