MRPT  1.9.9
checkerboard_find_corners.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "vision-precomp.h" // Precompiled headers
11 
12 #include <mrpt/math/geometry.h> // crossProduct3D()
14 
15 // Universal include for all versions of OpenCV
16 #include <mrpt/3rdparty/do_opencv_includes.h>
18 
19 using namespace mrpt;
20 using namespace mrpt::vision;
21 using namespace mrpt::img;
22 using namespace mrpt::math;
23 using namespace std;
24 
25 /** Look for the corners of a chessboard in the image
26  * \param cornerCoords [OUT] The pixel coordinates of all the corners.
27  * \param check_size_x [IN] The number of squares, in the X direction
28  * \param check_size_y [IN] The number of squares, in the Y direction
29  * \param normalize_image [IN] Whether to normalize the image before detection
30  * \param useScaramuzzaMethod [IN] Whether to use the alternative, more robust
31  * method by M. Rufli, D. Scaramuzza, and R. Siegwart.
32  *
33  * \return true on success
34  */
36  const mrpt::img::CImage& in_img, std::vector<TPixelCoordf>& cornerCoords,
37  unsigned int check_size_x, unsigned int check_size_y, bool normalize_image,
38  bool useScaramuzzaMethod)
39 {
40 #if MRPT_HAS_OPENCV
42 
43  ASSERT_(check_size_y > 0 && check_size_x > 0);
44 
45  // Grayscale version:
46  const CImage img(in_img, FAST_REF_OR_CONVERT_TO_GRAY);
47 
48  // Try with expanded versions of the image if it fails to detect the
49  // checkerboard:
50  int corners_count;
51  bool corners_found = false;
52 
53  const CvSize check_size = cvSize(check_size_x, check_size_y);
54 
55  const int CORNERS_COUNT = check_size_x * check_size_y;
56 
57  vector<CvPoint2D32f> corners_list;
58  corners_count = CORNERS_COUNT;
59  corners_list.resize(CORNERS_COUNT);
60 
61  cornerCoords.clear();
62 
63  int find_chess_flags = cv::CALIB_CB_ADAPTIVE_THRESH;
64  if (normalize_image) find_chess_flags |= cv::CALIB_CB_NORMALIZE_IMAGE;
65 
66  cv::Mat cvImg = img.asCvMat<cv::Mat>(SHALLOW_COPY);
67  if (!useScaramuzzaMethod)
68  {
69  vector<cv::Point2f> pointbuf;
70 
71  // Standard OpenCV's function:
72  corners_found = 0 != cv::findChessboardCorners(
73  cvImg, check_size, pointbuf, find_chess_flags);
74 
75  corners_list.resize(pointbuf.size());
76  for (size_t i = 0; i < pointbuf.size(); i++)
77  {
78  corners_list[i].x = pointbuf[i].x;
79  corners_list[i].y = pointbuf[i].y;
80  }
81  }
82  else
83  {
84  // Return: -1: errors, 0: not found, 1: found OK
85  corners_found =
86  1 == cvFindChessboardCorners3(img, check_size, corners_list);
87  }
88 
89  // Check # of corners:
90  if (corners_found && corners_count != CORNERS_COUNT) corners_found = false;
91 
92  if (corners_found)
93  {
94  IplImage iplImg = cvIplImage(cvImg);
95  // Refine corners:
96  cvFindCornerSubPix(
97  &iplImg, &corners_list[0], corners_count, cvSize(5, 5), // window
98  cvSize(-1, -1),
99  cvTermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 10, 0.01f));
100 
101  // save the corners in the data structure:
102  int y;
103  unsigned int k;
104  for (y = 0, k = 0; y < check_size.height; y++)
105  for (int x = 0; x < check_size.width; x++, k++)
106  cornerCoords.emplace_back(corners_list[k].x, corners_list[k].y);
107  }
108 
109  return corners_found;
110 
111  MRPT_END
112 #else
113  return false;
114 #endif
115 }
116 
117 /** Look for the corners of one or more chessboard/checkerboards in the image.
118  * This method uses an improved version of OpenCV's cvFindChessboardCorners
119  * published
120  * by M. Rufli, D. Scaramuzza, and R. Siegwart.
121  * That method has been extended in this MRPT implementation to automatically
122  * detect a
123  * number of different checkerboards in the same image.
124  *
125  * \param cornerCoords [OUT] A vector of N vectors of pixel coordinates, for
126  * each of the N chessboards detected.
127  * \param check_size_x [IN] The number of squares, in the X direction
128  * \param check_size_y [IN] The number of squares, in the Y direction
129  *
130  *
131  * \sa mrpt::vision::checkerBoardCameraCalibration, drawChessboardCorners
132  */
134  const mrpt::img::CImage& in_img,
135  std::vector<std::vector<TPixelCoordf>>& cornerCoords,
136  unsigned int check_size_x, unsigned int check_size_y)
137 {
138 #if MRPT_HAS_OPENCV
139  // Grayscale version:
140  const CImage img(in_img, FAST_REF_OR_CONVERT_TO_GRAY);
141  const cv::Mat img_m = img.asCvMat<cv::Mat>(SHALLOW_COPY);
142  const IplImage img_ipl = cvIplImage(img_m);
143 
144  std::vector<std::vector<CvPoint2D32f>> corners_list;
145 
146  // Return: -1: errors, 0: not found, 1: found OK
147  bool corners_found = find_chessboard_corners_multiple(
148  img, cvSize(check_size_x, check_size_y), corners_list);
149 
150  if (corners_found && !corners_list.empty())
151  {
152  // Alloc space for output points:
153  cornerCoords.resize(corners_list.size());
154 
155  // Refine corners:
156  for (size_t i = 0; i < corners_list.size(); i++)
157  {
158  ASSERT_(corners_list[i].size() == check_size_x * check_size_y);
159 
160  cvFindCornerSubPix(
161  &img_ipl, &corners_list[i][0], check_size_x * check_size_y,
162  cvSize(5, 5), // window
163  cvSize(-1, -1),
164  cvTermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 10, 0.01f));
165 
166  // save the corners in the data structure:
167  for (unsigned int y = 0, k = 0; y < check_size_y; y++)
168  for (unsigned int x = 0; x < check_size_x; x++, k++)
169  cornerCoords[i].push_back(TPixelCoordf(
170  corners_list[i][k].x, corners_list[i][k].y));
171 
172  // Consistency of the counter-clockwise XYZ reference system and
173  // corners ORDER.
174 
175  // Key idea: The cross product of X * Y must point outwards the
176  // screen:
177 
178  const mrpt::math::TPoint2D pt_0{cornerCoords[i][0].x,
179  cornerCoords[i][0].y},
180  pt_x1{cornerCoords[i][1].x, cornerCoords[i][1].y},
181  pt_y1{cornerCoords[i][check_size_x].x,
182  cornerCoords[i][check_size_x].y};
183  const mrpt::math::TPoint3D Ax = pt_x1 - pt_0; // z=0
184  const mrpt::math::TPoint3D Ay = pt_y1 - pt_0; // z=0
185 
186  const auto Az = mrpt::math::crossProduct3D(Ax, Ay);
187  if (Az[2] > 0)
188  {
189  // Invert all rows (X):
190  for (unsigned int y = 0; y < check_size_y; y++)
191  std::reverse(
192  cornerCoords[i].begin() + y * check_size_x,
193  cornerCoords[i].begin() + (y + 1) * check_size_x);
194  }
195  }
196  }
197  else
198  { // Not found.
199  cornerCoords.clear();
200  return;
201  }
202 
203 #endif
204 }
void findMultipleChessboardsCorners(const mrpt::img::CImage &img, std::vector< std::vector< mrpt::img::TPixelCoordf >> &cornerCoords, unsigned int check_size_x, unsigned int check_size_y)
Look for the corners of one or more chessboard/checkerboards in the image.
Shallow copy: the copied object is a reference to the original one.
Definition: img/CImage.h:75
#define MRPT_START
Definition: exceptions.h:241
T x
X,Y coordinates.
Definition: TPoint2D.h:25
size_t size(const MATRIXLIKE &m, const int dim)
A pair (x,y) of pixel coordinates (subpixel resolution).
Definition: TPixelCoord.h:18
STL namespace.
void crossProduct3D(const T &v0, const U &v1, V &vOut)
Computes the cross product of two 3D vectors, returning a vector normal to both.
Definition: geometry.h:765
void asCvMat(cv::Mat &out_img, copy_type_t copy_type) const
Makes a shallow or deep copy of this image into the provided cv::Mat.
Definition: CImage.cpp:217
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
This base provides a set of functions for maths stuff.
bool findChessboardCorners(const mrpt::img::CImage &img, std::vector< mrpt::img::TPixelCoordf > &cornerCoords, unsigned int check_size_x, unsigned int check_size_y, bool normalize_image=true, bool useScaramuzzaMethod=false)
Look for the corners of a chessboard in the image using one of two different methods.
Classes for computer vision, detectors, features, etc.
Definition: CDifodo.h:17
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
const_iterator begin() const
Definition: ts_hash_map.h:240
struct _IplImage IplImage
Definition: img/CImage.h:22
#define MRPT_END
Definition: exceptions.h:245
bool find_chessboard_corners_multiple(const CImage &img_, CvSize pattern_size, std::vector< std::vector< CvPoint2D32f >> &out_corners)
int cvFindChessboardCorners3(const CImage &img_, CvSize pattern_size, std::vector< CvPoint2D32f > &out_corners)
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:148



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020