#include <iostream>
using namespace std;
namespace
{
enum Pattern { CHESSBOARD, CIRCLES_GRID, ASYMMETRIC_CIRCLES_GRID };
void calcChessboardCorners(
Size boardSize,
float squareSize, vector<Point3f>& corners, Pattern patternType = CHESSBOARD)
{
corners.resize(0);
switch (patternType)
{
case CHESSBOARD:
case CIRCLES_GRID:
for(
int i = 0; i < boardSize.
height; i++ )
for(
int j = 0; j < boardSize.
width; j++ )
corners.push_back(
Point3f(
float(j*squareSize),
float(i*squareSize), 0));
break;
case ASYMMETRIC_CIRCLES_GRID:
for(
int i = 0; i < boardSize.
height; i++ )
for(
int j = 0; j < boardSize.
width; j++ )
corners.push_back(
Point3f(
float((2*j + i % 2)*squareSize),
float(i*squareSize), 0));
break;
default:
CV_Error(Error::StsBadArg,
"Unknown pattern type\n");
}
}
void poseEstimationFromCoplanarPoints(
const string &imgPath,
const string &intrinsicsPath,
const Size &patternSize,
const float squareSize)
{
Mat img = imread( samples::findFile( imgPath) );
vector<Point2f> corners;
bool found = findChessboardCorners(img, patternSize, corners);
if (!found)
{
cout << "Cannot find chessboard corners." << endl;
return;
}
drawChessboardCorners(img_corners, patternSize, corners, found);
imshow("Chessboard corners detection", img_corners);
vector<Point3f> objectPoints;
calcChessboardCorners(patternSize, squareSize, objectPoints);
vector<Point2f> objectPointsPlanar;
for (size_t i = 0; i < objectPoints.size(); i++)
{
objectPointsPlanar.push_back(
Point2f(objectPoints[i].x, objectPoints[i].y));
}
FileStorage fs( samples::findFile( intrinsicsPath ), FileStorage::READ);
Mat cameraMatrix, distCoeffs;
fs["camera_matrix"] >> cameraMatrix;
fs["distortion_coefficients"] >> distCoeffs;
vector<Point2f> imagePoints;
undistortPoints(corners, imagePoints, cameraMatrix, distCoeffs);
Mat H = findHomography(objectPointsPlanar, imagePoints);
cout << "H:\n" << H << endl;
double norm = sqrt(H.
at<
double>(0,0)*H.
at<
double>(0,0) +
H.
at<
double>(1,0)*H.
at<
double>(1,0) +
H.
at<
double>(2,0)*H.
at<
double>(2,0));
H /= norm;
for (int i = 0; i < 3; i++)
{
R.
at<
double>(i,0) = c1.
at<
double>(i,0);
R.
at<
double>(i,1) = c2.
at<
double>(i,0);
R.
at<
double>(i,2) = c3.
at<
double>(i,0);
}
cout << "R (before polar decomposition):\n" << R << "\ndet(R): " << determinant(R) << endl;
SVDecomp(R, W, U, Vt);
R = U*Vt;
double det = determinant(R);
if (det < 0)
{
Vt.
at<
double>(2,0) *= -1;
Vt.
at<
double>(2,1) *= -1;
Vt.
at<
double>(2,2) *= -1;
R = U*Vt;
}
cout << "R (after polar decomposition):\n" << R << "\ndet(R): " << determinant(R) << endl;
Rodrigues(R, rvec);
drawFrameAxes(img_pose, cameraMatrix, distCoeffs, rvec, tvec, 2*squareSize);
imshow("Pose from coplanar points", img_pose);
waitKey();
}
const char* params
= "{ help h | | print usage }"
"{ image | left04.jpg | path to a chessboard image }"
"{ intrinsics | left_intrinsics.yml | path to camera intrinsics }"
"{ width bw | 9 | chessboard width }"
"{ height bh | 6 | chessboard height }"
"{ square_size | 0.025 | chessboard square size }";
}
int main(
int argc,
char *argv[])
{
if (parser.has("help"))
{
parser.about("Code for homography tutorial.\n"
"Example 1: pose from homography with coplanar points.\n");
parser.printMessage();
return 0;
}
Size patternSize(parser.get<
int>(
"width"), parser.get<
int>(
"height"));
float squareSize = (float) parser.get<double>("square_size");
poseEstimationFromCoplanarPoints(parser.get<
String>(
"image"),
parser.get<
String>(
"intrinsics"),
patternSize, squareSize);
return 0;
}
Designed for command line parsing.
Definition utility.hpp:890
XML/YAML/JSON file storage class that encapsulates all the information necessary for writing or readi...
Definition persistence.hpp:261
Template matrix class derived from Mat.
Definition mat.hpp:2517
n-dimensional dense array class
Definition mat.hpp:950
CV_NODISCARD_STD Mat clone() const
Creates a full copy of the array and the underlying data.
Mat col(int x) const
Creates a matrix header for the specified matrix column.
Mat cross(InputArray m) const
Computes a cross-product of two 3-element vectors.
_Tp & at(int i0=0)
Returns a reference to the specified array element.
Template class for 3D points specified by its coordinates x, y and z.
Definition types.hpp:255
Template class for specifying the size of an image or rectangle.
Definition types.hpp:338
_Tp height
the height
Definition types.hpp:366
_Tp width
the width
Definition types.hpp:365
std::string String
Definition cvstd.hpp:151
#define CV_64F
Definition interface.h:82
#define CV_Error(code, msg)
Call the error handler.
Definition exception.hpp:174
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3