欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

itk 3d 旋转 平移 重采样

程序员文章站 2022-04-01 07:57:20
...

网上和书的的 一些例子是2d ,这里分享一个3D

     typedef itk::LinearInterpolateImageFunction <ImageType, double>  InterpolatorType;
     InterpolatorType::Pointer interpolator = InterpolatorType::New();
     typedef itk::ResampleImageFilter< ImageType, ImageType >  RotateFilterType;
     RotateFilterType::Pointer Rotatefilter = RotateFilterType::New();
     Rotatefilter->SetInterpolator(interpolator);
     Rotatefilter->SetDefaultPixelValue(0);

     const ImageType * inputImage = move_Connector->GetOutput();
     const ImageType::SpacingType & spacing = inputImage->GetSpacing();
     const ImageType::PointType & origin = inputImage->GetOrigin();
     ImageType::SizeType size = inputImage->GetLargestPossibleRegion().GetSize();
     Rotatefilter->SetOutputOrigin(origin);
     Rotatefilter->SetOutputSpacing(spacing);
     Rotatefilter->SetOutputDirection(inputImage->GetDirection());
     Rotatefilter->SetSize(size);
     Rotatefilter->SetInput(inputImage);

    
     typedef itk::AffineTransform<double, 3>  TransformType;
     TransformType::Pointer transform = TransformType::New();

         // 另外一种方式实现
     //TransformType::OutputVectorType translation1;
     //const double imageCenterX = origin[0] + spacing[0] * size[0] / 2.0;
     //const double imageCenterY = origin[1] + spacing[1] * size[1] / 2.0;
     //const double imageCenterZ = origin[2] + spacing[2] * size[2] / 2.0;
     //translation1[0] = -imageCenterX;
     //translation1[1] = -imageCenterY;
     //translation1[2] = -imageCenterZ;
     //transform->Translate(translation1);
     //std::cout << "imageCenterX = " << imageCenterX << std::endl;
     //std::cout << "imageCenterY = " << imageCenterY << std::endl;
     //std::cout << "imageCenterZ = " << imageCenterZ << std::endl;
     //const double degreesToRadians = vcl_atan(1.0) / 45.0;
     //const double angle = 30 * degreesToRadians;

     //TransformType::OutputVectorType translationXYZ;
     //translationXYZ[0] = 1;
     //translationXYZ[1] = 0;
     //translationXYZ[2] = 0;
     //
     //transform->Rotate3D(translationXYZ, -angle, false);
     //TransformType::OutputVectorType translation2;
     //translation2[0] = imageCenterX;
     //translation2[1] = imageCenterY;
     //translation2[2] = imageCenterZ;
     //transform->Translate(translation1, false);

     typedef  TransformType::ParametersType ParametersType;
     typedef  ParametersType::ValueType   ValueType;
     ParametersType * pParametersType = new ParametersType(6);
         double itkTrans[3] = { -0.065351,
             0.018019,
             0.035492 };
    
         double itkMax[9] = { 0.903796,
             -0.223097,
             0.365212,
             0.316126,
             0.923252,
             -0.218336,
             -0.288473,
             0.312784,
             0.904958 };
 
     Matrix< double, 3, 3 > RotationX;
     int indexx = 0;
     for (int i = 0; i < 3; i++)
         for (int j = 0; j < 3; j++)
         {
             RotationX[i][j] = itkMax[indexx];
             indexx++;
         }
     typedef   TransformType::OutputVectorType          OutputVectorType;
     OutputVectorType newTranslation;
     newTranslation[0] = itkTrans[0];
     newTranslation[1] = itkTrans[1];
     newTranslation[2] = itkTrans[2];
     transform->SetMatrix(RotationX);
     transform->SetTranslation(newTranslation);
    
     Rotatefilter->SetTransform(transform);

 
     ImageType::DirectionType direction;
     direction.SetIdentity();
     Rotatefilter->SetOutputDirection(direction);
     try
     {
         Rotatefilter->UpdateLargestPossibleRegion();
     }
     catch (itk::ExceptionObject & exp)
     {
         cerr << "Exception caught !" << std::endl;
         cerr << exp << std::endl;
     }
    
     try{
         Rotatefilter->Update();
     }
     catch (itk::ExceptionObject &e){
         std::cerr << e << std::endl;
         std::cerr << "Rotatefilter caught !" << std::endl;
        
     }

 

相关标签: ITK