SourceXtractorPlusPlus  0.10
Please provide a description of the project.
utils.h
Go to the documentation of this file.
1 
23 #ifndef UTILS_H
24 #define UTILS_H
25 
26 #include <iostream>
27 #include <memory>
28 #include <utility>
29 #include <valarray>
30 #include <string>
31 #include <algorithm>
32 #include <opencv2/opencv.hpp>
33 #include <CCfits/CCfits>
37 
40 void writeToFits(const cv::Mat& image, const std::string& filename) {
41  std::valarray<double> data (image.total());
42  std::copy(image.begin<double>(), image.end<double>(), begin(data));
43  long naxis = 2;
44  long naxes[2] = {image.size[1], image.size[0]};
45  std::unique_ptr<CCfits::FITS> pFits {new CCfits::FITS("!"+filename, DOUBLE_IMG, naxis, naxes)};
46  pFits->pHDU().write(1, image.total(), data);
47  std::cout << "Created file " << filename << '\n';
48 }
49 
54  // Read the HDU from the file
55  std::unique_ptr<CCfits::FITS> pFits {new CCfits::FITS(filename)};
56  auto& image_hdu = pFits->pHDU();
57  // Get the pixel scale from the header
58  double pixel_scale = 0.;
59  image_hdu.readKey("SCALE", pixel_scale);
60  // Get the dimension of the image
61  if (image_hdu.axis(0) != image_hdu.axis(1)) {
62  throw Elements::Exception() << "Non square PSF (" << image_hdu.axis(0) << 'X'
63  << image_hdu.axis(1) << ')';
64  }
65  auto size = image_hdu.axis(0);
66  // Get the data
67  std::valarray<double> data {};
68  image_hdu.read(data);
69  cv::Mat kernel (size, size, CV_64F);
70  std::copy(begin(data), end(data), kernel.begin<double>());
71  return ModelFitting::OpenCvPsf {pixel_scale, kernel};
72 }
73 
78  // Read the HDU from the file
79  std::unique_ptr<CCfits::FITS> pFits {new CCfits::FITS(filename)};
80  auto& image_hdu = pFits->pHDU();
81  // Get the pixel scale from the header
82  double pixel_scale = 0.;
83  image_hdu.readKey("SCALE", pixel_scale);
84  // Get the dimension of the image
85  auto width = image_hdu.axis(1);
86  auto height = image_hdu.axis(0);
87  // Get the data
88  std::valarray<double> data {};
89  image_hdu.read(data);
90  cv::Mat image (width, height, CV_64F);
91  std::copy(begin(data), end(data), image.begin<double>());
92  return std::make_pair(image, pixel_scale);
93 }
94 
96  // Read the HDU from the file
97  std::unique_ptr<CCfits::FITS> pFits {new CCfits::FITS(filename)};
99  for (auto& pair : pFits->extension()) {
100  auto& image_hdu = *pair.second;
101  // Get the pixel scale from the header
102  double pixel_scale = 0.;
103  image_hdu.readKey("SCALE", pixel_scale);
104  // Get the dimension of the image
105  auto width = image_hdu.axis(1);
106  auto height = image_hdu.axis(0);
107  // Get the data
108  std::valarray<double> data {};
109  image_hdu.read(data);
110  cv::Mat image (width, height, CV_64F);
111  std::copy(begin(data), end(data), image.begin<double>());
112  result.emplace_back(image, pixel_scale);
113  }
114  return result;
115 }
116 
117 // Prints on the screen the info of the levmar minimization
119  std::cout << "\nMinimization info:\n";
120  std::cout << " ||e||_2 at initial p: " << info[0] << '\n';
121  std::cout << " ||e||_2: " << info[1] << '\n';
122  std::cout << " ||J^T e||_inf: " << info[2] << '\n';
123  std::cout << " ||Dp||_2: " << info[3] << '\n';
124  std::cout << " mu/max[J^T J]_ii: " << info[4] << '\n';
125  std::cout << " # iterations: " << info[5] << '\n';
126  switch ((int)info[6]) {
127  case 1:
128  std::cout << " stopped by small gradient J^T e\n";
129  break;
130  case 2:
131  std::cout << " stopped by small Dp\n";
132  break;
133  case 3:
134  std::cout << " stopped by itmax\n";
135  break;
136  case 4:
137  std::cout << " singular matrix. Restart from current p with increased mu\n";
138  break;
139  case 5:
140  std::cout << " no further error reduction is possible. Restart with increased mu\n";
141  break;
142  case 6:
143  std::cout << " stopped by small ||e||_2\n";
144  break;
145  case 7:
146  std::cout << " stopped by invalid (i.e. NaN or Inf) func values; a user error\n";
147  break;
148  default:
149  std::cout << " unknown stop reason " << (int)info[6] << "\n";
150  }
151  std::cout << " # function evaluations: " << info[7] << '\n';
152  std::cout << " # Jacobian evaluations: " << info[8] << '\n';
153  std::cout << " # linear systems solved: " << info[9] << "\n\n";
154 }
155 
156 #endif /* UTILS_H */
157 
void printLevmarInfo(std::array< double, 10 > info)
Definition: utils.h:118
T copy(T... args)
void writeToFits(const cv::Mat &image, const std::string &filename)
Definition: utils.h:40
STL class.
std::vector< std::pair< cv::Mat, double > > readFrames(const std::string &filename)
Definition: utils.h:95
string filename
Definition: conf.py:63
STL class.
T make_pair(T... args)
STL class.
STL class.
ModelFitting::OpenCvPsf readPsf(const std::string &filename)
Definition: utils.h:53
std::pair< cv::Mat, double > readImage(const std::string &filename)
Definition: utils.h:77
const double pixel_scale
Definition: TestImage.cpp:75