Debian Codegraph @

apps/fit_affine_xform_landmarks.cxx

raw symbols SCIP
1 /*
2 //
3 // Copyright 1997-2011 Torsten Rohlfing
4 //
5 // Copyright 2004-2014 SRI International
6 //
7 // This file is part of the Computational Morphometry Toolkit.
8 //
9 // http://www.nitrc.org/projects/cmtk/
10 //
11 // The Computational Morphometry Toolkit is free software: you can
12 // redistribute it and/or modify it under the terms of the GNU General Public
13 // License as published by the Free Software Foundation, either version 3 of
14 // the License, or (at your option) any later version.
15 //
16 // The Computational Morphometry Toolkit is distributed in the hope that it
17 // will be useful, but WITHOUT ANY WARRANTY; without even the implied
18 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License along
22 // with the Computational Morphometry Toolkit. If not, see
23 // <http://www.gnu.org/licenses/>.
24 //
25 // $Revision: 5436 $
26 //
27 // $LastChangedDate: 2018-12-10 19:01:20 -0800 (Mon, 10 Dec 2018) $
28 //
29 // $LastChangedBy: torstenrohlfing $
30 //
31 */
32
33 #include <cmtkconfig.h>
34
35 #include <System/cmtkConsole.h>
36 #include <System/cmtkDebugOutput.h>
37 #include <System/cmtkCommandLine.h>
38 #include <System/cmtkProgress.h>
39 #include <System/cmtkExitException.h>
40
41 #include <Base/cmtkAffineXform.h>
42 #include <Base/cmtkLandmarkPairList.h>
43 #include <Base/cmtkFitAffineToLandmarks.h>
44 #include <Base/cmtkFitRigidToLandmarks.h>
45
46 #include <IO/cmtkLandmarkListIO.h>
47 #include <IO/cmtkXformIO.h>
48
49 #include <fstream>
50
51 int
52 doMain ( const int argc, const char *argv[] )
53 {
54 std::string lmSourcePath;
55 std::string lmTargetPath;
56 std::string outputPath;
57
58 bool rigid = false;
59
60 try
61 {
62 cmtk::CommandLine cl;
63 cl.SetProgramInfo( cmtk::CommandLine::PRG_TITLE, "Fit Affine Transformation to Landmarks" );
64 cl.SetProgramInfo( cmtk::CommandLine::PRG_DESCR, "Fit a linear affine transformation to a set of matched landmarks." );
65
66 typedef cmtk::CommandLine::Key Key;
67 cl.AddParameter( &lmSourcePath, "SourcePath", "Path to file with source-space landmarks." );
68 cl.AddParameter( &lmTargetPath, "TargetPath", "Path to file with target-space landmarks." );
69
70 cl.AddSwitch( Key( "rigid" ), &rigid, true, "Fit only a rigid, 6 degree-of-freedom transformation rather than full affine. This requires fewer landmarks and makes less trict assumptions about their location "
71 "(i.e., three non-collinear landmarks are sufficient to obtain a rigid transformation." );
72
73 cl.AddParameter( &outputPath, "OutputXform", "Path for output fitted affine transformation." )->SetProperties( cmtk::CommandLine::PROPS_XFORM | cmtk::CommandLine::PROPS_OUTPUT );
74
75 cl.Parse( argc, argv );
76 }
77 catch ( const cmtk::CommandLine::Exception& e )
78 {
79 cmtk::StdErr << e;
80 throw cmtk::ExitException( 1 );
81 }
82
83 std::ifstream lmSourceStream( lmSourcePath.c_str() );
84 if ( !lmSourceStream.good() )
85 {
86 cmtk::StdErr << "ERROR: could not open source landmark file " << lmSourcePath << "\n";
87 throw cmtk::ExitException( 1 );
88 }
89
90 cmtk::LandmarkList sourceLandmarks;
91 lmSourceStream >> sourceLandmarks;
92
93 cmtk::DebugOutput( 5 ) << "Read " << sourceLandmarks.size() << " source landmarks\n";
94
95 std::ifstream lmTargetStream( lmTargetPath.c_str() );
96 if ( !lmTargetStream.good() )
97 {
98 cmtk::StdErr << "ERROR: could not open target landmark file " << lmTargetPath << "\n";
99 throw cmtk::ExitException( 1 );
100 }
101
102 cmtk::LandmarkList targetLandmarks;
103 lmTargetStream >> targetLandmarks;
104
105 cmtk::DebugOutput( 5 ) << "Read " << targetLandmarks.size() << " target landmarks\n";
106
107 cmtk::LandmarkPairList landmarkPairs( sourceLandmarks, targetLandmarks );
108 cmtk::DebugOutput( 5 ) << "Matched " << landmarkPairs.size() << " landmark pairs\n";
109
110 cmtk::AffineXform::SmartConstPtr xform;
111 try
112 {
113 xform = cmtk::AffineXform::SmartConstPtr( rigid ? cmtk::FitRigidToLandmarks( landmarkPairs ).GetRigidXform() : cmtk::FitAffineToLandmarks( landmarkPairs ).GetAffineXform() );
114 }
115 catch ( const cmtk::AffineXform::MatrixType::SingularMatrixException& )
116 {
117 cmtk::StdErr << "ERROR: singular matrix encountered in cmtk::FitRigidToLandmarks or cmtk::FitAffineToLandmarks\n";
118 throw cmtk::ExitException( 1 );
119 }
120
121 cmtk::XformIO::Write( xform, outputPath );
122
123 if ( cmtk::DebugOutput::GetGlobalLevel() >= 5 )
124 {
125 cmtk::DebugOutput( 5 ) << "INFO: RMS fitting residual = " << sqrt( xform->GetLandmarksMSD( landmarkPairs ) ) << "\n";
126 }
127
128 return 0;
129 }
130
131 #include "cmtkSafeMain"