def _load_single_image(self, attr_base, filename): """ Load itk image and store the image itself and its type as a class attribute. The image is stored as `<prefix>_image` attribute and the type of the image is stored under the `<prefix> type` attribute. :param attr_base: The base for the class attribute name. :type attr_base: str :return: None :rtype: None """ # Generating the attributes names based on # the image type. image_attr_name = "_" + attr_base + "_image" type_attr_name = "_" + attr_base + "_type" setattr(self, image_attr_name, pos_itk_transforms.read_itk_image(filename)) setattr(self, type_attr_name, pos_itk_core.autodetect_file_type(filename)) # We read and report the number of components of the image. numbers_of_components = \ getattr(self, image_attr_name).GetNumberOfComponentsPerPixel() self._logger.info("Number of components of the image %s is: %d.", filename, numbers_of_components)
def read_itk_image(image_filename): """ Loads the `image_filename` image. Automatically detects the type of the image and selects approperiate image loader to handle the image file. The returned object is of `itk.Image` type. :param image_filename: File to load :type image_filename: str :return: Itk image object :rtype: `itk.Image` """ # Autodetect the image type, instanciate approperiate reader type, load and # return the image. image_type = pos_itk_core.autodetect_file_type(image_filename) image_reader = itk.ImageFileReader[image_type].New() image_reader.SetFileName(image_filename) image_reader.Update() return image_reader.GetOutput()