ITK深拷贝itk::Image

 ITK版本:5.0.1

 

1. 自己实现拷贝数据:

template<typename TPixel, uint Dim>
void deepCopyItkImageData(const itk::Image<TPixel, Dim>* src, itk::Image<TPixel, Dim>* dst)
{
    if (!src || !dst) {
        return;
    }
    dst->CopyInformation(src);
    dst->SetMetaDataDictionary(src->GetMetaDataDictionary());
    dst->SetRegions(src->GetLargestPossibleRegion());
    dst->Allocate();
    std::copy_n(src->GetBufferPointer(),
        src->GetPixelContainer()->Size(), dst->GetBufferPointer());
}

template<typename TPixel, uint Dim>
typename itk::Image<TPixel, Dim>::Pointer deepCopyItkImageData(const itk::Image<TPixel, Dim>* data)
{
    using ImageT = itk::Image<TPixel, Dim>;
    typename ImageT::Pointer ret = ImageT::New();
    deepCopyItkImageData(data, ret.GetPointer());
    return ret;
}

 

2. 利用itkImageDuplicator类实现拷贝:

#include "itkImage.h"
#include "itkImageDuplicator.h"
#include "itkRandomImageSource.h"

int
main(int, char *[])
{
  constexpr unsigned int Dimension = 2;
  using PixelType = unsigned char;

  using ImageType = itk::Image<PixelType, Dimension>;

  using RandomSourceType = itk::RandomImageSource<ImageType>;

  RandomSourceType::Pointer randomImageSource = RandomSourceType::New();
  randomImageSource->SetNumberOfWorkUnits(1); // to produce non-random results

  ImageType::Pointer image = randomImageSource->GetOutput();

  using DuplicatorType = itk::ImageDuplicator<ImageType>;
  DuplicatorType::Pointer duplicator = DuplicatorType::New();
  duplicator->SetInputImage(image);
  duplicator->Update();

  ImageType::Pointer clonedImage = duplicator->GetOutput();

  return EXIT_SUCCESS;
}

参见:ITK Example - Duplicate An Image

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页