Get the array data of the image. If `RescaleSlope` and `RescaleIntercept` are available, the raw array data will be rescaled. The output data has the dtype float32 if the rescaling is applied. Args: img: a Pydicom dataset object. filename: the file p
(self, img, filename)
| 959 | return data |
| 960 | |
| 961 | def _get_array_data(self, img, filename): |
| 962 | """ |
| 963 | Get the array data of the image. If `RescaleSlope` and `RescaleIntercept` are available, the raw array data |
| 964 | will be rescaled. The output data has the dtype float32 if the rescaling is applied. |
| 965 | |
| 966 | Args: |
| 967 | img: a Pydicom dataset object. |
| 968 | filename: the file path of the image. |
| 969 | |
| 970 | """ |
| 971 | # process Dicom series |
| 972 | |
| 973 | if self.to_gpu: |
| 974 | data = self._get_array_data_from_gpu(img, filename) |
| 975 | else: |
| 976 | if not hasattr(img, "pixel_array"): |
| 977 | raise ValueError(f"dicom data: {filename} does not have pixel_array.") |
| 978 | data = img.pixel_array |
| 979 | |
| 980 | slope, offset = 1.0, 0.0 |
| 981 | rescale_flag = False |
| 982 | if hasattr(img, "RescaleSlope"): |
| 983 | slope = img.RescaleSlope |
| 984 | rescale_flag = True |
| 985 | if hasattr(img, "RescaleIntercept"): |
| 986 | offset = img.RescaleIntercept |
| 987 | rescale_flag = True |
| 988 | |
| 989 | if rescale_flag: |
| 990 | if self.to_gpu: |
| 991 | slope = cp.asarray(slope, dtype=cp.float32) |
| 992 | offset = cp.asarray(offset, dtype=cp.float32) |
| 993 | data = data.astype(cp.float32) * slope + offset |
| 994 | else: |
| 995 | data = data.astype(np.float32) * slope + offset |
| 996 | |
| 997 | return data |
| 998 | |
| 999 | |
| 1000 | @require_pkg(pkg_name="nibabel") |
no test coverage detected