网上和书的的 一些例子是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;
}
222222222222222222222222222222
//函数化 升采样 重置大小
void Resize(ImageType::Pointer input,ImageType::Pointer src,ImageType::Pointer* dst)
{
std::cout <<"----升采样----"<< std::endl;
typedef itk::ResampleImageFilter<ImageType, ImageType> ResampleImageFilterType;
ResampleImageFilterType::Pointer resample = ResampleImageFilterType::New();
resample->SetInput(input);
typedef itk::AffineTransform<double,2> TransformType;
TransformType::Pointer transform = TransformType::New();
resample->SetTransform(transform);
typedef itk::NearestNeighborInterpolateImageFunction<ImageType,double> InterpolatorType;
InterpolatorType::Pointer interpolator = InterpolatorType::New();
resample->SetInterpolator(interpolator);
resample->SetDefaultPixelValue(0);
resample->SetSize(src->GetLargestPossibleRegion().GetSize());
resample->SetOutputSpacing(src->GetSpacing());
resample->SetOutputOrigin(src->GetOrigin());
ImageType2D::DirectionType direction;
direction.SetIdentity();
resample->SetOutputDirection(direction);
try
{
resample->UpdateLargestPossibleRegion();
}
catch( itk::ExceptionObject & exp )
{
cerr << "Exception caught !" << std::endl;
cerr << exp << std::endl;
}
*dst = resample->GetOutput();
}