ITK系列8_ 从缓冲器中输入图像数据

实例8 从缓冲器中输入图像数据

#include "itkImage.h"
#include "itkImportImageFilter.h"//包含 ImportImageFilter(图像像素数据导入缓冲器) 类的头文件
#include "itkImageFileWriter.h"
//这个例子阐述了如何将数据输入到 itk::Image 类中。这在同其他软件系统相连时更加有
//用。许多系统都使用内存的一个邻近内存块作为图像像素数据的缓冲器。当前样例就是假定
//这种情况并在缓冲器中插入一个 itk::ImportImageFilter ,从而产生一个图像作为输出。
//我们调用内存中心块创建一个同步的图像并将这个内存块传给 ImportImageFilter 。这个
//例子是基于运行上而设定的,用户必须提供一个输出文件名作为一个命令行变量。
int main(int argc, char * argv[])
{
  /*if( argc < 2 )
    {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0] << "  outputImageFile" << std::endl;
    return EXIT_FAILURE;
    }*/
  /*选择数据类型来表示图像像素。我们假设内存的外部内存块使用同样的数据类
    型来表示像素*/
  typedef unsigned char   PixelType;
  const unsigned int Dimension = 3;

  typedef itk::Image< PixelType, Dimension > ImageType;
  //ImportImageFilter 类型的实例化
  typedef itk::ImportImageFilter< PixelType, Dimension >   ImportFilterType;
  //使用 New( ) 方法创建一个滤镜对象importFilter然后指向一个智能指针
  ImportFilterType::Pointer importFilter = ImportFilterType::New();
  /*滤镜要求用户指定图像的大小来作为输出,使用 SetRgion() 方法即可做到。图像大小必
   须和当前调用的缓冲器的像素变量的数字相匹配*/
  ImportFilterType::SizeType  size;
  size[0]  = 200;  // size along X
  size[1]  = 200;  // size along Y
  size[2]  = 200;  // size along Z
  ImportFilterType::IndexType start;
  start.Fill(0);

  ImportFilterType::RegionType region;
  region.SetIndex( start );
  region.SetSize(  size  );
  importFilter->SetRegion( region );
  //使用 SetOrigin() 方法来指定输出图像的原点
  const itk::SpacePrecisionType origin[ Dimension ] = { 0.0, 0.0, 0.0 };
  importFilter->SetOrigin( origin );
  //使用 SetSpacing( ) 方法来传递输出图像的间距
  const itk::SpacePrecisionType  spacing[ Dimension ] =  { 1.0, 1.0, 1.0 };
  importFilter->SetSpacing( spacing );
  /*现在我们分配包含像素数据的内存块传递信息到 ImportImageFilter 。注意:我们使用与
  SetRegion() 方法指定的大小完全相同的尺寸。在实际应用中,你可以使用一个代表图像的
  不同的数据结构从一些其他的类库中得到这个缓冲器。*/
  const unsigned int numberOfPixels =  size[0] * size[1] * size[2];
  PixelType * localBuffer = new PixelType[ numberOfPixels ];

  const double radius = 80.0;
  /*这里可以用一个 binary sphere 来填充这个缓冲器。这里我们像 C 或 FOTTRAN 编程语
  言一样使用简单的 for () 循环。注意: ITK 在其访问像素的内部编码中不能使用 for () 循环。
  使用支持处理 n 维图像的 itk::ImageIterators 来代替执行所以的像素访问任务。*/
  const double radius2 = radius * radius;
  PixelType * it = localBuffer;

  for(unsigned int z=0; z < size[2]; z++)
    {
    const double dz = static_cast<double>( z )
      - static_cast<double>(size[2])/2.0;
    for(unsigned int y=0; y < size[1]; y++)
      {
      const double dy = static_cast<double>( y )
        - static_cast<double>(size[1])/2.0;
      for(unsigned int x=0; x < size[0]; x++)
        {
        const double dx = static_cast<double>( x )
          - static_cast<double>(size[0])/2.0;
        const double d2 = dx*dx + dy*dy + dz*dz;
        *it++ = ( d2 < radius2 ) ? 255 : 0;
        }
      }
    }
 /* 缓冲器在 SetImportPointer() 作用下传递到 ImportImageFilter 。注意这种方法的最后一个
 问题是当内存不再使用时指定谁来释放内存。当返回值为假时,表示当调用析构时
 ImportImageFilter 并没有释放缓冲器;另一方面,当返回值是真时,表示允许释放析构的输
 入滤镜上的内存块。
 由于 ImportImageFilter 释放了适当的内存块, C++ new() 操作就可以调用这些内存。用
 其他分配内存机制分配的内存,比如 C 中的 malloc 和 calloc ,将不会由 ImportImageFilter
 来释放适当的内存。换句话说,编程应用者就需要确保仅仅给 ImportImageFilter 命令来释放
 C++ 新操作分配内存。*/
  const bool importImageFilterWillOwnTheBuffer = true;
  importFilter->SetImportPointer( localBuffer, numberOfPixels,
                                  importImageFilterWillOwnTheBuffer );
  typedef itk::ImageFileWriter< ImageType > WriterType;
  WriterType::Pointer writer = WriterType::New();

  writer->SetFileName(argv[1]);
  writer->SetFileName("123.png");
  /*最后,我们将这个滤镜的输出连到一个管道上。为简便起见,我们在这里只使用一个
  writer ,当然其他任何滤镜都可以:*/
  writer->SetInput(  importFilter->GetOutput()  );

  try
    {
    writer->Update();
    }
  catch( itk::ExceptionObject & exp )
    {
    std::cerr << "Exception caught !" << std::endl;
    std::cerr << exp << std::endl;
    return EXIT_FAILURE;
    }
  return EXIT_SUCCESS;
}
//注意:我们传递 true 作为 SetImportPointer() 的最后问题就不需要对缓冲器调用释放操
//作。现在缓冲器归 ImportImageFilter 所有

                                                

            输入图像123.png                                                            写入数据后的图像123.png

ITK系列目录:

1 ITK图像数据表达之图像

2 ITK图像处理之图像滤波

3 ITK图像处理之图像分割

注:例程配套素材见系列目录

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亦我飞也

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值