detect_calibration_pattern.cpp
Go to the documentation of this file.
2 
3 void PatternDetector::setCameraMatrices(cv::Matx33d K_, cv::Matx33d D_)
4 {
5  K = K_;
6  D = D_;
7 }
8 
9 void PatternDetector::setPattern(cv::Size grid_size_, float square_size_,
10  Pattern pattern_type_, cv::Point3f offset_)
11 {
12  ideal_points = calcChessboardCorners(grid_size_, square_size_, pattern_type_, offset_);
13  pattern_type = pattern_type_;
14  grid_size = grid_size_;
15  square_size = square_size_;
16 }
17 
19  float squareSize,
20  Pattern patternType,
21  cv::Point3f offset)
22 {
23  object_pts_t corners;
24  switch (patternType)
25  {
26  case CHESSBOARD:
27  case CIRCLES_GRID:
28  for (int i = 0; i < boardSize.height; i++)
29  for (int j = 0; j < boardSize.width; j++)
30  corners.push_back(
31  cv::Point3f(float(j * squareSize),
32  float(i * squareSize), 0) + offset);
33  break;
35  for (int i = 0; i < boardSize.height; i++)
36  for (int j = 0; j < boardSize.width; j++)
37  corners.push_back(
38  cv::Point3f(float(i * squareSize),
39  float((2 * j + i % 2) * squareSize), 0) + offset);
40  break;
41  default:
42  std::logic_error("Unknown pattern type.");
43  }
44  return corners;
45 }
46 
47 
48 
49 int PatternDetector::detectPattern(cv::Mat& inm, Eigen::Vector3f& translation, Eigen::Quaternionf& orientation)
50 {
51  translation.setZero();
52  orientation.setIdentity();
53 
54  bool found = false;
55 
56  observation_pts_t observation_points;
57 
58  switch (pattern_type)
59  {
61  found = cv::findCirclesGrid(inm, grid_size, observation_points,
62  cv::CALIB_CB_ASYMMETRIC_GRID | cv::CALIB_CB_CLUSTERING);
63  break;
64  case CHESSBOARD:
65  found = cv::findChessboardCorners(inm, grid_size, observation_points, cv::CALIB_CB_ADAPTIVE_THRESH);
66  break;
67  case CIRCLES_GRID:
68  found = cv::findCirclesGrid(inm, grid_size, observation_points, cv::CALIB_CB_SYMMETRIC_GRID);
69  break;
70  }
71 
72  if(found)
73  {
74  // Do subpixel ONLY IF THE PATTERN IS A CHESSBOARD
75  if (pattern_type == CHESSBOARD)
76  {
77  cv::cornerSubPix(inm, observation_points, cv::Size(5,5), cv::Size(-1,-1),
78  cv::TermCriteria(cv::TermCriteria::MAX_ITER + cv::TermCriteria::EPS, 100, 0.01));
79  }
80 
81  cv::solvePnP(cv::Mat(ideal_points), cv::Mat(observation_points), K, D,
82  rvec, tvec, false);
83  cv::Rodrigues(rvec, R); //take the 3x1 rotation representation to a 3x3 rotation matrix.
84 
85  cv::drawChessboardCorners(inm, grid_size, cv::Mat(observation_points), found);
86 
87  convertCVtoEigen(tvec, R, translation, orientation);
88  }
89 
90  return found;
91 }
92 
93 void convertCVtoEigen(cv::Mat& tvec, cv::Mat& R, Eigen::Vector3f& translation, Eigen::Quaternionf& orientation)
94 {
95  // This assumes that cv::Mats are stored as doubles. Is there a way to check this?
96  // Since it's templated...
97  translation = Eigen::Vector3f(tvec.at<double>(0,0), tvec.at<double>(0,1), tvec.at<double>(0, 2));
98 
99  Eigen::Matrix3f Rmat;
100  Rmat << R.at<double>(0,0), R.at<double>(0,1), R.at<double>(0,2),
101  R.at<double>(1,0), R.at<double>(1,1), R.at<double>(1,2),
102  R.at<double>(2,0), R.at<double>(2,1), R.at<double>(2,2);
103 
104  orientation = Eigen::Quaternionf(Rmat);
105 
106 }
107 
void convertCVtoEigen(cv::Mat &tvec, cv::Mat &R, Eigen::Vector3f &translation, Eigen::Quaternionf &orientation)
void setPattern(cv::Size grid_size_, float square_size_, Pattern pattern_type_, cv::Point3f offset_=cv::Point3f())
GLintptr offset
void setCameraMatrices(cv::Matx33d K_, cv::Matx33d D_)
std::vector< cv::Point3f > object_pts_t
std::vector< cv::Point2f > observation_pts_t
static object_pts_t calcChessboardCorners(cv::Size boardSize, float squareSize, Pattern patternType=CHESSBOARD, cv::Point3f offset=cv::Point3f())
int detectPattern(cv::Mat &inm, Eigen::Vector3f &translation, Eigen::Quaternionf &orientation)


turtlebot_actions
Author(s): Helen Oleynikova, Melonee Wise
autogenerated on Mon Jun 10 2019 15:43:57