itk中创建一个图像数据

来源:互联网 发布:拜占庭东罗马知乎 编辑:程序博客网 时间:2024/06/02 01:59
关于数据的创建,有几种不同的方法可以用。itk提供了一个专业的类:itkImportImageFilter。它实现了从N维数组转到一个itk类型的image数据。不过方法和之前文《itk中的基本图像操作》中的流程大同小异,分别设置了图像的4大要素:SetRegion(区域),SetOrigin(原点),SetSpacing(间距),SetImportPointer(数据)。这种方法的使用环境更适用于自定义数组数据结构与itk算法之间做接口。

详细如下:
  typedef unsigned char   PixelType;  const unsigned int Dimension = 3;  typedef itk::Image< PixelType, Dimension > ImageType;  typedef itk::ImportImageFilter< PixelType, Dimension >   ImportFilterType;   ImportFilterType::Pointer importFilter = ImportFilterType::New();   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 );   double origin[ Dimension ];  origin[0] = 0.0;    // X coordinate  origin[1] = 0.0;    // Y coordinate  origin[2] = 0.0;    // Z coordinate   importFilter->SetOrigin( origin );   double spacing[ Dimension ];  spacing[0] = 1.0;    // along X direction  spacing[1] = 1.0;    // along Y direction  spacing[2] = 1.0;    // along Z direction   importFilter->SetSpacing( spacing );   const unsigned int numberOfPixels =  size[0] * size[1] * size[2];  PixelType * localBuffer = new PixelType[ numberOfPixels ];   const double radius = 80.0;   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; //画一个球 三木运算符        }      }    }   const bool importImageFilterWillOwnTheBuffer = true;  importFilter->SetImportPointer( localBuffer, numberOfPixels,                                  importImageFilterWillOwnTheBuffer );  output_data = importFilter->GetOutput()
类的源码中有一个关键函数(这个方法的精髓在这里):
SetImportPointer(TPixel *ptr(输入数组的指针), unsigned long num(像素点个数), bool LetFilterManageMemory(让过滤器设置内存???没看懂呢?)){  if (ptr != m_ImportPointer)    {    if (m_ImportPointer && m_FilterManageMemory)      {      delete [] m_ImportPointer;      }    m_ImportPointer = ptr;    this->Modified();    }  m_FilterManageMemory = LetFilterManageMemory;  m_Size = num;}
数据生成函数:
GenerateData(){  // Normally, GenerateData() allocates memory.  However, the application  // provides the memory for this filter via the SetImportPointer() method.  // Therefore, this filter does not call outputPtr->Allocate().    // get pointer to the output  OutputImagePointer outputPtr = this->GetOutput();  // the output buffer size is set to the size specified by the user via the  // SetRegion() method.  outputPtr->SetBufferedRegion( outputPtr->GetLargestPossibleRegion() );  // pass the pointer down to the container during each Update() since  // a call to Initialize() causes the container to forget the  // pointer.  Note that we tell the container NOT to manage the  // memory itself.  This filter will properly manage the memory (as  // opposed to the container) if the user wants it to.  outputPtr->GetPixelContainer()->SetImportPointer( m_ImportPointer,                                                    m_Size, false );}

用户需求,产品需求,技术需求;
基本需求,期望需求,兴奋需求;
紧急重要,紧急不重要,不紧急重要,不紧急不重要;
其实不多数事情都能通过产品需求分析使其量化,参数化,这样才能在做决策的时候尽量客观,使风险降到最低。
科学的管理方法是很有用的。

参考文献:
1.https://itk.org/Wiki/ITK/Examples/IO/ImportImageFilter
2.http://blog.csdn.net/ameyume/article/details/6326278

原创粉丝点击