ITK 实例7 向量图像(将一个向量存储到一个图像像素中)

发布时间 2023-08-16 14:19:14作者: 一杯清酒邀明月
 1 #include "itkVector.h"//向量类的头文件
 2 #include "itkImage.h"
 3  
 4 int main(int, char *[])
 5 {
 6   /*向量类的使用是在基于空间中代表坐标和维数的类型之上进行模板化的。在此例中,向
 7   量的长度和图像长度相匹配,但是并不是完全相同。我们可以用一个三维的向量作为像素来
 8   定义一个四维的图像*/
 9   typedef itk::Vector< float, 3 >       PixelType;
10   typedef itk::Image< PixelType, 3 >    ImageType;
11   
12   ImageType::Pointer image = ImageType::New();
13  
14   const ImageType::IndexType start = {{0,0,0}}; //First index at {X,Y,Z}
15   const ImageType::SizeType  size = {{200,200,200}}; //Size of {X,Y,Z}
16  
17   ImageType::RegionType region;
18   region.SetSize( size );
19   region.SetIndex( start );
20  
21   image->SetRegions( region );
22   image->Allocate();
23  
24   ImageType::PixelType  initialValue;
25   initialValue.Fill( 0.0 );
26   image->FillBuffer( initialValue );
27   const ImageType::IndexType pixelIndex = {{27,29,37}}; //{X,Y,Z}对应像素索引位置
28   //由于向量类从 itk::FixedArray 类继承了[] 操作,所以也可以使用 index 符号来访问向量成员
29   ImageType::PixelType   pixelValue;
30   pixelValue[0] =  1.345;   // x component
31   pixelValue[1] =  6.841;   // y component
32   pixelValue[2] =  3.295;   // x component
33  
34   std::cout << "pixelIndex索引处给定的向量值:" << std::endl;
35   std::cout << pixelValue << std::endl;
36   //通过定义一个标识并调用 SetPixel( ) 方法来将这个向量储存到pixelIndex索引像素中
37   image->SetPixel(   pixelIndex,   pixelValue  );
38   //获取pixelIndex处像素值value
39   ImageType::PixelType value = image->GetPixel( pixelIndex );
40   //打印像素值value
41   std::cout << "pixelIndex索引处读取的像素值:" << std::endl;
42   std::cout << value << std::endl;
43  
44   return EXIT_SUCCESS;
45 }