CASToR  3.2
Tomographic Reconstruction (PET/SPECT/CT)
iPenaltyQuadratic.cc
Go to the documentation of this file.
1 
8 #include "iPenaltyQuadratic.hh"
9 
10 // =====================================================================
11 // ---------------------------------------------------------------------
12 // ---------------------------------------------------------------------
13 // =====================================================================
14 
16 {
17  // --------------------------
18  // Specific member parameters
19  // --------------------------
20  m_penaltyID = "Quadratic";
22  m_targetImagePath = "";
23  mp_targetImage = NULL;
24 }
25 
26 // =====================================================================
27 // ---------------------------------------------------------------------
28 // ---------------------------------------------------------------------
29 // =====================================================================
30 
32 {
33  // Note: there is no need to deallocate the images themselves as they are allocate using the
34  // miscellaneous image function from the image space, which automatically deals with
35  // memory deallocations.
36 }
37 
38 // =====================================================================
39 // ---------------------------------------------------------------------
40 // ---------------------------------------------------------------------
41 // =====================================================================
42 
44 {
45  cout << "This penalty is a simple quadratic error penalty, corresponding to a L2 distance between the reconstructed image" << endl;
46  cout << "and a target image." << endl;
47  cout << "N.B.: The penalty strength is scaled by 0.5." << endl;
48  cout << "The following options can be used:" << endl;
49  cout << " target image path: to set a target image to fit. By default, it is a uniform image with zero value." << endl;
50 }
51 
52 // =====================================================================
53 // ---------------------------------------------------------------------
54 // ---------------------------------------------------------------------
55 // =====================================================================
56 
57 int iPenaltyQuadratic::ReadConfigurationFile(const string& a_configurationFile)
58 {
59  string key_word = "";
60  // Read the target image path to use
61  key_word = "target image path";
62  if (ReadDataASCIIFile(a_configurationFile, key_word, &m_targetImagePath, 1, KEYWORD_MANDATORY))
63  {
64  Cerr("***** iPenaltyQuadratic::ReadConfigurationFile() -> Failed to get the '" << key_word << "' keyword !" << endl);
65  return 1;
66  }
67  // End
68  return 0;
69 }
70 
71 // =====================================================================
72 // ---------------------------------------------------------------------
73 // ---------------------------------------------------------------------
74 // =====================================================================
75 
76 int iPenaltyQuadratic::ReadOptionsList(const string& a_optionsList)
77 {
78  // Too complicated to do it that way
79  Cerr("***** iPenaltyQuadratic::ReadOptionsList() -> Options can be specified only using a configuration file !" << endl);
80  return 1;
81 }
82 
83 // =====================================================================
84 // ---------------------------------------------------------------------
85 // ---------------------------------------------------------------------
86 // =====================================================================
87 
89 {
90  // Warn user no target image was set
91  if (m_targetImagePath == "")
92  {
93  Cerr("!!!!! iPenaltyQuadratic::CheckSpecificParameters() -> No target image was provided, thus Quadratic penalty will use a full zeros target image !"<< endl);
94  }
95  // Normal end
96  return 0;
97 }
98 
99 // =====================================================================
100 // ---------------------------------------------------------------------
101 // ---------------------------------------------------------------------
102 // =====================================================================
103 
105 {
106  // Allocate the target image
107  mp_targetImage = mp_ImageSpace -> AllocateMiscellaneousImage(); // Get a pointer to a newly allocated image
108  // Initialize/Read the target image
109  if (m_targetImagePath == "") // whole phantom to be considered, so uniform image
110  {
111  for (int v=0; v<mp_ImageDimensionsAndQuantification->GetNbVoxXYZ(); v++)
112  {
113  mp_targetImage[v] = 0.;
114  }
115  }
116  else
117  {
118  // Interfile image reading (INTF_LERP_ENABLED = interpolation allowed)
120  {
121  Cerr("***** iPenaltyQuadratic::InitializeSpecific() -> Error reading interfile image '" << m_targetImagePath << "' !" << endl);
122  return 1;
123  }
124  }
125  // Verbose
127  {
128  Cout("iPenaltyQuadratic::InitializeSpecific() -> Use the Quadratic optimizer" << endl);
130  {
131  if (m_targetImagePath == "") Cout(" --> Uniform zero target image" << endl);
132  else Cout(" --> Target image from file: " << m_targetImagePath << endl);
133  }
134  }
135  // Normal end
136  return 0;
137 }
138 
139 // =====================================================================
140 // ---------------------------------------------------------------------
141 // ---------------------------------------------------------------------
142 // =====================================================================
143 
145 {
146  // Compute error between reconstructed image and target image to compute quadratic penalty term
147  FLTNB difference;
148  difference = ap_image[a_voxel] - mp_targetImage[a_voxel];
149  FLTNB penalty = 0.5 * m_penaltyStrength * difference * difference;
150  // Check for Inf, Nan, etc
151  if (fpclassify(penalty) != FP_NORMAL) penalty = 0.;
152  // Return result
153  return penalty;
154 }
155 
156 // =====================================================================
157 // ---------------------------------------------------------------------
158 // ---------------------------------------------------------------------
159 // =====================================================================
160 
162 {
163  // Compute error between reconstructed image and target image to compute first derivative
164  FLTNB difference;
165  difference = ap_image[a_voxel] - mp_targetImage[a_voxel];
166  FLTNB first_derivative = m_penaltyStrength * difference;
167  // Check for Inf, Nan, etc
168  if (fpclassify(first_derivative) != FP_NORMAL) first_derivative = 0.;
169  // Return result
170  return first_derivative;
171 }
172 
173 
174 // =====================================================================
175 // ---------------------------------------------------------------------
176 // ---------------------------------------------------------------------
177 // =====================================================================
178 
180 {
181  FLTNB second_derivative = m_penaltyStrength;
182  return second_derivative;
183 }
#define INTF_LERP_ENABLED
#define Cerr(MESSAGE)
int CheckSpecificParameters()
A private function used to check the parameters settings specific to the child penalty.
iPenaltyQuadratic()
The constructor of iPenaltyQuadratic.
int InitializeSpecific()
This function is used to initialize specific stuff to the child penalty.
FLTNB ComputePenaltyValue(FLTNB *ap_image, INTNB a_voxel, int a_th)
Implementation of the pure virtual vPenalty::ComputePenaltyValue()
int IntfReadImage(const string &a_pathToHeaderFile, FLTNB *ap_ImgMatrix, oImageDimensionsAndQuantification *ap_ID, int vb, bool a_lerpFlag)
Main function dedicated to Interfile 3D image loading.
int ReadConfigurationFile(const string &a_configurationFile)
A function used to read options from a configuration file.
Declaration of class iPenaltyQuadratic.
This class is designed to generically described any penalty applied to MAP algorithms.
#define KEYWORD_MANDATORY
FLTNB ComputeSecondDerivative(FLTNB *ap_image, INTNB a_voxel, int a_th)
Implementation of the pure virtual vPenalty::ComputeSecondDerivative()
int ReadOptionsList(const string &a_optionsList)
A function used to read options from a list of options.
FLTNB ComputeFirstDerivative(FLTNB *ap_image, INTNB a_voxel, int a_th)
Implementation of the pure virtual vPenalty::ComputeFirstDerivative()
int ReadDataASCIIFile(const string &a_file, const string &a_keyword, T *ap_return, int a_nbElts, bool a_mandatoryFlag)
Look for "a_nbElts" elts in the "a_file" file matching the "a_keyword" string passed as parameter a...
~iPenaltyQuadratic()
The destructor of iPenaltyQuadratic.
#define Cout(MESSAGE)
oImageDimensionsAndQuantification * mp_ImageDimensionsAndQuantification
void ShowHelpSpecific()
A function used to show help about the child penalty.