forked from ANTsX/ANTs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetOrigin.cxx
115 lines (100 loc) · 3.44 KB
/
SetOrigin.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*=========================================================================
Program: Advanced Normalization Tools
Module: $RCSfile: SetOrigin.cxx,v $
Language: C++
Date: $Date: 2008/11/15 23:46:06 $
Version: $Revision: 1.19 $
Copyright (c) ConsortiumOfANTS. All rights reserved.
See accompanying COPYING.txt or
http://sourceforge.net/projects/advants/files/ANTS/ANTSCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include <iostream>
#include <fstream>
#include <stdio.h>
#include "itkImage.h"
#include "itkImageFileWriter.h"
#include "itkImageFileReader.h"
#include "itkCastImageFilter.h"
#include "itkRescaleIntensityImageFilter.h"
template <unsigned int ImageDimension>
int SetOrigin(int argc, char *argv[])
{
typedef float outPixelType;
typedef float floatPixelType;
typedef float inPixelType;
typedef itk::Image<inPixelType, ImageDimension> ImageType;
typedef itk::Image<floatPixelType, ImageDimension> IntermediateType;
typedef itk::Image<outPixelType, ImageDimension> OutImageType;
typedef itk::ImageFileReader<ImageType> readertype;
typedef itk::ImageFileWriter<OutImageType> writertype;
typename readertype::Pointer reader = readertype::New();
reader->SetFileName(argv[1]);
reader->Update();
typename OutImageType::Pointer outim = reader->GetOutput();
typename OutImageType::PointType orig = outim->GetOrigin();
std::cout << " Old Orig " << outim->GetOrigin();
if( argc > 3 )
{
orig[0] = atof(argv[3]);
}
if( argc > 4 )
{
orig[1] = atof(argv[4]);
}
if( argc > 5 )
{
orig[2] = atof(argv[5]);
}
std::cout << " New Orig " << orig << std::endl;
outim->SetOrigin(orig);
typedef itk::ImageRegionIteratorWithIndex<ImageType> Iterator;
typename ImageType::Pointer varimage = ImageType::New();
varimage->SetLargestPossibleRegion( outim->GetLargestPossibleRegion() );
varimage->SetBufferedRegion( outim->GetLargestPossibleRegion() );
varimage->SetLargestPossibleRegion( outim->GetLargestPossibleRegion() );
varimage->Allocate();
varimage->SetSpacing(outim->GetSpacing() );
varimage->SetOrigin(orig);
varimage->SetDirection( outim->GetDirection() );
Iterator vfIter2( varimage, varimage->GetLargestPossibleRegion() );
for( vfIter2.GoToBegin(); !vfIter2.IsAtEnd(); ++vfIter2 )
{
vfIter2.Set(outim->GetPixel(vfIter2.GetIndex() ) );
}
typename writertype::Pointer writer = writertype::New();
writer->SetFileName(argv[2]);
writer->SetInput( varimage );
writer->Update();
writer->Write();
return 0;
}
int main(int argc, char *argv[])
{
if( argc < 3 )
{
std::cout << "Usage: " << argv[0] << " Dimension infile.hdr outfile.nii OriginX OriginY {OriginZ} "
<< std::endl;
return 1;
}
// Get the image dimension
switch( atoi(argv[1]) )
{
case 2:
{
SetOrigin<2>(argc - 1, argv + 1);
}
break;
case 3:
{
SetOrigin<3>(argc - 1, argv + 1);
}
break;
default:
std::cerr << "Unsupported dimension" << std::endl;
exit( EXIT_FAILURE );
}
return 0;
}