1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://www.centreleonberard.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
19 /* =================================================
20 * @file clitkPointRigidRegistrationGenericFilter.txx
21 * @author xxx <xxx@creatis.insa-lyon.fr>
24 * @brief PointRigidRegistration an image
26 ===================================================*/
29 #include "clitkPointRigidRegistration_ggo.h"
32 //paste from RigidRegistration
33 #include "itkImageFileReader.h"
34 #include "itkImageFileWriter.h"
36 #include "itkVector.h"
37 #include "itkResampleImageFilter.h"
38 #include "itkLandmarkBasedTransformInitializer.h"
39 #include "itkRigid2DTransform.h"
40 #include "itkVersorRigid3DTransform.h"
44 #include "itkMatrix.h"
46 //#include "itkPoint.h"
47 #include "clitkImageCommon.h"
48 #include "clitkCommon.h"
49 //#define VTK_EXCLUDE_STRSTREAM_HEADERS
50 #include <vtkMatrix4x4.h>
51 #include <vtkSmartPointer.h>
53 //for open file for reading
55 #include "clitkImageCommon.h"
56 #include "clitkCommon.h"
59 //--------------------------------------------------------------------
60 int main(int argc, char * argv[])
64 GGO(clitkPointRigidRegistration, args_info);
72 //open stream to reading
74 clitk::openFileForReading(is, args_info.input_arg);
78 //reading first line of input file to chck thw dimension of data
80 //clitk::skipComment(is);
83 typedef unsigned char PixelType;
89 unsigned int Dimension_temp = (unsigned int)x;
93 if (Dimension_temp==2)
95 const unsigned int Dimension = 2;
96 typedef itk::Image< PixelType, Dimension > ImageType;
97 typedef float VectorComponentType;
98 typedef itk::Vector< VectorComponentType, Dimension > VectorType;
99 //Typ LandmarkBasedTransormInitializer
100 typedef itk::Rigid2DTransform< double > Rigid2DTransformType;
101 typedef itk::LandmarkBasedTransformInitializer< Rigid2DTransformType,ImageType, ImageType>
102 LandmarkBasedTransformInitializerType;
104 LandmarkBasedTransformInitializerType::Pointer landmarkBasedTransformInitializer = LandmarkBasedTransformInitializerType::New();
106 // Create source and target landmarks.
107 typedef LandmarkBasedTransformInitializerType::LandmarkPointContainer LandmarkContainerType;
108 typedef LandmarkBasedTransformInitializerType::LandmarkPointType LandmarkPointType;
110 LandmarkContainerType imageLandmarks;
111 LandmarkContainerType trackerLandmarks;
113 LandmarkPointType imagePoint;
114 LandmarkPointType trackerPoint;
119 while (is && !is.eof()) {
124 is >> trackerPoint[0];
125 is >> trackerPoint[1];
127 imageLandmarks.push_back(imagePoint );
128 trackerLandmarks.push_back(trackerPoint );
136 landmarkBasedTransformInitializer->SetFixedLandmarks( imageLandmarks);
137 landmarkBasedTransformInitializer->SetMovingLandmarks( trackerLandmarks);
139 Rigid2DTransformType::Pointer transform = Rigid2DTransformType::New();
141 transform->SetIdentity();
143 landmarkBasedTransformInitializer->SetTransform(transform);
144 landmarkBasedTransformInitializer->InitializeTransform();
146 Rigid2DTransformType::MatrixType matrix = transform->GetMatrix();
147 Rigid2DTransformType::OffsetType offset = transform->GetOffset();
151 clitk::openFileForWriting(out, args_info.output_arg);
153 out << matrix[0][0] << ' ' << matrix[0][1] << ' ' << offset[0] << std::endl;
154 out << matrix[1][0] << ' ' << matrix[1][1] << ' ' << offset[1] << std::endl;
155 out << 0.0 << ' ' << 0.0 << ' ' << 1.0;
161 if (args_info.error_arg != 0)
164 clitk::openFileForWriting(error, args_info.error_arg);
168 int nImagePoints = imageLandmarks.size();
169 double imagePoints[3][nImagePoints];
170 double trackerPoints[3][nImagePoints];
171 double imagePointstoTrackerCoordinate[3][nImagePoints];
173 double PointEuclideanDistance = 0;
174 double SumofPointsEuclideanDistance = 0;
177 for(int n= 0; n < imageLandmarks.size(); n++)
179 for(int j= 0; j < 2; j++)
181 imagePoints[j][n] = imageLandmarks[n][j];
182 trackerPoints[j][n] = trackerLandmarks[n][j];
187 // imagePoints[j][n] = trackerLandmarks[n][j];
188 // trackerPoints[j][n] = imageLandmarks[n][j];
189 imagePointstoTrackerCoordinate[j][n] = 0;
192 imagePoints[2][n] = 1.0;
193 trackerPoints[2][n] = 1.0;
194 imagePointstoTrackerCoordinate[2][n] = 0;
198 //transformation matrix
199 double RigidTransformationImagetoTracker[4][4];
202 for(int i = 0; i < 2; i++)
203 for(int j = 0; j < 2; j++)
204 RigidTransformationImagetoTracker[i][j] = matrix[i][j];
207 for(int i = 0; i < 2; i++)
208 RigidTransformationImagetoTracker[i][2] = offset[i];
210 RigidTransformationImagetoTracker[2][0] = 0.0;
211 RigidTransformationImagetoTracker[2][1] = 0.0;
212 RigidTransformationImagetoTracker[2][2] = 1.0;
216 error << "ImagePoints: CorrespondingTrackerPoints: ImagePointstoTrackerCoordinateSystem: PointsDistanceafterRigidRegistration:" << std::endl;
224 for(int n= 0; n < imageLandmarks.size(); n++)
228 //Calculate imagePointstoTrackerCoordinate
229 for(int i = 0; i < 3; i++)
232 for(int k = 0; k < 3; k++)
234 imagePointstoTrackerCoordinate[i][n] = imagePointstoTrackerCoordinate[i][n] + RigidTransformationImagetoTracker[i][k]*imagePoints[k][n];
243 for(int i = 0; i < 2; i++)
244 error << imagePoints[i][n] << ' ';
248 for(int i = 0; i < 2; i++)
249 error << trackerPoints[i][n] << ' ';
253 for(int i = 0; i < 2; i++)
254 error << imagePointstoTrackerCoordinate[i][n] << ' ';
258 //calculate PointEuclideanDistance
259 PointEuclideanDistance = 0;
260 for(int i=0; i < 2;i++)
262 PointEuclideanDistance = PointEuclideanDistance + std::pow((trackerPoints[i][n] - imagePointstoTrackerCoordinate[i][n]),2);
264 PointEuclideanDistance = std::sqrt(PointEuclideanDistance);
265 error << " " << PointEuclideanDistance << " ";
267 SumofPointsEuclideanDistance = SumofPointsEuclideanDistance + std::pow(PointEuclideanDistance,2);
280 } //end loop via points
283 SumofPointsEuclideanDistance = std::sqrt(SumofPointsEuclideanDistance/nImagePoints);
284 error << "Fiducial Registration Error: " << SumofPointsEuclideanDistance;
287 } //end if (args_info.error_arg != null)
288 } //end Dimension == 2 case
289 else if (Dimension_temp==3)
292 const unsigned int Dimension = 3;
293 typedef itk::Image< PixelType, Dimension > ImageType;
294 typedef float VectorComponentType;
295 typedef itk::Vector< VectorComponentType, Dimension > VectorType;
296 //Typ LandmarkBasedTransormInitializer
297 typedef itk::VersorRigid3DTransform< double > Rigid3DTransformType;
298 typedef itk::LandmarkBasedTransformInitializer< Rigid3DTransformType,ImageType, ImageType>
299 LandmarkBasedTransformInitializerType;
301 LandmarkBasedTransformInitializerType::Pointer landmarkBasedTransformInitializer = LandmarkBasedTransformInitializerType::New();
303 // Create source and target landmarks.
304 typedef LandmarkBasedTransformInitializerType::LandmarkPointContainer LandmarkContainerType;
305 typedef LandmarkBasedTransformInitializerType::LandmarkPointType LandmarkPointType;
307 LandmarkContainerType imageLandmarks;
308 LandmarkContainerType trackerLandmarks;
309 LandmarkContainerType imageLandmarksTotrackerCoordinate;
312 LandmarkPointType imagePoint;
313 LandmarkPointType trackerPoint;
317 while (is && !is.eof()) {
322 is >> trackerPoint[0];
323 is >> trackerPoint[1];
324 is >> trackerPoint[2];
326 imageLandmarks.push_back(imagePoint );
327 trackerLandmarks.push_back(trackerPoint );
334 landmarkBasedTransformInitializer->SetFixedLandmarks( imageLandmarks);
335 landmarkBasedTransformInitializer->SetMovingLandmarks( trackerLandmarks);
337 Rigid3DTransformType::Pointer transform = Rigid3DTransformType::New();
339 transform->SetIdentity();
341 landmarkBasedTransformInitializer->SetTransform(transform);
342 landmarkBasedTransformInitializer->InitializeTransform();
344 Rigid3DTransformType::MatrixType matrix = transform->GetMatrix();
345 Rigid3DTransformType::OffsetType offset = transform->GetOffset();
349 clitk::openFileForWriting(out, args_info.output_arg);
351 out << matrix[0][0] << ' ' << matrix[0][1] << ' ' << matrix[0][2] << ' '<< offset[0] << std::endl;
352 out << matrix[1][0] << ' ' << matrix[1][1] << ' ' << matrix[1][2] << ' '<< offset[1] << std::endl;
353 out << matrix[2][0] << ' ' << matrix[2][1] << ' ' << matrix[2][2] << ' '<< offset[2] << std::endl;
354 out << 0.0 << ' ' << 0.0 << ' ' << 0.0 << ' ' << 1.0;
358 if (args_info.error_arg != 0)
362 clitk::openFileForWriting(error, args_info.error_arg);
366 int nImagePoints = imageLandmarks.size();
367 double imagePoints[4][nImagePoints];
368 double trackerPoints[4][nImagePoints];
369 double imagePointstoTrackerCoordinate[4][nImagePoints];
371 double PointEuclideanDistance = 0;
372 double SumofPointsEuclideanDistance = 0;
375 for(int n= 0; n < imageLandmarks.size(); n++)
377 for(int j= 0; j < 3; j++)
379 imagePoints[j][n] = imageLandmarks[n][j];
380 trackerPoints[j][n] = trackerLandmarks[n][j];
385 // imagePoints[j][n] = trackerLandmarks[n][j];
386 // trackerPoints[j][n] = imageLandmarks[n][j];
387 imagePointstoTrackerCoordinate[j][n] = 0;
390 imagePoints[3][n] = 1.0;
391 trackerPoints[3][n] = 1.0;
392 imagePointstoTrackerCoordinate[3][n] = 0;
396 //transformation matrix
397 double RigidTransformationImagetoTracker[4][4];
400 for(int i = 0; i < 3; i++)
401 for(int j = 0; j < 3; j++)
402 RigidTransformationImagetoTracker[i][j] = matrix[i][j];
405 for(int i = 0; i < 3; i++)
406 RigidTransformationImagetoTracker[i][3] = offset[i];
408 RigidTransformationImagetoTracker[3][0] = 0.0;
409 RigidTransformationImagetoTracker[3][1] = 0.0;
410 RigidTransformationImagetoTracker[3][2] = 0.0;
411 RigidTransformationImagetoTracker[3][3] = 1.0;
414 error << "ImagePoints: CorrespondingTrackerPoints: ImagePointstoTrackerCoordinateSystem: PointsDistanceafterRigidRegistration:" << std::endl;
422 for(int n= 0; n < imageLandmarks.size(); n++)
426 //Calculate imagePointstoTrackerCoordinate
427 for(int i = 0; i < 4; i++)
430 for(int k = 0; k < 4; k++)
432 imagePointstoTrackerCoordinate[i][n] = imagePointstoTrackerCoordinate[i][n] + RigidTransformationImagetoTracker[i][k]*imagePoints[k][n];
441 for(int i = 0; i < 3; i++)
442 error << imagePoints[i][n] << ' ';
446 for(int i = 0; i < 3; i++)
447 error << trackerPoints[i][n] << ' ';
451 for(int i = 0; i < 3; i++)
452 error << imagePointstoTrackerCoordinate[i][n] << ' ';
456 //calculate PointEuclideanDistance
457 PointEuclideanDistance = 0;
458 for(int i=0; i < 3;i++)
460 PointEuclideanDistance = PointEuclideanDistance + std::pow((trackerPoints[i][n] - imagePointstoTrackerCoordinate[i][n]),2);
462 PointEuclideanDistance = std::sqrt(PointEuclideanDistance);
463 error << " " << PointEuclideanDistance << " ";
465 SumofPointsEuclideanDistance = SumofPointsEuclideanDistance + std::pow(PointEuclideanDistance,2);
478 } //end loop via points
481 SumofPointsEuclideanDistance = std::sqrt(SumofPointsEuclideanDistance/nImagePoints);
482 error << "Fiducial Registration Error: " << SumofPointsEuclideanDistance;
486 } //end if (args_info.error_arg != null)
487 } //end Dimension == 3 case
503 //--------------------------------------------------------------------