NPL
Neurological Programs and Libraries
registration.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2014 Micah C Chambers (micahc.vt@gmail.com)
3  *
4  * NPL is free software: you can redistribute it and/or modify it under the
5  * terms of the BSD 2-Clause License available in LICENSE or at
6  * http://opensource.org/licenses/BSD-2-Clause
7  *
8  * @file registration.h Tools for registering 2D or 3D images.
9  *
10  *****************************************************************************/
11 
12 #ifndef REGISTRATION_H
13 #define REGISTRATION_H
14 
15 #include "mrimage.h"
16 #include "accessors.h"
17 #include "iterators.h"
18 #include <Eigen/Dense>
19 
20 #include <memory>
21 
22 namespace npl {
23 
52 {
53 public:
54 
64  RigidInfoComp(bool mindiff);
65 
75  int valueGrad(const Eigen::VectorXd& params, double& val,
76  Eigen::VectorXd& grad);
77 
88  int grad(const Eigen::VectorXd& params, Eigen::VectorXd& grad);
89 
98  int value(const Eigen::VectorXd& params, double& val);
99 
104 
109 
117  void setBins(size_t nbins, size_t krad);
118 
124  void setFixed(ptr<const MRImage> fixed);
125 
130  ptr<const MRImage> getFixed() { return m_fixed; };
131 
142  void setMoving(ptr<const MRImage> moving);
143 
151  ptr<const MRImage> getMoving() { return m_moving; };
152 
158  size_t nparam() { return 6; };
159 
160 private:
161 
162  ptr<const MRImage> m_fixed;
163  ptr<const MRImage> m_moving;
164  ptr<MRImage> m_dmoving;
165 
169  int m_bins;
170 
174  int m_krad;
175 
176  // for interpolating moving image, and iterating fixed
177  LinInterp3DView<double> m_move_get;
178  LinInterp3DView<double> m_dmove_get;
179  NDConstIter<double> m_fit;
180 
181  double m_center[3];
182 
183  NDArrayStore<1, double> m_pdfmove;
184  NDArrayStore<1, double> m_pdffix;
185 
186  NDArrayStore<2, double> m_pdfjoint;
187  NDArrayStore<3, double> m_dpdfjoint;
188  NDArrayStore<2, double> m_dpdfmove;
189 
190  vector<double> m_gradHjoint;
191  vector<double> m_gradHmove;
192 
193  double m_Hfix;
194  double m_Hmove;
195  double m_Hjoint;
196 
197  double m_rangemove[2];
198  double m_rangefix[2];
199  double m_wmove;
200  double m_wfix;
201 };
202 
214 {
215 public:
216 
228  RigidCorrComp(bool mindiff);
229 
239  int valueGrad(const Eigen::VectorXd& params, double& val,
240  Eigen::VectorXd& grad);
241 
252  int grad(const Eigen::VectorXd& params, Eigen::VectorXd& grad);
253 
262  int value(const Eigen::VectorXd& params, double& val);
263 
269  void setFixed(ptr<const MRImage> fixed);
270 
275  ptr<const MRImage> getFixed() { return m_fixed; };
276 
287  void setMoving(ptr<const MRImage> moving);
288 
296  ptr<const MRImage> getMoving() { return m_moving; };
297 
303  size_t nparam() { return 6; };
304 
309  bool m_compdiff;
310 private:
311 
312  ptr<const MRImage> m_fixed;
313  ptr<const MRImage> m_moving;
314  ptr<MRImage> m_dmoving;
315 
316  double m_center[3];
317 
318 };
319 
331 {
332 public:
333 
340  ProbDistCorrInfoComp(bool mindiff);
341 
351  int valueGrad(const Eigen::VectorXd& params, double& val,
352  Eigen::VectorXd& grad);
353 
364  int grad(const Eigen::VectorXd& params, Eigen::VectorXd& grad);
365 
374  int value(const Eigen::VectorXd& params, double& val);
375 
380 
384  double m_tps_reg;
385 
389  double m_jac_reg;
390 
395 
401  size_t nparam() { return m_deform ? m_deform->elements() : 0; };
402 
421  size_t m_krad, size_t nbins, double space, int dir);
422 
427  ptr<const MRImage> getFixed() { return m_fixed; };
428 
436  ptr<const MRImage> getMoving() { return m_moving; };
437 
443  ptr<MRImage> getDeform() { return m_deform; };
444 
445 private:
452  int metric(double& val, VectorXd& grad);
453 
459  int metric(double& val);
460 
461  /* Variables:
462  *
463  * m_bins, m_krad
464  *
465  */
466 
470  int m_dir;
471 
472  ptr<const MRImage> m_fixed;
473  ptr<const MRImage> m_moving;
474  ptr<MRImage> m_dmoving;
475  ptr<MRImage> m_deform;
476 
477  ptr<MRImage> m_fixed_cache;
478 
482  void updateCaches();
483 
487  int m_bins;
488 
492  int m_krad;
493 
497  double m_knotspace;
498 
499  // for interpolating moving image, and iterating fixed
500  VectorXd gradbuff;
501 
505  NDArrayStore<1, double> m_pdffix;
506 
510  NDArrayStore<1, double> m_pdfmove;
511 
515  NDArrayStore<2, double> m_pdfjoint;
516 
521  MRImageStore<5, double> m_dpdfjoint;
522 
527  MRImageStore<4, double> m_dpdfmove;
528 
533  MRImageStore<3, double> m_gradHjoint;
534 
539  MRImageStore<3, double> m_gradHmove;
540 
544  double m_Hfix;
545 
549  double m_Hmove;
550 
554  double m_Hjoint;
555 
559  double m_rangefix[2];
560 
565  double m_wfix;
566 };
567 
568 
579 {
580 public:
581 
588  DistCorrInfoComp(bool mindiff);
589 
599  int valueGrad(const Eigen::VectorXd& params, double& val,
600  Eigen::VectorXd& grad);
601 
612  int grad(const Eigen::VectorXd& params, Eigen::VectorXd& grad);
613 
622  int value(const Eigen::VectorXd& params, double& val);
623 
627  int m_dir;
628 
633 
637  double m_tps_reg;
638 
642  double m_jac_reg;
643 
648 
654  size_t nparam() { return m_deform ? m_deform->elements() : 0; };
655 
663  void setBins(size_t nbins, size_t krad);
664 
671  void initializeKnots(double space);
672 
678  void setFixed(ptr<const MRImage> fixed);
679 
684  ptr<const MRImage> getFixed() { return m_fixed; };
685 
697  void setMoving(ptr<const MRImage> moving, int dir = -1);
698 
706  ptr<const MRImage> getMoving() { return m_moving; };
707 
713  ptr<MRImage> getDeform() { return m_deform; };
714 
715 private:
722  int metric(double& val, VectorXd& grad);
723 
729  int metric(double& val);
730 
731  /* Variables:
732  *
733  * m_bins, m_krad
734  *
735  */
736 
737  ptr<const MRImage> m_fixed;
738  ptr<const MRImage> m_moving;
739  ptr<MRImage> m_dmoving;
740  ptr<MRImage> m_deform;
741 
742  ptr<MRImage> m_move_cache;
743  ptr<MRImage> m_dmove_cache;
744  ptr<MRImage> m_corr_cache;
745 
749  void updateCaches();
750 
754  int m_bins;
755 
759  int m_krad;
760 
764  double m_knotspace;
765 
766  // for interpolating moving image, and iterating fixed
767  VectorXd gradbuff;
768 
772  NDArrayStore<1, double> m_pdfmove;
773 
777  NDArrayStore<1, double> m_pdffix;
778 
782  NDArrayStore<2, double> m_pdfjoint;
783 
788  MRImageStore<5, double> m_dpdfjoint;
789 
794  MRImageStore<4, double> m_dpdfmove;
795 
800  MRImageStore<3, double> m_gradHjoint;
801 
806  MRImageStore<3, double> m_gradHmove;
807 
811  double m_Hfix;
812 
816  double m_Hmove;
817 
821  double m_Hjoint;
822 
826  double m_rangemove[2];
827 
831  double m_rangefix[2];
832 
837  double m_wmove;
838 
843  double m_wfix;
844 };
845 
846 
856 {
857  Vector3d rotation; //Rx, Ry, Rz
858  Vector3d shift;
859  Vector3d center;
860 
865  bool ras_coord;
866 
868  ras_coord = true;
869  rotation.setZero();
870  shift.setZero();
871  center.setZero();
872  };
873 
889  void invert();
890 
896  Matrix3d rotMatrix();
897 
903  void setRotation(const Matrix3d& rot);
904 
926 
950  void toIndexCoords(ptr<const MRImage> in, bool forcegridcenter);
951 
952 };
953 
965 ptr<MRImage> motionCorrect(ptr<const MRImage> input, size_t ref);
966 
980 Rigid3DTrans corReg3D(ptr<const MRImage> fixed, ptr<const MRImage> moving,
981  const std::vector<double>& sigmas);
982 
999 Rigid3DTrans informationReg3D(ptr<const MRImage> fixed,
1000  ptr<const MRImage> moving, const std::vector<double>& sigmas,
1001  size_t nbins = 128, size_t binradius = 4, std::string metric = "MI",
1002  double stopx = 0.001);
1003 
1028 ptr<MRImage> infoDistCor(ptr<const MRImage> fixed, ptr<const MRImage> moving,
1029  bool otsu, int dir, double bspace, double jac, double tps,
1030  const std::vector<double>& sigmas,
1031  size_t nbins = 128, size_t binradius = 4, string metric = "MI",
1032  size_t hist = 8, double stopx = 1e-5, double beta = 0.5);
1033 
1045 int cor3DDerivTest(double step, double tol, ptr<const MRImage> in1,
1046  ptr<const MRImage> in2);
1047 
1059 int information3DDerivTest(double step, double tol,
1060  ptr<const MRImage> in1, ptr<const MRImage> in2);
1061 
1075 int distcorDerivTest(double step, double tol,
1076  shared_ptr<const MRImage> in1, shared_ptr<const MRImage> in2,
1077  double regj = 0, double regt = 0);
1078 
1087 inline
1088 std::ostream& operator<< (std::ostream& stream, const Rigid3DTrans& rigid)
1089 {
1090  stream << "Rigid3DTrans ";
1091  if(rigid.ras_coord)
1092  stream << "(In RAS)\n";
1093  else
1094  stream << "(In Index)\n";
1095 
1096  stream << "Rotation: ";
1097  for(size_t ii=0; ii<2; ii++)
1098  stream << rigid.rotation[ii] << ", ";
1099  stream << rigid.rotation[2] << "\n";
1100 
1101  stream << "Center: ";
1102  for(size_t ii=0; ii<2; ii++)
1103  stream << rigid.center[ii] << ", ";
1104  stream << rigid.center[2] << "\n";
1105 
1106  stream << "Shift : ";
1107  for(size_t ii=0; ii<2; ii++)
1108  stream << rigid.shift[ii] << ", ";
1109  stream << rigid.shift[2] << "\n";
1110 
1111  return stream;
1112 }
1113 
1116 } // npl
1117 #endif //REGISTRATION_H
1118 
1119 
size_t nparam()
Returns the number of parameters that are being estimated.
Definition: registration.h:158
bool ras_coord
Indicates the stored transform is relative to physical coordintes rather than index coordinates...
Definition: registration.h:865
double m_tps_reg
Thin-plate spline regularization weight.
Definition: registration.h:637
The distortion correction MI Computer is used to compute the mutual information and gradient of mutua...
Definition: registration.h:578
RigidInfoComp(bool mindiff)
Constructor for the rigid correlation class. Note that rigid rotation is assumed to be about the cent...
Definition: accessors.h:29
size_t nparam()
Returns the number of parameters (knots)
Definition: registration.h:401
int value(const Eigen::VectorXd &params, double &val)
Computes the correlation.
void toIndexCoords(ptr< const MRImage > in, bool forcegridcenter)
Converts from world coordinates to index coordinates based on the orientation stored in input image...
void setRotation(const Matrix3d &rot)
Constructs rotation vector from rotation matrix.
void setFixed(ptr< const MRImage > fixed)
Set the fixed image for registration/comparison.
int grad(const Eigen::VectorXd &params, Eigen::VectorXd &grad)
Computes the gradient of the correlation. Note that this function just calls valueGrad because comput...
ptr< const MRImage > getMoving()
Return the current moving image.
Definition: registration.h:151
ptr< const MRImage > getFixed()
Return the current fixed image.
Definition: registration.h:684
void toRASCoords(ptr< const MRImage > in)
Converts to world coordinates based on the orientation stored in input image.
bool m_compdiff
Compute the difference of images (negate MI and NMI)
Definition: registration.h:379
int value(const Eigen::VectorXd &params, double &val)
Computes the correlation.
void setFixed(ptr< const MRImage > fixed)
Set the fixed image for registration/comparison.
int grad(const Eigen::VectorXd &params, Eigen::VectorXd &grad)
Computes the gradient of the correlation. Note that this function just calls valueGrad because comput...
bool m_compdiff
Compute the difference of images (negate MI and NMI)
Definition: registration.h:103
Metric m_metric
Metric to use.
Definition: registration.h:647
ptr< const MRImage > getMoving()
Return the current moving image.
Definition: registration.h:706
int grad(const Eigen::VectorXd &params, Eigen::VectorXd &grad)
Computes the gradient of the correlation. Note that this function just calls valueGrad because comput...
ptr< const MRImage > getFixed()
Return the current fixed image.
Definition: registration.h:275
Metric m_metric
Metric to use.
Definition: registration.h:394
int value(const Eigen::VectorXd &params, double &val)
Computes the correlation.
The Rigid Corr Computer is used to compute the correlation and gradient of correlation between two im...
Definition: registration.h:213
int value(const Eigen::VectorXd &params, double &val)
Computes the correlation.
Metric m_metric
Metric to use.
Definition: registration.h:108
Matrix3d rotMatrix()
Constructs and returns rotation Matrix.
The Rigid MI Computer is used to compute the mutual information and gradient of mutual information be...
Definition: registration.h:51
void setBins(size_t nbins, size_t krad)
Reallocates histograms and if m_fixed has been set, regenerates histogram estimate of fixed pdf...
ptr< MRImage > motionCorrect(ptr< const MRImage > input, size_t ref)
Performs motion correction on a set of volumes. Each 3D volume is extracted and linearly registered w...
void setMoving(ptr< const MRImage > moving, int dir=-1)
Set the moving image for comparison, note that setting this triggers a derivative computation and so ...
double m_jac_reg
Jacobian regularization weight.
Definition: registration.h:642
std::ostream & operator<<(std::ostream &os, const Matrix< D1, D2 > &b)
int valueGrad(const Eigen::VectorXd &params, double &val, Eigen::VectorXd &grad)
Computes the gradient and value of the correlation.
int grad(const Eigen::VectorXd &params, Eigen::VectorXd &grad)
Computes the gradient of the correlation. Note that this function just calls valueGrad because comput...
ptr< const MRImage > getMoving()
Return the current moving image.
Definition: registration.h:296
ptr< const MRImage > getMoving()
Return the current moving image.
Definition: registration.h:436
ProbDistCorrInfoComp(bool mindiff)
Constructor for the distortion correction class.
ptr< MRImage > getDeform()
Returns the current deformation.
Definition: registration.h:713
void setFixed(ptr< const MRImage > fixed)
Set the fixed image for registration/comparison.
Rigid3DTrans informationReg3D(ptr< const MRImage > fixed, ptr< const MRImage > moving, const std::vector< double > &sigmas, size_t nbins=128, size_t binradius=4, std::string metric="MI", double stopx=0.001)
Performs information-based registration between two 3D volumes. note that the two volumes should have...
bool m_compdiff
Negative of correlation (which will make it work with most optimizers)
Definition: registration.h:303
int valueGrad(const Eigen::VectorXd &params, double &val, Eigen::VectorXd &grad)
Computes the gradient and value of the correlation.
void setMoving(ptr< const MRImage > moving)
Set the moving image for comparison, note that setting this triggers a derivative computation and so ...
ptr< const MRImage > getFixed()
Return the current fixed image.
Definition: registration.h:130
ptr< const MRImage > getFixed()
Return the current fixed image.
Definition: registration.h:427
std::shared_ptr< T > ptr
Make the shared_ptr name shorter...
Definition: npltypes.h:42
ptr< MRImage > getDeform()
Returns the current deformation.
Definition: registration.h:443
RigidCorrComp(bool mindiff)
Constructor for the rigid correlation class. Note that rigid rotation is assumed to be about the cent...
double m_jac_reg
Jacobian regularization weight.
Definition: registration.h:389
size_t nparam()
Returns the number of parameters (knots)
Definition: registration.h:654
int cor3DDerivTest(double step, double tol, ptr< const MRImage > in1, ptr< const MRImage > in2)
This function checks the validity of the derivative functions used to optimize between-image corrlati...
double m_tps_reg
Thin-plate spline regularization weight.
Definition: registration.h:384
void setMoving(ptr< const MRImage > moving)
Set the moving image for comparison, note that setting this triggers a derivative computation and so ...
void initialize(ptr< const MRImage > fixed, ptr< const MRImage > moving, size_t m_krad, size_t nbins, double space, int dir)
Initializes the computer. This is much simipler to keep track of properly than several initialize fun...
int m_dir
Phase encode (distortion) dimensions.
Definition: registration.h:627
void invert()
Inverts rigid transform, where:
int valueGrad(const Eigen::VectorXd &params, double &val, Eigen::VectorXd &grad)
Computes the gradient and value of the correlation.
DistCorrInfoComp(bool mindiff)
Constructor for the distortion correction class.
bool m_compdiff
Compute the difference of images (negate MI and NMI)
Definition: registration.h:632
The distortion correction MI Computer is used to compute the mutual information and gradient of mutua...
Definition: registration.h:330
void initializeKnots(double space)
Initializes knot spacing and if m_fixed has been set, then initializes the m_deform image...
size_t nparam()
Returns the number of parameters that are being estimated.
Definition: registration.h:303
int information3DDerivTest(double step, double tol, ptr< const MRImage > in1, ptr< const MRImage > in2)
This function checks the validity of the derivative functions used to optimize between-image corrlati...
int distcorDerivTest(double step, double tol, shared_ptr< const MRImage > in1, shared_ptr< const MRImage > in2, double regj=0, double regt=0)
This function checks the validity of the derivative functions used to optimize between-image corrlati...
void setBins(size_t nbins, size_t krad)
Reallocates histograms and if m_fixed has been set, regenerates histogram estimate of fixed pdf...
int valueGrad(const Eigen::VectorXd &params, double &val, Eigen::VectorXd &grad)
Computes the gradient and value of the correlation.
Rigid3DTrans corReg3D(ptr< const MRImage > fixed, ptr< const MRImage > moving, const std::vector< double > &sigmas)
Performs correlation based registration between two 3D volumes. note that the two volumes should have...
Struct for holding information about a rigid transform. Note that rotation R = Rx*Ry*Rz, where Rx, Ry, and Rz are the rotations about x, y and z aaxes, and the angles are stored (in radians) in the rotation member.
Definition: registration.h:855
Metric
Information-based Metric to use.
Definition: npltypes.h:33
ptr< MRImage > infoDistCor(ptr< const MRImage > fixed, ptr< const MRImage > moving, bool otsu, int dir, double bspace, double jac, double tps, const std::vector< double > &sigmas, size_t nbins=128, size_t binradius=4, string metric="MI", size_t hist=8, double stopx=1e-5, double beta=0.5)
Information based registration between two 3D volumes. note that the two volumes should have identica...