ITK系列5_ ITK定义图像原点和间距

该博客介绍了如何在ITK(Insight Toolkit)中设置图像的原点、间距,并展示了从物理点到像素索引的映射过程。通过示例代码,解释了如何初始化和修改图像的尺寸、空间间隔和原点,以及验证映射的准确性。
摘要由CSDN通过智能技术生成

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

#include "itkImage.h"

// Function to simulate getting mouse click from an image
static itk::Image< unsigned short, 3 >::IndexType GetIndexFromMouseClick()
{
  itk::Image< unsigned short, 3 >::IndexType LeftEyeIndex;
  LeftEyeIndex[0]=60;
  LeftEyeIndex[1]=127;
  LeftEyeIndex[2]=93;
  return LeftEyeIndex;
}
int main(int, char *[])
{
  const unsigned int Dimension=3;
  typedef itk::Image< unsigned short, Dimension > ImageType;
  ImageType::Pointer image = ImageType::New();

  const ImageType::SizeType  size  = {{ 200, 200, 200}}; //Size along {X,Y,Z}
  const ImageType::IndexType start = {{ 0, 0, 0 }}; // First index on {X,Y,Z}

  ImageType::RegionType region;
  region.SetSize( size );
  region.SetIndex( start );

  image->SetRegions( region );
  image->Allocate(true); // initialize buffer to zero

  //图像类中处理原点和间距的方法
  //创建一个和图像数据类型相一致的数列spacing
  ImageType::SpacingType spacing;
  //设定X、Y、Z方向间距
  spacing[0] = 0.33; // spacing along X X方向上相邻两像素中心的间距
  spacing[1] = 0.33; // spacing along Y Y方向上相邻两像素中心的间距
  spacing[2] = 1.20; // spacing along Z Z方向上相邻两像素中心的间距
  //使用 SetSpacing( ) 方法指向数列spacing
  image->SetSpacing( spacing );
  //使用 GetSpacing( ) 方法可以从图像中得到间距信息, const 表示数列是不可修改的
  const ImageType::SpacingType& sp = image->GetSpacing();
  //输出读取到的图像X、Y、Z方向的间距信息
  std::cout << "Spacing = ";
  std::cout << sp[0] << ", " << sp[1] << ", " << sp[2] << std::endl;

  //初始化图像原点的变量newOrigin的创建和分配
  ImageType::PointType newOrigin;
  newOrigin.Fill(0.0);
  image->SetOrigin( newOrigin );
  // GetOrigin( ) 方法可以从图像中读取原点
  const ImageType::PointType & origin = image->GetOrigin();
  //输出读取到图像的原点坐标
  std::cout << "Origin = ";
  std::cout << origin[0] << ", "
            << origin[1] << ", "
            << origin[2] << std::endl;


  ImageType::DirectionType direction;
  direction.SetIdentity();
  image->SetDirection( direction );
 
  const ImageType::DirectionType& direct = image->GetDirection();

  std::cout << "Direction = " << std::endl;
  std::cout << direct << std::endl;
  //将物理空间映射到读取最近像素内容的图像index中
  //声明一个 itk::Point 类型。这个 Point 类型在用来表示坐标的类型和空间大小之上模块化
  typedef itk::Point< double, ImageType::ImageDimension > PointType;
  
  PointType point;
  point[0] = 1.45;    // x coordinate
  point[1] = 7.21;    // y coordinate
  point[2] = 9.28;    // z coordinate
  //图像类型中定义的 IndexType 来对 index 对象进行实例化
  ImageType::IndexType pixelIndex;
  
  // Point 到 index 的映射和访问图像像素数据的像素 index 的用法
  const bool isInside =
    image->TransformPhysicalPointToIndex( point, pixelIndex );
  if ( isInside )
    {
    ImageType::PixelType pixelValue = image->GetPixel( pixelIndex );
    pixelValue += 5;
    image->SetPixel( pixelIndex, pixelValue );
    }
  
  const ImageType::IndexType LeftEyeIndex = GetIndexFromMouseClick();
  ImageType::PointType LeftEyePoint;
  image->TransformIndexToPhysicalPoint(LeftEyeIndex,LeftEyePoint);

  std::cout << "===========================================" << std::endl;
  std::cout << "The Left Eye Location is " << LeftEyePoint << std::endl;

  
  typedef itk::Matrix<double, Dimension, Dimension> MatrixType;
  MatrixType SpacingMatrix;
  SpacingMatrix.Fill( 0.0F );

  const ImageType::SpacingType & ImageSpacing = image->GetSpacing();
  SpacingMatrix( 0,0 ) = ImageSpacing[0];
  SpacingMatrix( 1,1 ) = ImageSpacing[1];
  SpacingMatrix( 2,2 ) = ImageSpacing[2];

  const ImageType::DirectionType & ImageDirectionCosines =
    image->GetDirection();
  const ImageType::PointType &ImageOrigin = image->GetOrigin();

  typedef itk::Vector< double, Dimension > VectorType;
  VectorType LeftEyeIndexVector;
  LeftEyeIndexVector[0]= LeftEyeIndex[0];
  LeftEyeIndexVector[1]= LeftEyeIndex[1];
  LeftEyeIndexVector[2]= LeftEyeIndex[2];

  ImageType::PointType LeftEyePointByHand =
     ImageOrigin + ImageDirectionCosines * SpacingMatrix * LeftEyeIndexVector;
  // Software Guide : EndCodeSnippet

  std::cout << "===========================================" << std::endl;
  std::cout << "Spacing:: " << std::endl << SpacingMatrix << std::endl;
  std::cout << "===========================================" << std::endl;
  std::cout << "DirectionCosines:: " << std::endl << ImageDirectionCosines << std::endl;
  std::cout << "===========================================" << std::endl;
  std::cout << "Origin:: " << std::endl << ImageOrigin << std::endl;
  std::cout << "===========================================" << std::endl;
  std::cout << "The Left Eye Location is " << LeftEyePointByHand << std::endl;

  if ( (LeftEyePointByHand - LeftEyePoint).GetNorm() < 0.01F )
  {
    std::cout << "===========================================" << std::endl;
    std::cout << "Two results are identical as expected!" << std::endl;
    std::cout << "The Left Eye from TransformIndexToPhysicalPoint is " << LeftEyePoint << std::endl;
    std::cout << "The Left Eye from Math is " << LeftEyePointByHand << std::endl;
  }

  return EXIT_SUCCESS;
}

ITK系列目录:

1 ITK图像数据表达之图像

2 ITK图像处理之图像滤波

3 ITK图像处理之图像分割

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

亦我飞也

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

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

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

打赏作者

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

抵扣说明:

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

余额充值