ITK:未能正确调整图像大小

ITK: Failure to properly resize an image

我是 ITK 的新手,在尝试调整图像大小时遇到​​了问题。我觉得我正在遵循示例代码 (ResampleImageFilter),但我始终得到一个输出图像,据报告其“总质量”为零,并且在写入磁盘时生成图像大小合适 (outputSize),但完全空白。

如有任何帮助,我们将不胜感激。 谢谢

typedef unsigned char               PixelType;
typedef itk::Image<PixelType, 2>    ImageType;

...

ImageType::Pointer resize(ImageType::Pointer image, ImageType::SizeType inputSize, ImageType::SizeType outputSize){
    ImageType::SpacingType outputSpacing;
    outputSpacing[0] = image->GetSpacing()[0] * (static_cast<double>(inputSize[0]) / static_cast<double>(outputSize[0]));
    outputSpacing[1] = image->GetSpacing()[1] * (static_cast<double>(inputSize[1]) / static_cast<double>(outputSize[1]));

    typedef itk::IdentityTransform<double, 2> TransformType;
    typedef itk::ResampleImageFilter<ImageType, ImageType> ResampleImageFilterType;

    ResampleImageFilterType::Pointer resample = ResampleImageFilterType::New();
    resample->SetInput(image);
    resample->SetSize(outputSize);
    resample->SetOutputSpacing(outputSpacing);
    resample->SetTransform(TransformType::New());
    resample->Update();
    resample->UpdateOutputInformation();

    return resample->GetOutput();
}

...

movingImage = resize(croppedImage, cropped_img_size, img_size);

@note:

cropped_img_size == [1251, 787]

img_size == [1251, 814]

编辑:

工作版本:

typedef unsigned char               PixelType;
typedef itk::Image<PixelType, 2>    ImageType;

...

ImageType::Pointerresize(ImageType::Pointer image, ImageType::Pointer referenceImage){
    ImageType::SizeType inputSize  = image->GetLargestPossibleRegion().GetSize();
    ImageType::SizeType outputSize = referenceImage->GetLargestPossibleRegion().GetSize();

    ImageType::SpacingType outputSpacing;
    outputSpacing[0] = image->GetSpacing()[0] * (static_cast<double>(inputSize[0]) / static_cast<double>(outputSize[0]));
    outputSpacing[1] = image->GetSpacing()[1] * (static_cast<double>(inputSize[1]) / static_cast<double>(outputSize[1]));

    typedef itk::IdentityTransform <double, 2>              TransformType;
    typedef itk::ResampleImageFilter<ImageType, ImageType>  ResampleImageFilterType;

    ResampleImageFilterType::Pointer resample = ResampleImageFilterType::New();
    resample->SetInput(image);
    resample->SetOutputParametersFromImage(referenceImage);
    resample->SetSize(outputSize);
    resample->SetOutputSpacing(outputSpacing);
    resample->SetTransform(TransformType::New());
    resample->UpdateOutputInformation();
    resample->Update();

    return resample->GetOutput();
}

...

movingImage = resize(croppedImage, fixedImage);

您的术语 "resize" 不是查看 ITK ResampleImageFilter 的正确概念。调整大小意味着简单的像素到像素操作。 ITK 图像的一个基本概念是它具有由以下定义的物理位置:原点、间距和方向矩阵。 ResampleImageFilter 应用从输入图像的物理 space 到由 ResampleImageFiler 的输出参数定义的输出图像的物理 space 的几何变换。

您忘记设置 "OutputOrigin" 和 "OutputDirection"。或者,您可以调用 "SetOutputParametersFromImage",然后只需设置不同的参数,例如 "OutputSpacing".