-
Notifications
You must be signed in to change notification settings - Fork 2
/
bisLinearRPMRegistration.cpp
executable file
·175 lines (140 loc) · 6.81 KB
/
bisLinearRPMRegistration.cpp
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* License
_This file is Copyright 2018 by the Image Processing and Analysis Group (BioImage Suite Team). Dept. of Radiology & Biomedical Imaging, Yale School of Medicine._ It is released under the terms of the GPL v2.
----
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
See also http: www.gnu.org/licenses/gpl.html
If this software is modified please retain this statement and add a notice
that it had been modified (and by whom).
Endlicense */
#include "bisLinearRPMRegistration.h"
#include "bisUtil.h"
bisLinearRPMRegistration::bisLinearRPMRegistration(std::string n) : bisRPMCorrespondenceFinder(n) {
this->Output=new bisMatrixTransformation();
}
bisLinearRPMRegistration::~bisLinearRPMRegistration()
{
if (this->Output)
delete this->Output;
}
// Description:
// get Output Transformation
bisSimpleMatrix<float>* bisLinearRPMRegistration::getOutputMatrix() {
return this->Output->getSimpleMatrix("lin_reg_matrix");
}
/** run transformation
* @param transformMode (0=rigid,1=similarity,2=affine)
* @param initialTemperature initial temperature
* @param finalTemperature final temperature
* @param annealRate anneal rate for RPM
* @param useCentroids if true center points first
* @param initialTransformaiton use this to initialize the mappings
* @param debug if true print extra messages
* @returns 1 if success, 0 if failed
*/
int bisLinearRPMRegistration::run(int in_transformMode,
int in_correspondenceMode,
float in_initialTemperature,
float in_finalTemperature,
int in_iterationPerTemperature,
float in_annealRate,
int in_useCentroids,
bisMatrixTransformation* in_initialTransformation,
int in_debug) {
if (this->locator==NULL) {
std::cerr << "___ bisLinearRPMRegistration not initialized" << std::endl;
return 0;
}
int IterationPerTemperature = bisUtil::irange(in_iterationPerTemperature,1,10);
int TransformMode=bisUtil::irange(in_transformMode,0,2);
int CorrespondenceMode=bisUtil::irange(in_correspondenceMode,0,2);
float FinalTemperature=bisUtil::frange(in_finalTemperature,0.01,1000.0);
float InitialTemperature=bisUtil::frange(in_initialTemperature,FinalTemperature,1000.0);
float AnnealRate=bisUtil::frange(in_annealRate,0.5,0.999);
int debug=in_debug;
// Initial Mapping including centroid shift!
bisUtil::mat44 m;
if (!in_initialTransformation) {
bisUtil::makeIdentityMatrix(m);
if (in_useCentroids) {
float cx[3],cy[3];
bisPointRegistrationUtils::computeCentroid(this->SampledReferencePoints.get(),cx,debug);
bisPointRegistrationUtils::computeCentroid(this->SampledTargetPoints.get(),cy,debug);
if (debug)
std::cout << "___ using initial centroid alignment :";
for (int ia=0;ia<=2;ia++) {
m[ia][3]=cy[ia]-cx[ia];
if (debug)
std::cout << m[ia][3] << " ";
}
if (debug)
std::cout << std::endl;
} else {
if (debug)
std::cout << "Not using centroids " << std::endl;
}
this->Output->setMatrix(m);
} else {
if (debug)
std::cout << "Using Initial Transformation" << std::endl;
in_initialTransformation->getMatrix(m);
this->Output->setMatrix(m);
}
bisPointRegistrationUtils::printMatrix(this->Output,"Initial Mapping");
bisSimpleMatrix<float>* OutputRefLandmarks=new bisSimpleMatrix<float>();
bisSimpleMatrix<float>* OutputTargetLandmarks=new bisSimpleMatrix<float>();
bisSimpleVector<float>* OutputWeights=new bisSimpleVector<float>();
float Temperature=InitialTemperature;
int numpoints=this->SampledReferencePoints->getNumRows();
int numpoints2=this->SampledTargetPoints->getNumRows();
if (debug) {
std::cout << "___ Beginning Linear RPM Registration tmode=" << TransformMode << " cmode=" << CorrespondenceMode << std::endl;
std::cout << "___ NumPoints= " << numpoints << " vs " << numpoints2 << " temperatures=" << InitialTemperature << ":" << AnnealRate << ":" << FinalTemperature << std::endl;
}
int iteration=0;
int totaliter=int(fabs(log(InitialTemperature/FinalTemperature)/log(AnnealRate))+1.0)*IterationPerTemperature;
int incr=int(numpoints/5);
while (Temperature > FinalTemperature)
{
for (int it=0;it<IterationPerTemperature;it++) {
iteration=iteration+1;
if (debug)
std::cout << "___ Beginning iteration " << iteration << "/" << totaliter << ". Temp=" << Temperature << " TransformMode=" << TransformMode << std::endl;
this->estimateCorrespondence(this->Output,
Temperature,
CorrespondenceMode,
OutputRefLandmarks,
OutputTargetLandmarks,
OutputWeights,
debug);
if (debug) {
bisPointRegistrationUtils::printJointPoints(OutputRefLandmarks,OutputTargetLandmarks,OutputWeights,"out_ref->targ",incr);
}
bisPointRegistrationUtils::computeLandmarkTransformation(OutputRefLandmarks,
OutputTargetLandmarks,
TransformMode,
this->Output,
OutputWeights,
0);
if (debug) {
bisPointRegistrationUtils::printMatrix(this->Output,"End");
std::cout << "__ Mean RMS=" << bisPointRegistrationUtils::computeMappingError(OutputRefLandmarks,OutputTargetLandmarks,this->Output) << std::endl;
}
}
Temperature*=AnnealRate;
// Check error
}
delete OutputRefLandmarks;
delete OutputTargetLandmarks;
delete OutputWeights;
return iteration;
}