detect_calibration_pattern.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
31 
32 void PatternDetector::setCameraMatrices(cv::Matx33d K_, cv::Mat D_)
33 {
34  K = K_;
35  D = D_;
36 }
37 
38 void PatternDetector::setPattern(cv::Size grid_size_, float square_size_, Pattern pattern_type_, cv::Point3f offset_)
39 {
40  ideal_points = calcChessboardCorners(grid_size_, square_size_, pattern_type_, offset_);
41  pattern_type = pattern_type_;
42  grid_size = grid_size_;
43  square_size = square_size_;
44 }
45 
46 object_pts_t PatternDetector::calcChessboardCorners(cv::Size boardSize, float squareSize, Pattern patternType,
47  cv::Point3f offset)
48 {
49  object_pts_t corners;
50  switch (patternType)
51  {
52  case CHESSBOARD:
53  case CIRCLES_GRID:
54  for (int i = 0; i < boardSize.height; i++)
55  for (int j = 0; j < boardSize.width; j++)
56  corners.push_back(cv::Point3f(float(j * squareSize), float(i * squareSize), 0) + offset);
57  break;
59  for (int i = 0; i < boardSize.height; i++)
60  for (int j = 0; j < boardSize.width; j++)
61  corners.push_back(cv::Point3f(float(i * squareSize), float((2 * j + i % 2) * squareSize), 0) + offset);
62  break;
63  default:
64  std::logic_error("Unknown pattern type.");
65  }
66  return corners;
67 }
68 
69 int PatternDetector::detectPattern(cv::Mat& image_in, Eigen::Vector3f& translation, Eigen::Quaternionf& orientation,
70  cv::Mat& image_out)
71 {
72  translation.setZero();
73  orientation.setIdentity();
74 
75  bool found = false;
76 
77  observation_pts_t observation_points;
78 
79  switch (pattern_type)
80  {
82  found = cv::findCirclesGrid(image_in, grid_size, observation_points,
83  cv::CALIB_CB_ASYMMETRIC_GRID | cv::CALIB_CB_CLUSTERING);
84  break;
85  case CHESSBOARD:
86  found = cv::findChessboardCorners(image_in, grid_size, observation_points, cv::CALIB_CB_ADAPTIVE_THRESH);
87  break;
88  case CIRCLES_GRID:
89  found = cv::findCirclesGrid(image_in, grid_size, observation_points, cv::CALIB_CB_SYMMETRIC_GRID);
90  break;
91  }
92 
93  if (found)
94  {
95  // Do subpixel ONLY IF THE PATTERN IS A CHESSBOARD
96  if (pattern_type == CHESSBOARD)
97  {
98  cv::cornerSubPix(image_in, observation_points, cv::Size(5, 5), cv::Size(-1, -1),
99  cv::TermCriteria(cv::TermCriteria::MAX_ITER + cv::TermCriteria::EPS, 100, 0.01));
100  }
101 
102  cv::solvePnP(cv::Mat(ideal_points), cv::Mat(observation_points), K, D, rvec, tvec, false);
103  cv::Rodrigues(rvec, R); //take the 3x1 rotation representation to a 3x3 rotation matrix.
104 
105  cv::drawChessboardCorners(image_out, grid_size, cv::Mat(observation_points), found);
106 
107  convertCVtoEigen(tvec, R, translation, orientation);
108  }
109 
110  return found;
111 }
112 
113 void convertCVtoEigen(cv::Mat& tvec, cv::Mat& R, Eigen::Vector3f& translation, Eigen::Quaternionf& orientation)
114 {
115  // This assumes that cv::Mats are stored as doubles. Is there a way to check this?
116  // Since it's templated...
117  translation = Eigen::Vector3f(tvec.at<double>(0, 0), tvec.at<double>(0, 1), tvec.at<double>(0, 2));
118 
119  Eigen::Matrix3f Rmat;
120  Rmat << R.at<double>(0, 0), R.at<double>(0, 1), R.at<double>(0, 2),
121  R.at<double>(1, 0), R.at<double>(1, 1), R.at<double>(1, 2),
122  R.at<double>(2, 0), R.at<double>(2, 1), R.at<double>(2, 2);
123 
124  orientation = Eigen::Quaternionf(Rmat);
125 }
void convertCVtoEigen(cv::Mat &tvec, cv::Mat &R, Eigen::Vector3f &translation, Eigen::Quaternionf &orientation)
void setCameraMatrices(cv::Matx33d K_, cv::Mat D_)
void setPattern(cv::Size grid_size_, float square_size_, Pattern pattern_type_, cv::Point3f offset_=cv::Point3f())
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 &image_in, Eigen::Vector3f &translation, Eigen::Quaternionf &orientation, cv::Mat &image_out)


turtlebot_arm_kinect_calibration
Author(s): Michael Ferguson, Helen Oleynikova
autogenerated on Fri Feb 7 2020 03:56:29