Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
CameraParam.cpp
1 /*
2 $Id: CameraParam.cpp,v 2.30 2006/07/31 13:47:40 streckel Exp $
3 */
4 /*
5 This file is part of the BIAS library (Basic ImageAlgorithmS).
6 
7 Copyright (C) 2003-2009 (see file CONTACT for details)
8  Multimediale Systeme der Informationsverarbeitung
9  Institut fuer Informatik
10  Christian-Albrechts-Universitaet Kiel
11 
12 
13 BIAS is free software; you can redistribute it and/or modify
14 it under the terms of the GNU Lesser General Public License as published by
15 the Free Software Foundation; either version 2.1 of the License, or
16 (at your option) any later version.
17 
18 BIAS is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU Lesser General Public License for more details.
22 
23 You should have received a copy of the GNU Lesser General Public License
24 along with BIAS; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 */
27 #include "CameraParam.hh"
28 #include <Geometry/ProjectionParametersIO.hh>
29 #include <iostream>
30 #include <math.h>
31 #include <Base/Image/ImageAttributes.hh>
32 
33 using namespace BIAS;
34 using namespace std;
35 
36 
37 /*
38 std::string CameraParam::CameraModelName[]={
39  "SONY DFW-X700",
40  "SONY DFW-V500",
41  "PTGREY DRAGONFLY COLOR",
42  "SONY EVI-D31",
43  "CAMERA_SONY_DV",
44  "UNKNOWN",
45 };
46 
47 std::string CameraParam::LensModelName[]={
48  "SONY EVID31 BUILTIN",
49  "SONY DV BUILTIN",
50  "PENTAX CMOUNT 4.8",
51  "PENTAX CMOUNT 8.0",
52  "PENTAX CMOUNT 4.2",
53 };
54 */
55 
56 
58  Init();
59 }
60 
61 
63  Init(cp);
64 }
65 
66 
67 #ifdef BIAS_HAVE_XML2
68 CameraParam::CameraParam(const std::string &filename)
69 {
70  Init(filename);
71 }
72 #endif //BIAS_HAVE_XML2
73 
74 
75 // #ifdef BIAS_HAVE_XML2
76 // CameraParam::CameraParam(XMLIO &myXML, xmlNodePtr &cameraNode){
77 // Init(myXML, cameraNode);
78 // }
79 // #endif //BIAS_HAVE_XML2
80 
81 
83 }
84 
85 
87 {
88  cd_.MaxCamAngle_=0;
89  cd_.CamModel_ = "Default Initialization";
90  cd_.CamID_ = 1;
91  cd_.LensModel_ = "Default Initialization";
92  cd_.LensIsSpherical_ = false;
93 
94  cd_.width_=cd_.height_=0;
95  cd_.aspectratio_ = 1;
96  cd_.focallengthDef_ = 0;
97  cd_.kc1Def_ = cd_.kc2Def_ = cd_.kc3Def_ = cd_.kc4Def_ = 0;
98  cd_.cellSizeX_ = cd_.cellSizeY_ = 0;
99  cd_.principalX_ = cd_.principalY_ =0;
100  cd_.IlluCorrX_.clear();
101  cd_.IlluCorrY_.clear();
102  cd_.knownparams_vect_.clear();
103  cd_.cellSizeX_ = cd_.cellSizeY_ = 1;
104  cd_.zoomCamera_=false;
105  cd_.AngleCorrX_.clear();
106  cd_.AngleCorrY_.clear();
107  cd_.IlluCorrX_.clear();
108  cd_.IlluCorrY_.clear();
109  cd_.radius_ = 0;
110  zoom_ = 0;
111  focallengthZoom_ = 0;
112  downsamplingFactor_ = 1.0;
113 }
114 
116  SetCameraData(cp.GetCameraData());
117 // cd_.CamModel_ = cp.GetCameraData().CamModel_;
118 // cd_.CamID_ = cp.GetCameraData().CamID_;
119 // cd_.LensModel_ = cp.GetCameraData().LensModel_;
120 // cd_.LensIsSpherical_ = cp.GetCameraData().LensIsSpherical_;
121 // cd_.zoomCamera_ = cp.GetCameraData().zoomCamera_;
122 // cd_.width_ = cp.GetCameraData().width_;
123 // cd_.height_ = cp.GetCameraData().height_;
124 // cd_.knownparams_vect_ = cp.GetCameraData().knownparams_vect_;
125 // cd_.aspectratio_ = cp.GetCameraData().aspectratio_;
126 // cd_.cellSizeX_ = cp.GetCameraData().cellSizeX_;
127 // cd_.cellSizeY_ = cp.GetCameraData().cellSizeY_;
128 // cd_.principalX_ = cp.GetCameraData().principalX_;
129 // cd_.principalY_ = cp.GetCameraData().principalY_;
130 // cd_.focallengthDef_ = cp.GetCameraData().focallengthDef_;
131 // cd_.kc1Def_ = cp.GetCameraData().kc1Def_;
132 // cd_.kc2Def_ = cp.GetCameraData().kc2Def_;
133 // cd_.kc3Def_ = cp.GetCameraData().kc3Def_;
134 // cd_.kc4Def_ = cp.GetCameraData().kc4Def_;
135 // cd_.IlluCorrX_ = cp.GetCameraData().IlluCorrX_;
136 // cd_.IlluCorrY_ = cp.GetCameraData().IlluCorrY_;
137 // cd_.AngleCorrX_ = cp.GetCameraData().AngleCorrX_;
138 // cd_.AngleCorrY_ = cp.GetCameraData().AngleCorrY_;
139 // if (cd_.AngleCorrX_.size()>0)
140 // cd_.MaxCamAngle_ = cd_.AngleCorrX_[cd_.AngleCorrX_.size()-1];
141 // else
142 // cd_.MaxCamAngle_ = 0;
143 // cd_.radius_ = cp.GetCameraData().radius_;
144  downsamplingFactor_ = cp.downsamplingFactor_;
145  if (cd_.zoomCamera_) {
146  zoom_ = cd_.knownparams_vect_[0].zoom;
147  focallengthZoom_ = cd_.knownparams_vect_[0].focallength;
148  }
149 }
150 
151 #ifdef BIAS_HAVE_XML2
152 int CameraParam::Init(const std::string &filename) {
153  CameraData cd;
154  int res = ProjectionParametersIO::ReadCameraData(filename, cd);
155  if (res <0) return res;
156  SetCameraData(cd);
157  return 0;
158 }
159 #endif //BIAS_HAVE_XML2
160 
161 
162 // #ifdef BIAS_HAVE_XML2
163 // int CameraParam::Init(XMLIO &myXML, xmlNodePtr &cameraNode)
164 // {
165 // int res = ProjectionParametersIO::ReadCameraData(myXML, cameraNode);
166 // if (res <0) return res;
167 // return 0;
168 // }
169 // #endif //BIAS_HAVE_XML2
170 
171 
173  KMatrix K;
174  K.SetIdentity();
175  if (IsPerspective()) {
176  K[0][0] = GetFocalLength();
177  K[1][1] = cd_.aspectratio_ * GetFocalLength();
178  K[0][1] = 0; //skew_;
179  K[0][2] = cd_.principalX_;
180  K[1][2] = cd_.principalY_;
181  } else {
182  BIASERR("No perspective camera-> No K-Matrix!");
183  }
184  return K;
185 }
186 
187 
188 void CameraParam::SetPrincipal(double x, double y) {
189  cd_.principalX_ = x;
190  cd_.principalY_ = y;
191 }
192 
194  return cd_.principalX_/downsamplingFactor_+0.000000001;
195 }
196 
198  return cd_.principalY_/downsamplingFactor_+0.000000001;
199 }
200 
201 unsigned int CameraParam::GetHeight() const {
202  return (int)(cd_.height_/downsamplingFactor_+0.000000001);
203 }
204 
205 unsigned int CameraParam::GetWidth() const {
206  return (int)(cd_.width_/downsamplingFactor_+0.000000001);
207 }
208 
209 void CameraParam::SetCellSize(const double CellSizeX,
210  const double AspectRatio) {
211  cd_.cellSizeX_ = CellSizeX;
212  if (AspectRatio!=0) {
213  cd_.cellSizeY_ = CellSizeX / AspectRatio;
214  cd_.aspectratio_ = AspectRatio;
215  } else {
216  cd_.cellSizeY_ = CellSizeX;
217  cd_.aspectratio_ = 1;
218  }
219 }
220 
221 void CameraParam::SetZoomCamera(const bool zoomCam) {
222  cd_.zoomCamera_=zoomCam;
223 }
224 
225 
227  return cd_.zoomCamera_;
228 }
229 
230 double CameraParam::GetZoom() const {
231  if (IsPerspective()) {
232  if (cd_.zoomCamera_) {
233  return zoom_;
234  }
235  return 0;
236  } else {
237  BIASERR("No perspective camera!");
238  return -1;
239  }
240 }
241 
243  if (IsPerspective()) {
244  if ( cd_.zoomCamera_) {
245  return focallengthZoom_/downsamplingFactor_+0.000000001;
246  }
247  return cd_.focallengthDef_/downsamplingFactor_+0.000000001;
248  } else {
249  BIASERR("No perspective camera!");
250  return -1;
251  }
252 }
253 
254 
256  return downsamplingFactor_;
257 }
258 
259 void CameraParam::SetZoom(const double abszoom){
260  if (cd_.zoomCamera_ && IsPerspective()) {
261  zoom_=abszoom;
262  focallengthZoom_ = InterpolateFocalLength_();
263  cout << "interpolating zoom:" << abszoom << " to: " <<
264  focallengthZoom_ << endl;
265  } else {
266  BIASERR("No perspective/zoom camera!");
267  return;
268  }
269 }
270 
271 
272 void CameraParam::SetFocalLength(const double focallength){
273  if (IsPerspective())
274  if (cd_.zoomCamera_){
275  focallengthZoom_=focallength;
276  zoom_=InterpolateAbsZoom_();
277  }
278  else
279  cd_.focallengthDef_ = focallength;
280  else
281  BIASERR("No perspective camera!");
282 }
283 
284 void CameraParam::SetDownsampling(const double factor) {
285  downsamplingFactor_ = factor;
286 }
287 
289  if (cd_.zoomCamera_ && IsPerspective()) {
290  if (cd_.focallengthDef_==0) {
291  zoom_=cd_.knownparams_vect_[0].zoom;
292  focallengthZoom_=cd_.knownparams_vect_[0].focallength;
293  } else {
294  zoom_=0;
295  focallengthZoom_=cd_.focallengthDef_;
296  }
297  }
298 }
299 
300 
301 #ifdef BIAS_HAVE_XML2
302 int CameraParam::WriteCameraData(const std::string& Filename) const {
303  return ProjectionParametersIO::WriteCameraData(Filename, GetCameraData());
304 
305 }
306 #endif //BIAS_HAVE_XML2
307 
308 
309 #ifdef BIAS_HAVE_XML2
310 // int CameraParam::AddCameraDataToRig(XMLIO &myXML,
311 // xmlNodePtr &cameraDataNode) const {
312 
313 // }
314 #endif //BIAS_HAVE_XML2
315 
316 
317 
318 #ifdef BIAS_HAVE_XML2
319 int CameraParam::ReadCameraData(const std::string& Filename) {
320  CameraData cd;
321  int res;
322  res = ProjectionParametersIO::ReadCameraData(Filename, cd);
323  SetCameraData(cd);
324  return res;
325 }
326 
327 // int CameraParam::ReadCameraData(XMLIO &myXML, xmlNodePtr &cameraNode) {
328 
329 // }
330 
331 
332 #endif //BIAS_HAVE_XML2
333 
334 void CameraParam::InitSpherical(const string CamModel, const int CamID,
335  const string LensModel,
336  const int imgWidth, const int imgHeight,
337  const double principalX,
338  const double principalY,
339  const double radius,
340  const vector<double> &AngleCorrX,
341  const vector<double> &AngleCorrY,
342  const vector<double> &IlluCorrX,
343  const vector<double> &IlluCorrY) {
344 
345  //reset this
346  Init();
347  downsamplingFactor_=1;
348 
349  cd_.CamModel_ = CamModel;
350  cd_.CamID_ = CamID;
351  cd_.LensModel_=LensModel;
352 
353  cd_.width_ = imgWidth;
354  cd_.height_ = imgHeight;
355  cd_.principalX_ = principalX;
356  cd_.principalY_ = principalY;
357  cd_.radius_ = radius;
358 
359  cd_.aspectratio_=1;
360  cd_.cellSizeX_=1;
361  cd_.cellSizeY_=1;
362  cd_.LensIsSpherical_ = true;
363  cd_.zoomCamera_=false;
364 
365  cd_.AngleCorrX_ = AngleCorrX;
366  cd_.AngleCorrY_ = AngleCorrY;
367  cd_.IlluCorrX_ = IlluCorrX;
368  cd_.IlluCorrY_ = IlluCorrY;
369 }
370 
371 
372 void CameraParam::InitPerspective(const string CamModel, const int CamID,
373  const string LensModel,
374  const int imgWidth, const int imgHeight,
375  const double principalX,
376  const double principalY,
377  const double focallength,
378  const double aspectratio,
379  const double k1, const double k2, const double k3, const double k4,
380  const vector<double> &IlluCorrX,
381  const vector<double> &IlluCorrY) {
382 
383  //reset this
384  Init();
385  downsamplingFactor_=1;
386  cd_.CamModel_ = CamModel;
387  cd_.CamID_ = CamID;
388  cd_.LensModel_=LensModel;
389  cd_.width_ = imgWidth;
390  cd_.height_ = imgHeight;
391  cd_.principalX_ = principalX;
392  cd_.principalY_ = principalY;
393  cd_.radius_ = 0.0;
394  cd_.focallengthDef_ = focallength;
395  cd_.aspectratio_=aspectratio;
396  cd_.cellSizeX_=1;
397  cd_.cellSizeY_=1/aspectratio;
398  cd_.LensIsSpherical_ = false;
399  cd_.zoomCamera_=false;
400  cd_.kc1Def_ = k1; cd_.kc2Def_ = k2; cd_.kc3Def_ = k3; cd_.kc4Def_ = k4;
401  cd_.IlluCorrX_ = IlluCorrX;
402  cd_.IlluCorrY_ = IlluCorrY;
403 }
404 
405 void CameraParam::DebugOut() const {
406  cout << "Camera Parameters:" << endl;
407  cout << "CamModel_ = " << cd_.CamModel_ << endl;
408  cout << "CamID_ = " << cd_.CamID_ << endl;
409  cout << "LensModel_ = " << cd_.LensModel_ << endl;
410  cout << "LensIsSpherical_ = " << cd_.LensIsSpherical_ << endl;
411  cout << "zoomCamera_ = " << cd_.zoomCamera_ << endl;
412  cout << "width_ = " << cd_.width_ << endl;
413  cout << "height_ = " << cd_.height_ << endl;
414  for (int i=0; i<(int)cd_.knownparams_vect_.size();i++) {
415  cout << "knownparams_vect_[" << i << "] = " << endl;
416  CPDiscreteParam dp;
417  dp=cd_.knownparams_vect_[i];
418  cout << " zoom = " << dp.zoom << endl;
419  cout << " focallength = " << dp.focallength << endl;
420  cout << " kc1 = " << dp.kc1 << endl;
421  cout << " kc2 = " << dp.kc2 << endl;
422  cout << " kc3 = " << dp.kc3 << endl;
423  cout << " kc4 = " << dp.kc4 << endl;
424  };
425  for (int i=0; i<(int) cd_.AngleCorrX_.size();i++) {
426  cout << "AngleCorrX_[" << i << "] = " << cd_.AngleCorrX_[i] << endl;
427  }
428  for (int i=0; i<(int) cd_.AngleCorrY_.size();i++) {
429  cout << "AngleCorrY_[" << i << "] = " << cd_.AngleCorrY_[i] << endl;
430  }
431  for (int i=0; i<(int) cd_.IlluCorrX_.size();i++) {
432  cout << "IlluCorrX_[" << i << "] = " << cd_.IlluCorrX_[i] << endl;
433  }
434  for (int i=0; i<(int) cd_.IlluCorrY_.size();i++) {
435  cout << "IlluCorrY_[" << i << "] = " << cd_.IlluCorrY_[i] << endl;
436  }
437 
438  cout << "aspectratio_ = " << cd_.aspectratio_ << endl;
439  cout << "cellSizeX_ = " << cd_.cellSizeX_ << endl;
440  cout << "cellSizeY_ = " << cd_.cellSizeY_ << endl;
441  cout << "principalX_ = " << cd_.principalX_ << endl;
442  cout << "principalY_ = " << cd_.principalY_ << endl;
443  cout << "focallengthDef_ = " << cd_.focallengthDef_ << endl;
444  cout << "kc1Def_ = " << cd_.kc1Def_ << endl;
445  cout << "kc2Def_ = " << cd_.kc2Def_ << endl;
446  cout << "kc3Def_ = " << cd_.kc3Def_ << endl;
447  cout << "kc4Def_ = " << cd_.kc4Def_ << endl;
448  cout << "downsamplingFactor_ = " << downsamplingFactor_ << endl;
449  cout << endl << endl;
450 }
451 
452 
453 int CameraParam::ReadFromEXIF(const std::string& filename) {
454  CameraData cd;
455  int res;
456  res=ProjectionParametersIO::ReadFromEXIF(filename, cd);
457  SetCameraData(cd);
458  return res;
459 }
460 
461 
462 CameraParam::CameraParam(const KMatrix& K, const int w, const int h) {
463  CameraData cd;
465  SetCameraData(cd);
466 }
467 
468 
470  CameraData cd;
472  SetCameraData(cd);
473 }
474 
475 
477  CameraData cd;
479  SetCameraData(cd);
480 }
481 
482 
484 
485  double relnorm;
486  double absdist;
487 
488  bool found;
489  unsigned int i;
490  found=false;
491  if (cd_.knownparams_vect_[0].zoom<zoom_){ // zoom is higher than the
492  // known values:
493  zoom_=cd_.knownparams_vect_[0].zoom; // set to highest available
494  }
495 
496  for (i=0;i<cd_.knownparams_vect_.size();i++){
497  if (cd_.knownparams_vect_[i].zoom<=zoom_){
498  found=true;
499  break;
500  }
501  }
502 
503  if (!found){ // zoom is lower than the known values
504  focallengthZoom_=cd_.knownparams_vect_[i-1].focallength;
505  return cd_.knownparams_vect_[i-1].focallength;
506  }
507 
508 
509  if (cd_.knownparams_vect_[i].zoom==zoom_){
510  return cd_.knownparams_vect_[i].focallength;
511  }
512  relnorm=(zoom_ - cd_.knownparams_vect_[i].zoom) /
513  (cd_.knownparams_vect_[i-1].zoom - cd_.knownparams_vect_[i].zoom);
514 
515  relnorm=1.0-relnorm;
516  absdist=relnorm*(cd_.knownparams_vect_[i].focallength-
517  cd_.knownparams_vect_[i-1].focallength);
518 
519  return (absdist+cd_.knownparams_vect_[i-1].focallength);
520 }
521 
522 
524 
525  double relnorm;
526  double absdist;
527  bool found;
528  unsigned int i;
529  found=false;
530 
531  if (cd_.knownparams_vect_[0].focallength<focallengthZoom_){
532  focallengthZoom_=cd_.knownparams_vect_[0].focallength;
533  }
534 
535  for (i=0;i<cd_.knownparams_vect_.size();i++){
536  if (cd_.knownparams_vect_[i].focallength<=focallengthZoom_){
537  found=true;
538  break;
539  }
540  }
541  if (!found){
542  return -1;
543  }
544 
545  if (cd_.knownparams_vect_[i].focallength==focallengthZoom_){
546  // interpolate distortion parameters
547  return cd_.knownparams_vect_[i].zoom;
548  }
549 
550  relnorm=(focallengthZoom_ - cd_.knownparams_vect_[i].focallength) /
551  (cd_.knownparams_vect_[i-1].focallength -
552  cd_.knownparams_vect_[i].focallength);
553 
554  relnorm=1.0-relnorm;
555 
556  absdist=relnorm*(cd_.knownparams_vect_[i].zoom-
557  cd_.knownparams_vect_[i-1].zoom);
558 
559  return (absdist+cd_.knownparams_vect_[i-1].zoom);
560 }
double InterpolateAbsZoom_()
void InitSpherical(const std::string CamModel, const int CamID, const std::string LensModel, const int imgWidth, const int imgHeight, const double principalX, const double principalY, const double radius, const std::vector< double > &AngleCorrX, const std::vector< double > &AngleCorrY, const std::vector< double > &IlluCorrX, const std::vector< double > &IlluCorrY)
static int ReadCameraData(const std::string &Filename, CameraData &cd, const bool silent=false)
Read a camera data parameter file and store the data in (*this)
unsigned int GetHeight() const
get camera image height.
static int WriteCameraData(const std::string &Filename, const CameraData &cd)
Write all data to a camera data parameter file )
double InterpolateFocalLength_()
double GetPrincipalY() const
get principal point, y in pixels relative to top left corner
double downsamplingFactor_
Definition: CameraParam.hh:296
void SetFocalLength(const double f)
Set the current camera focal length, the zoom will be calculated.
int ReadFromEXIF(const std::string &filename)
read jpeg EXIF header and set camera parameter
static int SetFromKMatrix(const BIAS::KMatrix &K, const int w, const int h, CameraData &cd)
pass K and image dims to construct a simple parameter file
void DebugOut() const
static void SetData_EVID31_147094(CameraData &cd)
void SetPrincipal(const double x, const double y)
Set principal point in pixels relative to top left corner.
double GetZoom() const
Set the current camera zoom, the focal length will be calculated.
unsigned int GetWidth() const
get camera image width.
void SetZoom(const double abszoom)
Set the current camera zoom, the focal length will be calculated.
static int ReadFromEXIF(const std::string &filename, CameraData &cd)
read jpeg EXIF header and set camera parameter
bool IsZoomCamera() const
True, if camera has a zoom lens.
enum BIAS::LensModel_e LensModel
double GetFocalLength() const
Get the current camera focal length.
void SetUnzoomed()
Set the zoom and focal length of a zoom camera to the default settings.
void SetData_OMNITECH_ORIFL_190_3()
int ReadCameraData(const std::string &Filename)
Add a camera sub-tree-structure to an allready created XML-tree and write the data from (*this) into ...
BIAS::KMatrix GetKMatrix() const
double GetDownsamplingFactor()
K describes the mapping from world coordinates (wcs) to pixel coordinates (pcs).
Definition: KMatrix.hh:48
void InitPerspective(const std::string CamModel, const int CamID, const std::string LensModel, const int imgWidth, const int imgHeight, const double principalX, const double principalY, const double focallength, const double aspectratio, const double k1, const double k2, const double k3, const double k4, const std::vector< double > &IlluCorrX, const std::vector< double > &IlluCorrY)
const CameraData GetCameraData() const
Definition: CameraParam.hh:284
int WriteCameraData(const std::string &Filename) const
Write all data to a camera data parameter file )
void SetZoomCamera(const bool zoomCam)
Set to &#39;true&#39;, if camera has a zoom lens.
void SetDownsampling(const double factor)
Set downsampling factor - all get functions will return transformed values.
double GetPrincipalX() const
get principal point, x in pixels relative to top left corner
void SetData_EVID31_147094()
Read a camera data form a precreated XML-tree and store the data in (*this) - called from the rig par...
void SetCellSize(const double CellSizeX, const double AspectRatio)
Set CCD cell-size in meter.
static void SetData_OMNITECH_ORIFL_190_3(CameraData &cd)
void SetIdentity()
set the elements of this matrix to the identity matrix (possibly overriding the inherited method) ...
Definition: Matrix3x3.hh:429