Get the array data and metadata of the segmentation image. Aegs: img: a Pydicom dataset object that has attribute "SegmentSequence". filename: the file path of the image.
(self, img, filename)
| 827 | yield (array_data[indices, ...], seg_descriptions[i]) |
| 828 | |
| 829 | def _get_seg_data(self, img, filename): |
| 830 | """ |
| 831 | Get the array data and metadata of the segmentation image. |
| 832 | |
| 833 | Aegs: |
| 834 | img: a Pydicom dataset object that has attribute "SegmentSequence". |
| 835 | filename: the file path of the image. |
| 836 | |
| 837 | """ |
| 838 | |
| 839 | metadata = self._get_meta_dict(img) |
| 840 | n_classes = len(img.SegmentSequence) |
| 841 | array_data = self._get_array_data(img, filename) |
| 842 | spatial_shape = list(array_data.shape) |
| 843 | spatial_shape[0] = spatial_shape[0] // n_classes |
| 844 | |
| 845 | if self.label_dict is not None: |
| 846 | metadata["labels"] = self.label_dict |
| 847 | if self.to_gpu: |
| 848 | all_segs = cp.zeros([*spatial_shape, len(self.label_dict)], dtype=array_data.dtype) |
| 849 | else: |
| 850 | all_segs = np.zeros([*spatial_shape, len(self.label_dict)], dtype=array_data.dtype) |
| 851 | else: |
| 852 | metadata["labels"] = {} |
| 853 | if self.to_gpu: |
| 854 | all_segs = cp.zeros([*spatial_shape, n_classes], dtype=array_data.dtype) |
| 855 | else: |
| 856 | all_segs = np.zeros([*spatial_shape, n_classes], dtype=array_data.dtype) |
| 857 | |
| 858 | for i, (frames, description) in enumerate(self._get_frame_data(img, filename, array_data)): |
| 859 | segment_label = getattr(description, "SegmentLabel", f"label_{i}") |
| 860 | class_name = getattr(description, "SegmentDescription", segment_label) |
| 861 | if class_name not in metadata["labels"].keys(): |
| 862 | metadata["labels"][class_name] = i |
| 863 | class_num = metadata["labels"][class_name] |
| 864 | all_segs[..., class_num] = frames |
| 865 | |
| 866 | all_segs = all_segs.transpose([1, 2, 0, 3]) |
| 867 | metadata[MetaKeys.SPATIAL_SHAPE] = all_segs.shape[:-1] |
| 868 | |
| 869 | if "52009229" in metadata.keys(): |
| 870 | shared_func_group_seq = metadata["52009229"]["Value"][0] |
| 871 | |
| 872 | # get `ImageOrientationPatient` |
| 873 | if "00209116" in shared_func_group_seq.keys(): |
| 874 | plane_orient_seq = shared_func_group_seq["00209116"]["Value"][0] |
| 875 | if "00200037" in plane_orient_seq.keys(): |
| 876 | metadata["00200037"] = plane_orient_seq["00200037"] |
| 877 | |
| 878 | # get `PixelSpacing` |
| 879 | if "00289110" in shared_func_group_seq.keys(): |
| 880 | pixel_measure_seq = shared_func_group_seq["00289110"]["Value"][0] |
| 881 | |
| 882 | if "00280030" in pixel_measure_seq.keys(): |
| 883 | pixel_spacing = pixel_measure_seq["00280030"]["Value"] |
| 884 | metadata["spacing"] = pixel_spacing |
| 885 | if "00180050" in pixel_measure_seq.keys(): |
| 886 | metadata["spacing"] += pixel_measure_seq["00180050"]["Value"] |
no test coverage detected