3DiVi Face SDK  3.24.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups
CameraCalibrator.h
Go to the documentation of this file.
1 
9 #ifndef __PBIO_API__PBIO__CAMERACALIBRATOR_H_
10 #define __PBIO_API__PBIO__CAMERACALIBRATOR_H_
11 
12 #include <sstream>
13 #include <vector>
14 
15 
16 #include "ComplexObject.h"
17 #include "Error.h"
18 #include "ExceptionCheck.h"
19 #include "Image.h"
20 #include "Point.h"
21 #include "RawImage.h"
22 #include "SmartPtr.h"
23 #include "stl_wraps_impls/WrapIStreamImpl.h"
24 #include "stl_wraps_impls/WrapOStreamImpl.h"
25 
26 
27 
28 
29 namespace pbio
30 {
31 
32 class FacerecService;
33 
39 class CameraCalibrator : public ComplexObject
40 {
41 public:
42 
48  typedef LightSmartPtr<CameraCalibrator>::tPtr Ptr;
49 
50 
57  {
64  {
68  };
69 
76 
83 
90 
97 
104 
111 
118 
125 
132 
134  {
135  fix_aspect_ratio = false;
136  calib_zero_tangent_dist = false;
139  }
140  };
141 
142 
160  void initCalibration(
161  const CalibrationSettings settings);
162 
163 
203  void addImage(
204  const RawImage image,
205  bool *pattern_found,
206  bool *pattern_accepted,
207  std::vector<Point> *pattern_points);
208 
209 
225  float getPatternSpaceCoverProgress() const;
226 
227 
243  std::vector<Point> getTip();
244 
245 
279  bool calibrate(
280  const int max_used_patterns_count,
281  float *reprojection_error);
282 
283 
322  const RawImage image,
323  const float alpha) const;
324 
325 
326 
347  std::ostream &binary_stream) const;
348 
368  pbio::stl_wraps::WrapOStream &binary_stream) const;
369 
370 
391  std::istream &binary_stream);
392 
413  pbio::stl_wraps::WrapIStream &binary_stream);
414 
415 
416 private:
417 
419  const DHPtr &dll_handle,
420  void* impl);
421 
422  int _pattern_width;
423  int _pattern_height;
424 
425  friend class FacerecService;
426  friend class object_with_ref_counter<CameraCalibrator>;
427 };
428 
429 } // pbio namespace
430 
431 
432 
436 
437 namespace pbio
438 {
439 
440 inline
441 CameraCalibrator::CameraCalibrator(
442  const DHPtr &dll_handle,
443  void* impl):
444 ComplexObject(dll_handle, impl)
445 {
446  _pattern_width = 0;
447  _pattern_height = 0;
448 }
449 
450 
451 inline
453  const CalibrationSettings settings)
454 {
455  _pattern_height = 0;
456  _pattern_width = 0;
457 
458  void* exception = NULL;
459 
460  _dll_handle->CameraCalibrator_initCalibration(
461  _impl,
462 
463  settings.image_width,
464  settings.image_height,
465  settings.pattern_type,
466  settings.pattern_width,
467  settings.pattern_height,
468 
469  settings.fix_aspect_ratio,
470  settings.calib_zero_tangent_dist,
471  settings.calib_fix_principal_point,
472  settings.rational_distortion_model,
473 
474  &exception);
475 
476  checkException(exception, *_dll_handle);
477 
478  _pattern_height = settings.pattern_height;
479  _pattern_width = settings.pattern_width;
480 }
481 
482 
483 inline
485  const RawImage image,
486  bool *pattern_found,
487  bool *pattern_accepted,
488  std::vector<Point> *pattern_points)
489 {
490 
491  int32_t pattern_found_i = 0;
492  int32_t pattern_accepted_i = 0;
493 
494  std::vector<float> pattern_points_f;
495 
496  if(_pattern_height > 0 && _pattern_width > 0)
497  pattern_points_f.resize(_pattern_height * _pattern_width * 2);
498 
499  void* exception = NULL;
500 
501  const RawImage::CapiData cdata = image.makeCapiData();
502 
503  _dll_handle->CameraCalibrator_addImage_with_crop(
504  _impl,
505 
506  cdata.data,
507  cdata.width,
508  cdata.height,
509  cdata.format,
510 
511  cdata.with_crop,
512  cdata.crop_info_offset_x,
513  cdata.crop_info_offset_y,
514  cdata.crop_info_data_image_width,
515  cdata.crop_info_data_image_height,
516 
517  &pattern_found_i,
518  &pattern_accepted_i,
519  pattern_points_f.empty() ?
520  NULL :
521  &pattern_points_f[0],
522  &exception);
523 
524  checkException(exception, *_dll_handle);
525 
526 
527  if(pattern_found)
528  *pattern_found = pattern_found_i;
529 
530  if(pattern_accepted)
531  *pattern_accepted = pattern_accepted_i;
532 
533  if(pattern_points)
534  {
535  pattern_points->resize(_pattern_width * _pattern_height);
536  for(size_t i = 0; i < pattern_points->size(); ++i)
537  {
538  pattern_points->at(i).x = pattern_points_f[i * 2 + 0];
539  pattern_points->at(i).y = pattern_points_f[i * 2 + 1];
540  }
541  }
542 }
543 
544 
545 inline
547 {
548  void* exception = NULL;
549 
550  const float progress = _dll_handle->CameraCalibrator_getPatternSpaceCoverProgress(
551  _impl,
552  &exception);
553 
554  checkException(exception, *_dll_handle);
555 
556  return progress;
557 }
558 
559 
560 inline
561 std::vector<Point> CameraCalibrator::getTip()
562 {
563  std::vector<float> pattern_points_f;
564 
565  if(_pattern_height > 0 && _pattern_width > 0)
566  pattern_points_f.resize(_pattern_height * _pattern_width * 2);
567 
568  void* exception = NULL;
569 
570  const int32_t tip_ready = _dll_handle->CameraCalibrator_getTip(
571  _impl,
572  pattern_points_f.empty() ?
573  NULL :
574  &pattern_points_f[0],
575  &exception);
576 
577  checkException(exception, *_dll_handle);
578 
579  std::vector<Point> pattern_points;
580 
581  if(tip_ready)
582  {
583  pattern_points.resize(_pattern_width * _pattern_height);
584 
585  for(size_t i = 0; i < pattern_points.size(); ++i)
586  {
587  pattern_points[i].x = pattern_points_f[i * 2 + 0];
588  pattern_points[i].y = pattern_points_f[i * 2 + 1];
589  }
590  }
591 
592  return pattern_points;
593 }
594 
595 
596 
597 inline
599  const int max_used_patterns_count,
600  float *reprojection_error_ptr)
601 {
602  void* exception = NULL;
603 
604  const float reprojection_error = _dll_handle->CameraCalibrator_calibrate(
605  _impl,
606  max_used_patterns_count,
607  &exception);
608 
609  checkException(exception, *_dll_handle);
610 
611  if(reprojection_error < 0 - 0.1f)
612  return false;
613 
614  if(reprojection_error_ptr)
615  *reprojection_error_ptr = reprojection_error;
616 
617  return true;
618 }
619 
620 
621 inline
623  const RawImage image,
624  const float alpha) const
625 {
626  pbio::image::Image::Ptr result(
627  new pbio::image::Image(
628  image.height,
629  image.width,
630  image.format));
631 
632  void* exception = NULL;
633 
634  const RawImage::CapiData cdata = image.makeCapiData();
635 
636  _dll_handle->CameraCalibrator_undistort_with_crop(
637  _impl,
638 
639  cdata.data,
640  cdata.width,
641  cdata.height,
642  cdata.format,
643 
644  cdata.with_crop,
645  cdata.crop_info_offset_x,
646  cdata.crop_info_offset_y,
647  cdata.crop_info_data_image_width,
648  cdata.crop_info_data_image_height,
649 
650  alpha,
651  result->data(),
652  &exception);
653 
654  checkException(exception, *_dll_handle);
655 
656  return result;
657 }
658 
659 
660 
661 inline
663  std::ostream &binary_stream) const
664 {
665  pbio::stl_wraps::WrapOStreamImpl binary_stream_wrap(binary_stream);
666 
667  saveCameraParameters(binary_stream_wrap);
668 }
669 
670 inline
672  pbio::stl_wraps::WrapOStream &binary_stream) const
673 {
674  void* exception = NULL;
675 
676  _dll_handle->CameraCalibrator_saveCameraParameters(
677  _impl,
678  &binary_stream,
679  pbio::stl_wraps::WrapOStream::write_func,
680  &exception);
681 
682  checkException(exception, *_dll_handle);
683 }
684 
685 
686 inline
688  std::istream &binary_stream)
689 {
690  pbio::stl_wraps::WrapIStreamImpl binary_stream_wrap(binary_stream);
691 
692  loadCameraParameters(binary_stream_wrap);
693 }
694 
695 inline
697  pbio::stl_wraps::WrapIStream &binary_stream)
698 {
699  void* exception = NULL;
700 
701  _dll_handle->CameraCalibrator_loadCameraParameters(
702  _impl,
703  &binary_stream,
704  pbio::stl_wraps::WrapIStream::read_func,
705  &exception);
706 
707  checkException(exception, *_dll_handle);
708 }
709 
710 
711 } // pbio namespace
712 
713 #endif // __PBIO_API__PBIO__CAMERACALIBRATOR_H_
HeavySmartPtr< IRawImage >::tPtr Ptr
Alias for the type of a smart pointer to IRawImage.
Definition: IRawImage.h:39
int image_height
Height of images for calibration.
Definition: CameraCalibrator.h:82
Struct that provides raw image data and optional cropping information.
Definition: RawImage.h:28
Interface object for creating other interface objects.
Definition: FacerecService.h:64
PatternType pattern_type
Pattern type.
Definition: CameraCalibrator.h:89
LightSmartPtr< CameraCalibrator >::tPtr Ptr
Alias for the type of a smart pointer to CameraCalibrator.
Definition: CameraCalibrator.h:48
std::vector< Point > getTip()
Get a tip where to put a pattern in the image to get a better pattern space coverage.
Definition: CameraCalibrator.h:561
int width
Image width.
Definition: RawImage.h:109
Сalibration process settings.
Definition: CameraCalibrator.h:56
Format format
Format of image data.
Definition: RawImage.h:125
Error - the class of exceptions thrown when errors occur.
bool fix_aspect_ratio
Fix the aspect ratio.
Definition: CameraCalibrator.h:110
int pattern_width
Pattern width.
Definition: CameraCalibrator.h:96
bool calib_fix_principal_point
Fix the principal point at the center.
Definition: CameraCalibrator.h:124
void addImage(const RawImage image, bool *pattern_found, bool *pattern_accepted, std::vector< Point > *pattern_points)
Search a pattern in the image and store it.
Definition: CameraCalibrator.h:484
IRawImage::Ptr undistort(const RawImage image, const float alpha) const
Correct the image distortion using the calibrated or loaded camera parameters.
Definition: CameraCalibrator.h:622
bool calib_zero_tangent_dist
Consider a tangential distortion as zero.
Definition: CameraCalibrator.h:117
int height
Image height.
Definition: RawImage.h:117
int pattern_height
Pattern height.
Definition: CameraCalibrator.h:103
void loadCameraParameters(std::istream &binary_stream)
Load the calibrated camera parameters from binary_stream. The format is platform-independent.
Definition: CameraCalibrator.h:687
Interface object for camera calibration and correction of image distortion.
Definition: CameraCalibrator.h:39
SmartPtr.
int image_width
Width of images for calibration.
Definition: CameraCalibrator.h:75
float getPatternSpaceCoverProgress() const
Evaluate covering of the pattern space to get more accurate calibration.
Definition: CameraCalibrator.h:546
Definition: FacerecService.mm:79
bool rational_distortion_model
Use the rational distortion model.
Definition: CameraCalibrator.h:131
PatternType
Types of calibration patterns.
Definition: CameraCalibrator.h:63
void initCalibration(const CalibrationSettings settings)
Initialize the calibration process. All previous accepted patterns will be removed.
Definition: CameraCalibrator.h:452
bool calibrate(const int max_used_patterns_count, float *reprojection_error)
Calibrate the camera.
Definition: CameraCalibrator.h:598
void saveCameraParameters(std::ostream &binary_stream) const
Save the calibrated camera parameters to binary_stream. The format is platform-independent.
Definition: CameraCalibrator.h:662