7 #include "OverlapNormalization.h"
11 #include "BasisFunction.h"
12 #include "IException.h"
13 #include "LeastSquares.h"
14 #include "Statistics.h"
29 OverlapNormalization::OverlapNormalization(std::vector<Statistics *> statsList) {
30 m_gainFunction = NULL;
32 m_offsetFunction = NULL;
35 m_statsList = statsList;
37 statsList.size(), statsList.size());
40 statsList.size(), statsList.size());
43 m_gains.resize(statsList.size());
44 m_offsets.resize(statsList.size());
45 for (
unsigned int i = 0; i < statsList.size(); i++) {
56 OverlapNormalization::~OverlapNormalization() {
57 if (m_gainFunction != NULL)
delete m_gainFunction;
58 if (m_offsetFunction != NULL)
delete m_offsetFunction;
59 if (m_gainLsq != NULL)
delete m_gainLsq;
60 if (m_offsetLsq != NULL)
delete m_offsetLsq;
61 for (
unsigned int i = 0; i < m_statsList.size(); i++)
delete m_statsList[i];
91 const Statistics &area1,
const unsigned index1,
92 const Statistics &area2,
const unsigned index2,
94 if (index1 >= m_statsList.size()) {
95 string msg =
"The index 1 is outside the bounds of the list.";
96 throw IException(IException::Programmer, msg, _FILEINFO_);
98 if (index2 >= m_statsList.size()) {
99 string msg =
"The index 2 is outside the bounds of the list.";
100 throw IException(IException::Programmer, msg, _FILEINFO_);
110 string msg =
"All weights must be positive real numbers.";
111 throw IException(IException::Programmer, msg, _FILEINFO_);
124 if (avg1 == 0 || avg2 == 0)
return NoContrast;
126 m_overlapList.push_back(o);
127 m_deltas.push_back(avg2 - avg1);
128 m_weights.push_back(weight);
150 if (m_overlapList.size() == 0) {
151 string msg =
"None of the input images overlap";
152 throw IException(IException::User, msg, _FILEINFO_);
158 if (m_overlapList.size() + m_idHoldList.size() < m_statsList.size()) {
159 string msg =
"Unable to normalize overlaps. The number of overlaps and "
160 "holds must be greater than the number of input images";
161 throw IException(IException::User, msg, _FILEINFO_);
164 if ( method == LeastSquares::SPARSE ) {
169 int sparseMatrixRows = m_overlapList.size() + m_idHoldList.size();
170 int sparseMatrixCols = m_offsetFunction->Coefficients();
171 m_offsetLsq =
new LeastSquares(*m_offsetFunction,
true, sparseMatrixRows, sparseMatrixCols,
true);
172 sparseMatrixCols = m_gainFunction->Coefficients();
173 m_gainLsq =
new LeastSquares(*m_gainFunction,
true, sparseMatrixRows, sparseMatrixCols,
true);
174 const std::vector<double> alphaWeight(sparseMatrixCols, 1/1000.0);
175 m_offsetLsq->SetParameterWeights( alphaWeight );
176 m_gainLsq->SetParameterWeights( alphaWeight );
180 if (type != Gains && type != GainsWithoutNormalization) {
182 for (
int overlap = 0; overlap < (int)m_overlapList.size(); overlap++) {
183 Overlap curOverlap = m_overlapList[overlap];
184 int id1 = curOverlap.
index1;
185 int id2 = curOverlap.
index2;
187 vector<double> input;
188 input.resize(m_statsList.size());
189 for (
int i = 0; i < (int)input.size(); i++) input[i] = 0.0;
193 m_offsetLsq->AddKnown(input, m_deltas[overlap], m_weights[overlap]);
197 for (
int h = 0; h < (int)m_idHoldList.size(); h++) {
198 int hold = m_idHoldList[h];
200 vector<double> input;
201 input.resize(m_statsList.size());
202 for (
int i = 0; i < (int)input.size(); i++) input[i] = 0.0;
204 m_offsetLsq->AddKnown(input, 0.0, 1e30);
209 m_offsets.resize(m_statsList.size());
210 m_offsetLsq->Solve(method);
211 for (
int i = 0; i < m_offsetFunction->Coefficients(); i++) {
212 m_offsets[i] = m_offsetFunction->Coefficient(i);
217 if (type != Offsets) {
219 for (
int overlap = 0; overlap < (int)m_overlapList.size(); overlap++) {
220 Overlap curOverlap = m_overlapList[overlap];
221 int id1 = curOverlap.
index1;
222 int id2 = curOverlap.
index2;
224 vector<double> input;
225 input.resize(m_statsList.size());
226 for (
int i = 0; i < (int)input.size(); i++) input[i] = 0.0;
232 if (type != GainsWithoutNormalization) {
251 m_gainLsq->AddKnown(input, log(tanp), m_weights[overlap]);
254 m_gainLsq->AddKnown(input, 0.0, 1e10);
259 for (
int h = 0; h < (int)m_idHoldList.size(); h++) {
260 int hold = m_idHoldList[h];
262 vector<double> input;
263 input.resize(m_statsList.size());
264 for (
int i = 0; i < (int)input.size(); i++) input[i] = 0.0;
266 m_gainLsq->AddKnown(input, 0.0, 1e10);
271 m_gains.resize(m_statsList.size());
272 m_gainLsq->Solve(method);
273 for (
int i = 0; i < m_gainFunction->Coefficients(); i++) {
274 m_gains[i] = exp(m_gainFunction->Coefficient(i));
293 double OverlapNormalization::Average(
const unsigned index)
const {
294 if (index >= m_statsList.size()) {
295 string msg =
"The index was out of bounds for the list of statistics.";
296 throw IException(IException::Programmer, msg, _FILEINFO_);
299 return m_statsList[index]->Average();
314 double OverlapNormalization::Gain(
const unsigned index)
const {
315 if (index >= m_statsList.size()) {
316 string msg =
"The index was out of bounds for the list of statistics.";
317 throw IException(IException::Programmer, msg, _FILEINFO_);
320 return m_gains[index];
335 double OverlapNormalization::Offset(
const unsigned index)
const {
336 if (index >= m_statsList.size()) {
337 string msg =
"The index was out of bounds for the list of statistics.";
338 throw IException(IException::Programmer, msg, _FILEINFO_);
341 return m_offsets[index];
358 double OverlapNormalization::Evaluate(
double dn,
unsigned index)
const {
360 string msg =
"The least squares equation has not been successfully ";
361 msg +=
"solved yet.";
362 throw IException(IException::Programmer, msg, _FILEINFO_);
366 return (dn -
Average(index)) * Gain(index) +
Average(index) + Offset(index);