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

标签 c++ image-processing itk

我是 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);

最佳答案

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

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

关于c++ - ITK:未能正确调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31705344/

相关文章:

C++ 模板特化编译

Python - 在不使用内置旋转方法的情况下旋转图像

python - 在使用 simpleITK (python) 注册期间捕获警告

php - 将不透明像素转换为黑色

iphone - 我想在 iOS 中实现图像处理算法,你能推荐一些好的入门指南吗

java - 如何将 int32Array 转换为 java int []?

c++ - itk OtsuMultipleThresholdsImageFilter 不处理

c++ - 修改 C++ vector 中的对象值时出现问题

c++ - Boost Spirit编译错误 "error_invalid_expression"

c++ - 排序函数给出大量输入 0 的浮点异常