示例#1
0
    def _cmp(self, filename, shape, ch_shape, reader_1, reader_2, outname,
             ext):
        data_dict = {"img": filename}
        keys = data_dict.keys()
        xforms = Compose(
            [LoadImaged(keys, reader=reader_1, ensure_channel_first=True)])
        img_dict = xforms(data_dict)  # load dicom with itk
        self.assertTupleEqual(img_dict["img"].shape, ch_shape)
        self.assertTupleEqual(
            tuple(img_dict[PostFix.meta("img")]["spatial_shape"]), shape)

        with tempfile.TemporaryDirectory() as tempdir:
            save_xform = SaveImageD(keys,
                                    meta_keys=PostFix.meta("img"),
                                    output_dir=tempdir,
                                    squeeze_end_dims=False,
                                    output_ext=ext)
            save_xform(img_dict)  # save to nifti

            new_xforms = Compose(
                [LoadImaged(keys, reader=reader_2),
                 EnsureChannelFirstD(keys)])
            out = new_xforms({"img":
                              os.path.join(tempdir,
                                           outname)})  # load nifti with itk
            self.assertTupleEqual(out["img"].shape, ch_shape)
            self.assertTupleEqual(
                tuple(out[PostFix.meta("img")]["spatial_shape"]), shape)
            if "affine" in img_dict[PostFix.meta("img")] and "affine" in out[
                    PostFix.meta("img")]:
                np.testing.assert_allclose(
                    img_dict[PostFix.meta("img")]["affine"],
                    out[PostFix.meta("img")]["affine"],
                    rtol=1e-3)
            np.testing.assert_allclose(out["img"], img_dict["img"], rtol=1e-3)
示例#2
0
    def test_channel_dim(self):
        spatial_size = (32, 64, 3, 128)
        test_image = np.random.rand(*spatial_size)
        with tempfile.TemporaryDirectory() as tempdir:
            filename = os.path.join(tempdir, "test_image.nii.gz")
            nib.save(nib.Nifti1Image(test_image, affine=np.eye(4)), filename)

            loader = LoadImaged(keys="img", image_only=True)
            loader.register(ITKReader(channel_dim=2))
            t = Compose([EnsureChannelFirstD("img"), FromMetaTensord("img")])
            result = t(loader({"img": filename}))
            self.assertTupleEqual(result["img"].shape, (3, 32, 64, 128))
示例#3
0
    def test_channel_dim(self):
        spatial_size = (32, 64, 3, 128)
        test_image = np.random.rand(*spatial_size)
        with tempfile.TemporaryDirectory() as tempdir:
            filename = os.path.join(tempdir, "test_image.nii.gz")
            nib.save(nib.Nifti1Image(test_image, affine=np.eye(4)), filename)

            loader = LoadImaged(keys="img")
            loader.register(ITKReader(channel_dim=2))
            result = EnsureChannelFirstD("img")(loader({"img": filename}))
            self.assertTupleEqual(tuple(result["img_meta_dict"]["spatial_shape"]), (32, 64, 128))
            self.assertTupleEqual(result["img"].shape, (3, 32, 64, 128))
示例#4
0
    def _cmp(self, filename, ch_shape, reader_1, reader_2, outname, ext):
        data_dict = {"img": filename}
        keys = data_dict.keys()
        xforms = Compose([
            LoadImaged(keys,
                       reader=reader_1,
                       ensure_channel_first=True,
                       image_only=True)
        ])
        img_dict = xforms(data_dict)  # load dicom with itk
        self.assertTupleEqual(img_dict["img"].shape, ch_shape)

        with tempfile.TemporaryDirectory() as tempdir:
            save_xform = SaveImageD(keys,
                                    output_dir=tempdir,
                                    squeeze_end_dims=False,
                                    output_ext=ext)
            save_xform(img_dict)  # save to nifti

            new_xforms = Compose([
                LoadImaged(keys, reader=reader_2, image_only=True),
                EnsureChannelFirstD(keys),
                FromMetaTensord(keys),
                ToMetaTensord(keys),
            ])
            out = new_xforms({"img":
                              os.path.join(tempdir,
                                           outname)})  # load nifti with itk
            self.assertTupleEqual(out["img"].shape, ch_shape)

            def is_identity(x):
                return (x == torch.eye(x.shape[0])).all()

            if not is_identity(img_dict["img"].affine) and not is_identity(
                    out["img"].affine):
                assert_allclose(img_dict["img"].affine,
                                out["img"].affine,
                                rtol=1e-3)
            assert_allclose(out["img"], img_dict["img"], rtol=1e-3)