stereo-vision
Public Member Functions
Camera Class Reference

The base class defining a simple camera. More...

#include <camera.h>

Public Member Functions

 Camera (string intrinsicFilePath)
 Costructor for initialization from file. More...
 
 Camera ()
 Default Constructor. More...
 
void calibrate (vector< string > imageList, int boardWidth, int boardHeight)
 It performs the camera calibration. More...
 
bool saveCalibration (string intrinsicFilePath)
 It saves the calibration. More...
 
Mat undistortImage (Mat image)
 It undistorts an image using the intrinsic parameters. More...
 
Mat getCameraMatrix ()
 It returns the 3x3 camera matrix. More...
 
Mat getDistVector ()
 It returns the 4x1 distortion coefficients. More...
 
void setCameraMatrix (Mat &K)
 It sets the camera parameters. More...
 
void setDistCoefficients (Mat &Dist)
 It sets the distortion parameters. More...
 

Detailed Description

The base class defining a simple camera.

It allows to calibrate the camera and to undistort a pair of images.

Definition at line 40 of file camera.h.

Constructor & Destructor Documentation

◆ Camera() [1/2]

Camera::Camera ( string  intrinsicFilePath)

Costructor for initialization from file.

Parameters
intrisicFileNamethe path of the intrinsic parameters file.

Definition at line 25 of file camera.cpp.

25  {
26  FileStorage fs(intrinsicFilePath.c_str(), FileStorage::READ);
27  if(!fs.isOpened())
28  {
29  printf("Failed to open file %s\n", intrinsicFilePath.c_str());
30  return;
31  }
32  fs["camera_matrix"] >> K;
33  fs["distortion_coefficients"] >> Dist;
34 
35 }

◆ Camera() [2/2]

Camera::Camera ( )
inline

Default Constructor.

You should initialize all the intrinsic and extrinsic parameters using the calibrate method

Definition at line 82 of file camera.h.

82 {};

Member Function Documentation

◆ calibrate()

void Camera::calibrate ( vector< string >  imageList,
int  boardWidth,
int  boardHeight 
)

It performs the camera calibration.

Parameters
imageListis the list containing the paths of the images with the chessboard patterns.
boardWidththe number of inner corners in the width direction of the chess board pattern (see stereoCalibration module)
boardHeightthe number of inner corners in the height direction of the chess board pattern (see stereoCalibration module)

Definition at line 53 of file camera.cpp.

53  {
54  vector<vector<Point2f> > imagePoints;
55  Size boardSize, imageSize;
56  boardSize.width=boardWidth;
57  boardSize.height=boardHeight;
58  int flags=0;
59  int i;
60 
61  float squareSize = 1.f, aspectRatio = 1.f;
62 
63  Mat view, viewGray;
64 
65  for(i = 0; i<(int)imageList.size();i++)
66  {
67 
68  view = cv::imread(imageList[i], 1);
69  imageSize = view.size();
70  vector<Point2f> pointbuf;
71  cvtColor(view, viewGray, cv::COLOR_BGR2GRAY);
72 
73  bool found = findChessboardCorners( view, boardSize, pointbuf,
74  cv::CALIB_CB_ADAPTIVE_THRESH + cv::CALIB_CB_FAST_CHECK + cv::CALIB_CB_NORMALIZE_IMAGE);
75 
76  if(found)
77  {
78  drawChessboardCorners( view, boardSize, Mat(pointbuf), found );
79  imagePoints.push_back(pointbuf);
80  }
81 
82  }
83  prepareandRunCalibration(imagePoints, imageSize, boardSize, squareSize, aspectRatio, flags, K, Dist);
84 
85 
86 }

◆ getCameraMatrix()

Mat Camera::getCameraMatrix ( )

It returns the 3x3 camera matrix.

Returns
3x3 camera matrix.

Definition at line 264 of file camera.cpp.

264  {
265  return this->K;
266 }

Referenced by StereoCamera::StereoCamera().

◆ getDistVector()

Mat Camera::getDistVector ( )

It returns the 4x1 distortion coefficients.

Returns
4x1 distortion coefficients.

Definition at line 267 of file camera.cpp.

267  {
268  return this->Dist;
269 }

Referenced by StereoCamera::StereoCamera().

◆ saveCalibration()

bool Camera::saveCalibration ( string  intrinsicFilePath)

It saves the calibration.

Parameters
intrinsicFilePaththe path of the intrinsic parameters file

Definition at line 237 of file camera.cpp.

237  {
238 
239  if(this->K.empty()) {
240  printf("Camera is not calibrated, run calibration method before..\n");
241  return false;
242  }
243  FileStorage fs( intrinsicFilePath, FileStorage::WRITE );
244  fs << "camera_matrix" << this->K;
245  fs << "distortion_coefficients" << this->Dist;
246  return true;
247 }

◆ setCameraMatrix()

void Camera::setCameraMatrix ( Mat &  K)

It sets the camera parameters.

Parameters
K3x3 camera matrix.

Definition at line 277 of file camera.cpp.

277  {
278  this->K=K;
279 }

◆ setDistCoefficients()

void Camera::setDistCoefficients ( Mat &  Dist)

It sets the distortion parameters.

Parameters
D4x1 camera distortion parameters.

Definition at line 281 of file camera.cpp.

281  {
282  this->Dist=Dist;
283 }

◆ undistortImage()

Mat Camera::undistortImage ( Mat  image)

It undistorts an image using the intrinsic parameters.

Note
Set undistortion coefficients and camera matrix before using this method.
Parameters
imagethe input distorted image.
Returns
the undistorted image.

Definition at line 271 of file camera.cpp.

271  {
272  Mat imund;
273  undistort(image,imund,K,Dist);
274  return imund;
275 }

The documentation for this class was generated from the following files: