AliceVision
Photogrammetric Computer Vision Framework
lcp.hpp
1 #pragma once
2 
3 #include <filesystem>
4 #include <string>
5 #include <vector>
6 #include <sstream>
7 #include <map>
8 
9 enum class LCPCorrectionMode
10 {
11  VIGNETTE,
12  DISTORTION,
13  CA
14 };
15 
16 enum class LCPReadingState
17 {
18  WaitSequence,
19  FillCommonAndCameraSettings,
20  FillCameraSettings,
21  FillGeometryModel,
22  FillVignetteModel,
23  FillChromaticGreenModel,
24  FillChromaticBlueGreenModel,
25  FillChromaticRedGreenModel,
26  WaitNewModel
27 };
28 
29 inline std::ostream& operator<<(std::ostream& os, const LCPReadingState& state)
30 {
31  if (state == LCPReadingState::WaitSequence)
32  os << "WaitSequence";
33  else if (state == LCPReadingState::FillCommonAndCameraSettings)
34  os << "FillCommonAndCameraSettings";
35  else if (state == LCPReadingState::FillCameraSettings)
36  os << "FillCameraSettings";
37  else if (state == LCPReadingState::FillGeometryModel)
38  os << "FillGeometryModel";
39  else if (state == LCPReadingState::FillVignetteModel)
40  os << "FillVignetteModel";
41  else if (state == LCPReadingState::FillChromaticGreenModel)
42  os << "FillChromaticGreenModel";
43  else if (state == LCPReadingState::FillChromaticBlueGreenModel)
44  os << "FillChromaticBlueGreenModel";
45  else if (state == LCPReadingState::FillChromaticRedGreenModel)
46  os << "FillChromaticRedGreenModel";
47  else if (state == LCPReadingState::WaitNewModel)
48  os << "WaitNewModel";
49 
50  return os;
51 }
52 
54 {
55  float FocalLength = 0.f;
56  float FocusDistance = 0.f;
57  float ApertureValue = 0.f;
58 
59  void reset()
60  {
61  FocalLength = 0.f;
62  FocusDistance = 0.f;
63  ApertureValue = 0.f;
64  }
65 };
66 
73 {
74  int Version = -1;
75  float FocalLengthX = 0.f;
76  float FocalLengthY = 0.f;
77  float ImageXCenter = 0.5f;
78  float ImageYCenter = 0.5f;
79  float ResidualMeanError = 0.f;
80  float ResidualStandardDeviation = 0.f;
81  float RadialDistortParam1 = 0.f;
82  float RadialDistortParam2 = 0.f;
83  float RadialDistortParam3 = 0.f;
84  float TangentialDistortParam1 = 0.f;
85  float TangentialDistortParam2 = 0.f;
86  float ScaleFactor = 1.f;
87  bool isEmpty = true;
88 
89  void reset() { *this = RectilinearModel(); }
90 
91  void distort(const float x, const float y, float& x_d, float& y_d)
92  {
93  const float rr = x * x + y * y;
94  const float p1 = 1.f + rr * (RadialDistortParam1 + rr * (RadialDistortParam2 + rr * RadialDistortParam3));
95  const float p2 = TangentialDistortParam1 * y + TangentialDistortParam2 * x;
96  x_d = ScaleFactor * (p1 * x + 2 * p2 * x + TangentialDistortParam2 * rr);
97  y_d = ScaleFactor * (p1 * y + 2 * p2 * y + TangentialDistortParam1 * rr);
98  }
99 
100  bool init3(const std::vector<float>& params)
101  {
102  if (params.size() < 7)
103  {
104  reset();
105  return false;
106  }
107  FocalLengthX = params[0];
108  FocalLengthY = params[1];
109  ImageXCenter = params[2];
110  ImageYCenter = params[3];
111  RadialDistortParam1 = params[4];
112  RadialDistortParam2 = params[5];
113  RadialDistortParam3 = params[6];
114  ScaleFactor = (params.size() >= 8) ? params[7] : 1.0;
115  isEmpty = false;
116  return true;
117  }
118 
119  bool init5(const std::vector<float>& params)
120  {
121  if (params.size() < 9)
122  {
123  reset();
124  return false;
125  }
126  init3(params);
127  TangentialDistortParam1 = params[7];
128  TangentialDistortParam2 = params[8];
129  ScaleFactor = (params.size() >= 10) ? params[9] : 1.0;
130  isEmpty = false;
131  return true;
132  }
133 };
134 
135 inline std::ostream& operator<<(std::ostream& os, const RectilinearModel& model)
136 {
137  if (model.isEmpty)
138  {
139  os << "Empty";
140  }
141  else
142  {
143  os << "Focal: (" << model.FocalLengthX << ", " << model.FocalLengthY << ")" << std::endl;
144  os << "Center: (" << model.ImageXCenter << ", " << model.ImageYCenter << ")" << std::endl;
145  os << "Radial: (" << model.RadialDistortParam1 << ", " << model.RadialDistortParam2 << ", " << model.RadialDistortParam3 << ")" << std::endl;
146  os << "Tangential: (" << model.TangentialDistortParam1 << ", " << model.TangentialDistortParam2 << ")" << std::endl;
147  os << "scale: " << model.ScaleFactor;
148  }
149  return os;
150 }
151 
158 {
159  float FocalLengthX = 0.f;
160  float FocalLengthY = 0.f;
161  float ImageXCenter = 0.5f;
162  float ImageYCenter = 0.5f;
163  float VignetteModelParam1 = 0.f;
164  float VignetteModelParam2 = 0.f;
165  float VignetteModelParam3 = 0.f;
166  bool isEmpty = true;
167 
168  void reset() { *this = VignetteModel(); }
169 };
170 
177 {
178  int Version = -1;
179  float FocalLengthX = 0.f;
180  float FocalLengthY = 0.f;
181  float ImageXCenter = 0.5f;
182  float ImageYCenter = 0.5f;
183  float ResidualMeanError = 0.f;
184  float ResidualStandardDeviation = 0.f;
185  float RadialDistortParam1 = 0.f;
186  float RadialDistortParam2 = 0.f;
187  bool isEmpty = true;
188 
189  void reset() { *this = FisheyeModel(); }
190 };
191 
199 {
200  public:
201  LensParam() = default;
202 
206  void clear();
207 
212  bool isEmpty() const { return perspParams.isEmpty && fisheyeParams.isEmpty; }
213 
218  bool isFisheye() const { return _isFisheye; }
219 
224  bool hasVignetteParams() const { return _hasVignetteParams; }
225 
230  bool hasChromaticParams() const { return _hasChromaticParams; }
231 
236  void setFisheyeStatus(bool s) { _isFisheye = s; }
237 
242  void setVignetteParamsStatus(bool s) { _hasVignetteParams = s; }
243 
248  void setChromaticParamsStatus(bool s) { _hasChromaticParams = s; }
249 
262 
270  // PerspectiveModel perspParams;
276 
281 
282  private:
283  bool _isFisheye = false;
284  bool _hasVignetteParams = false;
285  bool _hasChromaticParams = false;
286 };
287 
295 class LCPinfo
296 {
297  public:
298  LCPinfo() = default;
299 
305  LCPinfo(const std::string& filename, bool fullParsing = true);
306  ~LCPinfo() = default;
307 
313  void load(const std::string& filename, bool fullParsing = true);
314 
321  void getDistortionParams(const float& focalLength, const float& focusDistance, LensParam& lparam);
322 
329  void getVignettingParams(const float& focalLength, const float& aperture, LensParam& lparam);
330 
337  void getChromaticParams(const float& focalLength, const float& focusDistance, LensParam& lparam);
338 
343  inline bool isEmpty() const { return v_lensParams.empty(); }
344 
349  inline const std::string& getAuthor() const { return Author; }
350 
355  inline const std::string& getProfileName() const { return ProfileName; }
356 
361  inline const std::string& getCameraMaker() const { return Make; }
362 
367  inline const std::string& getCameraModel() const { return Model; }
368 
373  inline const std::string& getUniqueCameraModel() const { return UniqueCameraModel; }
374 
379  inline const std::string& getCameraPrettyName() const { return CameraPrettyName; }
380 
385  inline const std::string& getLensPrettyName() const { return LensPrettyName; }
386 
391  inline const std::string& getLensInfo() const { return LensInfo; }
392 
397  inline void getLensIDs(std::vector<int>& lensIDs) const { lensIDs = LensID; }
398 
403  inline void getLensModels(std::vector<std::string>& lensModels) const { lensModels = Lens; }
404 
409  inline int getImageWidth() const { return ImageWidth; }
410 
415  inline int getImageLength() const { return ImageLength; }
416 
421  inline float getSensorFormatFactor() const { return SensorFormatFactor; }
422 
427  inline bool isRawProfile() const { return CameraRawProfile; }
428 
433  inline int getModelNumber() const { return v_lensParams.size(); }
434 
439  inline void setAuthor(const std::string& str) { Author = str; }
440 
444  inline void setAuthor() { Author = _currText; }
445 
450  inline void setProfileName(const std::string& str) { ProfileName = str; }
451 
456  inline void setCameraMaker(const std::string& str) { Make = str; }
457 
462  inline void setCameraModel(const std::string& str) { Model = str; }
463 
468  inline void setUniqueCameraModel(const std::string& str) { UniqueCameraModel = str; }
469 
474  inline void setCameraPrettyName(const std::string& str) { CameraPrettyName = str; }
475 
480  inline void setLensPrettyName(const std::string& str) { LensPrettyName = str; }
481 
486  inline void setLensInfo(const std::string& str) { LensInfo = str; }
487 
492  inline void addLensID(int lensID) { LensID.push_back(lensID); }
493 
498  inline void addLensModel(std::string lensModel) { Lens.push_back(lensModel); }
499 
504  inline void setImageWidth(int w) { ImageWidth = w; }
505 
510  inline void setImageLength(int l) { ImageLength = l; }
511 
516  inline void setSensorFormatFactor(float f) { SensorFormatFactor = f; }
517 
522  inline void setAsRawProfile() { CameraRawProfile = true; }
523 
524  private:
525  // XML handlers
526  static void XmlStartHandler(void* pLCPinfo, const char* el, const char** attr);
527  static void XmlEndHandler(void* pLCPinfo, const char* el);
528  static void XmlStartHandlerCommonOnly(void* pLCPinfo, const char* el, const char** attr);
529  static void XmlEndHandlerCommonOnly(void* pLCPinfo, const char* el);
530  static void XmlTextHandler(void* pLCPinfo, const char* s, int len);
531 
532  // Loading states control
533  inline bool isSeqOpened() { return _isSeqOpened; }
534  inline void openSequence() { _isSeqOpened = true; }
535  inline void closeSequence() { _isSeqOpened = false; }
536 
537  inline bool isCommonOK() { return _isCommonOK; }
538  inline void setCommonOK() { _isCommonOK = true; }
539  inline void unsetCommonOK() { _isCommonOK = false; }
540 
541  inline bool isCamDataOK() { return _isCamDataOK; }
542  inline void setCamDataOK() { _isCamDataOK = true; }
543  inline void unsetCamDataOK() { _isCamDataOK = false; }
544 
545  inline bool isAlternateLensIDsOpened() { return _inAlternateLensIDs; }
546  inline void openAlternateLensIDs() { _inAlternateLensIDs = true; }
547  inline void closeAlternateLensIDs() { _inAlternateLensIDs = false; }
548 
549  inline bool isAlternateLensNamesOpened() { return _inAlternateLensNames; }
550  inline void openAlternateLensNames() { _inAlternateLensNames = true; }
551  inline void closeAlternateLensNames() { _inAlternateLensNames = false; }
552 
553  inline bool isWaitPerspModeldescription() { return _waitPerspModeldescription; }
554  inline void setWaitPerspModeldescription() { _waitPerspModeldescription = true; }
555  inline void unsetWaitPerspModeldescription() { _waitPerspModeldescription = false; }
556 
557  inline bool isGetText() { return _getText; }
558  inline void setGetText() { _getText = true; }
559  inline void unsetGetText() { _getText = false; }
560 
561  inline int getModelCount() { return _modelCount; }
562  inline void increaseModelCount() { _modelCount++; }
563 
564  inline void storeCurrParams() { v_lensParams.push_back(currLensParam); }
565 
566  LensParam currLensParam;
567 
568  bool search(settingsInfo& settings, LCPCorrectionMode mode, int& iLow, int& iHigh, float& weightLow);
569  void combine(size_t iLow, size_t iHigh, float weightLow, LCPCorrectionMode mode, LensParam& pOut);
570 
571  // Loading states
572  bool _isSeqOpened = false;
573  bool _isCommonOK = false;
574  bool _isCamDataOK = false;
575  bool _inAlternateLensIDs = false;
576  bool _inAlternateLensNames = false;
577  bool _waitPerspModeldescription = false;
578  bool _getText = false;
579  int _modelCount = 0;
580 
581  LCPReadingState _currReadingState = LCPReadingState::WaitSequence;
582  std::string _currText = "";
583 
584  // Set of models contained in the LCP file
585  std::vector<LensParam> v_lensParams;
586 
587  // Camera and Lens information, common for all models
588  std::string Author = "";
589  std::string Make = "";
590  std::string Model = "";
591  std::string UniqueCameraModel = "";
592  bool CameraRawProfile;
593  std::vector<int> LensID;
594  std::vector<std::string> Lens;
595  std::string LensInfo = "";
596  std::string CameraPrettyName = "";
597  std::string LensPrettyName = "";
598  std::string ProfileName = "";
599  float SensorFormatFactor = 1.f;
600  int ImageWidth = 0;
601  int ImageLength = 0;
602  float XResolution = 0.f;
603  float YResolution = 0.f;
604 
605  void setCommonSettings(const std::string& name);
606  void setCameraSettings(const std::string& name);
607  void setRectilinearModel(RectilinearModel& model, const std::string& name);
608  void setFisheyeModel(const std::string& name);
609  void setVignetteModel(const std::string& name);
610 };
611 
612 std::string reduceString(const std::string& str);
613 std::vector<std::string> reduceStrings(const std::vector<std::string>& v_str);
614 
619 {
620  public:
621  LCPdatabase() = default;
626  LCPdatabase(const std::string& folder, bool omitCameraModel = false)
627  : _omitCameraModel(omitCameraModel)
628  {
629  loadDirectory(folder);
630  }
631  ~LCPdatabase() = default;
632 
633  bool empty() const { return _lcpFilepaths.empty(); }
634 
635  size_t size() const { return _lcpFilepaths.size(); }
636 
637  void loadDirectory(const std::filesystem::path& p);
638 
639  LCPinfo* retrieveLCP() { return retrieveLCP(_lcpFilepaths.begin()->path.string()); }
640 
644  LCPinfo* retrieveLCP(const std::string& p);
645 
661  LCPinfo* findLCP(const std::string& cameraMake, const std::string& cameraModel, const std::string& lensModel, const int lensID, int rawMode);
662 
663  private:
664  struct LcpPath
665  {
666  LcpPath(const std::filesystem::path& p)
667  : path(p),
668  reducedPath(reduceString(p.string()))
669  {}
670  std::filesystem::path path;
671  std::string reducedPath;
672  };
673 
675  std::vector<LcpPath> _lcpFilepaths;
677  std::map<std::string, LCPinfo> _lcpHeaderCache;
679  std::map<std::string, LCPinfo> _lcpCache;
681  std::map<std::string, std::string> _lcpCameraMappingCache;
684  bool _omitCameraModel = false;
685 };
LensParam
LensParam contains parameters of distortion, vignetting and chromatic aberration models for a set of ...
Definition: lcp.hpp:198
LensParam::ChromaticBlueGreenParams
RectilinearModel ChromaticBlueGreenParams
Chromatic Blue/Green model parameters.
Definition: lcp.hpp:261
LCPdatabase::LCPdatabase
LCPdatabase(const std::string &folder, bool omitCameraModel=false)
LCPdatabase constructor.
Definition: lcp.hpp:626
LCPinfo::getLensModels
void getLensModels(std::vector< std::string > &lensModels) const
Get all known model names for the lens.
Definition: lcp.hpp:403
LensParam::ChromaticRedGreenParams
RectilinearModel ChromaticRedGreenParams
Chromatic Red/Green model parameters.
Definition: lcp.hpp:257
LCPinfo::setSensorFormatFactor
void setSensorFormatFactor(float f)
Set sensor format factor.
Definition: lcp.hpp:516
LensParam::hasChromaticParams
bool hasChromaticParams() const
Indicate that chromatic models are available.
Definition: lcp.hpp:230
LCPinfo::getImageWidth
int getImageWidth() const
Get image width.
Definition: lcp.hpp:409
LCPinfo::setUniqueCameraModel
void setUniqueCameraModel(const std::string &str)
Set unique camera model.
Definition: lcp.hpp:468
LCPinfo::getImageLength
int getImageLength() const
Get image length.
Definition: lcp.hpp:415
LCPinfo::getVignettingParams
void getVignettingParams(const float &focalLength, const float &aperture, LensParam &lparam)
Get vignetting parameters for a given couple focal length, aperture value. Aperture value can set to ...
Definition: lcp.cpp:990
LCPinfo::setAsRawProfile
void setAsRawProfile()
Set raw profile status.
Definition: lcp.hpp:522
LCPinfo::getModelNumber
int getModelNumber() const
Get lens information.
Definition: lcp.hpp:433
LCPinfo::setLensInfo
void setLensInfo(const std::string &str)
Set lens information.
Definition: lcp.hpp:486
LensParam::ChromaticGreenParams
RectilinearModel ChromaticGreenParams
Chromatic Green model parameters.
Definition: lcp.hpp:253
LCPinfo::getUniqueCameraModel
const std::string & getUniqueCameraModel() const
Get unique camera model.
Definition: lcp.hpp:373
LCPinfo::getLensIDs
void getLensIDs(std::vector< int > &lensIDs) const
Get all known IDs for the lens.
Definition: lcp.hpp:397
LCPinfo::getLensPrettyName
const std::string & getLensPrettyName() const
Get lens pretty name.
Definition: lcp.hpp:385
LCPinfo::setAuthor
void setAuthor()
Set profile author with current text value.
Definition: lcp.hpp:444
RectilinearModel
RectilinearModel contains parameters of a rectilinear model of distortion Detailed information on thi...
Definition: lcp.hpp:72
LCPinfo::setImageWidth
void setImageWidth(int w)
Set image width.
Definition: lcp.hpp:504
LCPinfo::setCameraPrettyName
void setCameraPrettyName(const std::string &str)
Set camera pretty name.
Definition: lcp.hpp:474
LensParam::camData
settingsInfo camData
Camera settings.
Definition: lcp.hpp:280
LCPdatabase
LCPdatabase allows to access all the LCP files in the database.
Definition: lcp.hpp:618
LCPinfo::addLensModel
void addLensModel(std::string lensModel)
Set an alternate model name for the lens.
Definition: lcp.hpp:498
VignetteModel
VignetteModel contains parameters of a vignetting model of distortion Detailed information on this mo...
Definition: lcp.hpp:157
LCPinfo::setAuthor
void setAuthor(const std::string &str)
Set profile author.
Definition: lcp.hpp:439
LCPinfo::getSensorFormatFactor
float getSensorFormatFactor() const
Get sensor format factor.
Definition: lcp.hpp:421
LensParam::perspParams
RectilinearModel perspParams
Pinhole model parameters.
Definition: lcp.hpp:271
LCPinfo::getDistortionParams
void getDistortionParams(const float &focalLength, const float &focusDistance, LensParam &lparam)
Get distortion parameters for a given couple focal length, focus distance. Focus distance can set to ...
Definition: lcp.cpp:975
LCPinfo::getProfileName
const std::string & getProfileName() const
Get profile name.
Definition: lcp.hpp:355
LensParam::isFisheye
bool isFisheye() const
Indicate that parameters apply for a fisheye lens.
Definition: lcp.hpp:218
LCPinfo::getCameraModel
const std::string & getCameraModel() const
Get camera model.
Definition: lcp.hpp:367
LensParam::setChromaticParamsStatus
void setChromaticParamsStatus(bool s)
Set chromatic models availability status.
Definition: lcp.hpp:248
LCPinfo::setLensPrettyName
void setLensPrettyName(const std::string &str)
Set lens pretty name.
Definition: lcp.hpp:480
LCPinfo::isEmpty
bool isEmpty() const
Indicate that no lens parameter set is available.
Definition: lcp.hpp:343
LCPinfo::setProfileName
void setProfileName(const std::string &str)
Set profile name.
Definition: lcp.hpp:450
LensParam::clear
void clear()
LensParam reset.
Definition: lcp.cpp:20
LensParam::setFisheyeStatus
void setFisheyeStatus(bool s)
Set fisheye status.
Definition: lcp.hpp:236
LensParam::vignParams
VignetteModel vignParams
Vignetting model parameters.
Definition: lcp.hpp:275
FisheyeModel
FisheyeModel contains parameters of a fisheye model of distortion Detailed information on this model ...
Definition: lcp.hpp:176
settingsInfo
Definition: lcp.hpp:53
LCPinfo::getCameraPrettyName
const std::string & getCameraPrettyName() const
Get camera pretty name.
Definition: lcp.hpp:379
LCPinfo::setCameraModel
void setCameraModel(const std::string &str)
Set camera model.
Definition: lcp.hpp:462
LCPinfo::load
void load(const std::string &filename, bool fullParsing=true)
LCPinfo loader.
Definition: lcp.cpp:508
LCPinfo::setImageLength
void setImageLength(int l)
Set image length.
Definition: lcp.hpp:510
LensParam::isEmpty
bool isEmpty() const
Indicate that no geometric model is available.
Definition: lcp.hpp:212
LCPinfo
LCPinfo loads and hosts the content of a Lens Correction Profile (LCP) file, parameters of distortion...
Definition: lcp.hpp:295
LensParam::setVignetteParamsStatus
void setVignetteParamsStatus(bool s)
Set vignetting availability status.
Definition: lcp.hpp:242
LCPdatabase::findLCP
LCPinfo * findLCP(const std::string &cameraMake, const std::string &cameraModel, const std::string &lensModel, const int lensID, int rawMode)
Try to find an appropriate LCP file for a set of camera and lens information amongst a set of files....
Definition: lcp.cpp:1192
LCPinfo::isRawProfile
bool isRawProfile() const
Get raw profile status.
Definition: lcp.hpp:427
LensParam::fisheyeParams
FisheyeModel fisheyeParams
Fisheye model parameters.
Definition: lcp.hpp:266
LCPinfo::getChromaticParams
void getChromaticParams(const float &focalLength, const float &focusDistance, LensParam &lparam)
Get defringing parameters for a given couple focal length, focus distance. Focus distance can set to ...
Definition: lcp.cpp:1005
LCPinfo::addLensID
void addLensID(int lensID)
Set an alternate lens ID for the lens.
Definition: lcp.hpp:492
LensParam::hasVignetteParams
bool hasVignetteParams() const
Indicate that a vignetting model is available.
Definition: lcp.hpp:224
LCPinfo::getCameraMaker
const std::string & getCameraMaker() const
Get camera maker.
Definition: lcp.hpp:361
LCPinfo::setCameraMaker
void setCameraMaker(const std::string &str)
Set camera maker.
Definition: lcp.hpp:456
LCPinfo::getLensInfo
const std::string & getLensInfo() const
Get lens information.
Definition: lcp.hpp:391
LCPinfo::getAuthor
const std::string & getAuthor() const
Get profile author.
Definition: lcp.hpp:349