ITK 实例5 定义图像原点和间距

发布时间 2023-08-16 14:14:08作者: 一杯清酒邀明月
  1 #include "itkImage.h"
  2  
  3 // Function to simulate getting mouse click from an image
  4 static itk::Image< unsigned short, 3 >::IndexType GetIndexFromMouseClick()
  5 {
  6   itk::Image< unsigned short, 3 >::IndexType LeftEyeIndex;
  7   LeftEyeIndex[0]=60;
  8   LeftEyeIndex[1]=127;
  9   LeftEyeIndex[2]=93;
 10   return LeftEyeIndex;
 11 }
 12 int main(int, char *[])
 13 {
 14   const unsigned int Dimension=3;
 15   typedef itk::Image< unsigned short, Dimension > ImageType;
 16   ImageType::Pointer image = ImageType::New();
 17  
 18   const ImageType::SizeType  size  = {{ 200, 200, 200}}; //Size along {X,Y,Z}
 19   const ImageType::IndexType start = {{ 0, 0, 0 }}; // First index on {X,Y,Z}
 20  
 21   ImageType::RegionType region;
 22   region.SetSize( size );
 23   region.SetIndex( start );
 24  
 25   image->SetRegions( region );
 26   image->Allocate(true); // initialize buffer to zero
 27  
 28   //图像类中处理原点和间距的方法
 29   //创建一个和图像数据类型相一致的数列spacing
 30   ImageType::SpacingType spacing;
 31   //设定X、Y、Z方向间距
 32   spacing[0] = 0.33; // spacing along X X方向上相邻两像素中心的间距
 33   spacing[1] = 0.33; // spacing along Y Y方向上相邻两像素中心的间距
 34   spacing[2] = 1.20; // spacing along Z Z方向上相邻两像素中心的间距
 35   //使用 SetSpacing( ) 方法指向数列spacing
 36   image->SetSpacing( spacing );
 37   //使用 GetSpacing( ) 方法可以从图像中得到间距信息, const 表示数列是不可修改的
 38   const ImageType::SpacingType& sp = image->GetSpacing();
 39   //输出读取到的图像X、Y、Z方向的间距信息
 40   std::cout << "Spacing = ";
 41   std::cout << sp[0] << ", " << sp[1] << ", " << sp[2] << std::endl;
 42  
 43   //初始化图像原点的变量newOrigin的创建和分配
 44   ImageType::PointType newOrigin;
 45   newOrigin.Fill(0.0);
 46   image->SetOrigin( newOrigin );
 47   // GetOrigin( ) 方法可以从图像中读取原点
 48   const ImageType::PointType & origin = image->GetOrigin();
 49   //输出读取到图像的原点坐标
 50   std::cout << "Origin = ";
 51   std::cout << origin[0] << ", "
 52             << origin[1] << ", "
 53             << origin[2] << std::endl;
 54  
 55  
 56   ImageType::DirectionType direction;
 57   direction.SetIdentity();
 58   image->SetDirection( direction );
 59  
 60   const ImageType::DirectionType& direct = image->GetDirection();
 61  
 62   std::cout << "Direction = " << std::endl;
 63   std::cout << direct << std::endl;
 64   //将物理空间映射到读取最近像素内容的图像index中
 65   //声明一个 itk::Point 类型。这个 Point 类型在用来表示坐标的类型和空间大小之上模块化
 66   typedef itk::Point< double, ImageType::ImageDimension > PointType;
 67   
 68   PointType point;
 69   point[0] = 1.45;    // x coordinate
 70   point[1] = 7.21;    // y coordinate
 71   point[2] = 9.28;    // z coordinate
 72   //图像类型中定义的 IndexType 来对 index 对象进行实例化
 73   ImageType::IndexType pixelIndex;
 74   
 75   // Point 到 index 的映射和访问图像像素数据的像素 index 的用法
 76   const bool isInside =
 77     image->TransformPhysicalPointToIndex( point, pixelIndex );
 78   if ( isInside )
 79     {
 80     ImageType::PixelType pixelValue = image->GetPixel( pixelIndex );
 81     pixelValue += 5;
 82     image->SetPixel( pixelIndex, pixelValue );
 83     }
 84   
 85   const ImageType::IndexType LeftEyeIndex = GetIndexFromMouseClick();
 86   ImageType::PointType LeftEyePoint;
 87   image->TransformIndexToPhysicalPoint(LeftEyeIndex,LeftEyePoint);
 88  
 89   std::cout << "===========================================" << std::endl;
 90   std::cout << "The Left Eye Location is " << LeftEyePoint << std::endl;
 91  
 92   
 93   typedef itk::Matrix<double, Dimension, Dimension> MatrixType;
 94   MatrixType SpacingMatrix;
 95   SpacingMatrix.Fill( 0.0F );
 96  
 97   const ImageType::SpacingType & ImageSpacing = image->GetSpacing();
 98   SpacingMatrix( 0,0 ) = ImageSpacing[0];
 99   SpacingMatrix( 1,1 ) = ImageSpacing[1];
100   SpacingMatrix( 2,2 ) = ImageSpacing[2];
101  
102   const ImageType::DirectionType & ImageDirectionCosines =
103     image->GetDirection();
104   const ImageType::PointType &ImageOrigin = image->GetOrigin();
105  
106   typedef itk::Vector< double, Dimension > VectorType;
107   VectorType LeftEyeIndexVector;
108   LeftEyeIndexVector[0]= LeftEyeIndex[0];
109   LeftEyeIndexVector[1]= LeftEyeIndex[1];
110   LeftEyeIndexVector[2]= LeftEyeIndex[2];
111  
112   ImageType::PointType LeftEyePointByHand =
113      ImageOrigin + ImageDirectionCosines * SpacingMatrix * LeftEyeIndexVector;
114   // Software Guide : EndCodeSnippet
115  
116   std::cout << "===========================================" << std::endl;
117   std::cout << "Spacing:: " << std::endl << SpacingMatrix << std::endl;
118   std::cout << "===========================================" << std::endl;
119   std::cout << "DirectionCosines:: " << std::endl << ImageDirectionCosines << std::endl;
120   std::cout << "===========================================" << std::endl;
121   std::cout << "Origin:: " << std::endl << ImageOrigin << std::endl;
122   std::cout << "===========================================" << std::endl;
123   std::cout << "The Left Eye Location is " << LeftEyePointByHand << std::endl;
124  
125   if ( (LeftEyePointByHand - LeftEyePoint).GetNorm() < 0.01F )
126   {
127     std::cout << "===========================================" << std::endl;
128     std::cout << "Two results are identical as expected!" << std::endl;
129     std::cout << "The Left Eye from TransformIndexToPhysicalPoint is " << LeftEyePoint << std::endl;
130     std::cout << "The Left Eye from Math is " << LeftEyePointByHand << std::endl;
131   }
132  
133   return EXIT_SUCCESS;
134 }