[docs]classKeepLargestComponent(LabelTransform):r"""Keep only the largest connected component in a binary label map. Args: **kwargs: See :class:`~torchio.transforms.Transform` for additional keyword arguments. .. note:: For now, this transform only works for binary images, i.e., label maps with a background and a foreground class. If you are interested in extending this transform, please `open a new issue`_. .. _open a new issue: https://github.com/fepegar/torchio/issues/new?assignees=&labels=enhancement&template=feature_request.md&title=Improve%20KeepLargestComponent%20transform """# noqa: B950defapply_transform(self,subject:Subject)->Subject:forimageinself.get_images(subject):ifimage.num_channels>1:message=('The number of input channels must be 1,'f' but it is {image.num_channels}')raiseRuntimeError(message)sitk_image=image.as_sitk()connected_components=sitk.ConnectedComponent(sitk_image)labeled_cc=sitk.RelabelComponent(connected_components)largest_cc=labeled_cc==1tensor,_=self.sitk_to_nib(largest_cc)image.set_data(tensor)returnsubject