4 #include <visp3/core/vpConfig.h>
6 #if VISP_HAVE_OPENCV_VERSION >= 0x020300
8 #include <opencv2/core/core.hpp>
9 #include <opencv2/imgproc/imgproc.hpp>
10 #include <opencv2/calib3d/calib3d.hpp>
11 #include <opencv2/highgui/highgui.hpp>
13 #include <visp3/gui/vpDisplayX.h>
14 #include <visp3/gui/vpDisplayGDI.h>
15 #include <visp3/gui/vpDisplayOpenCV.h>
16 #include <visp3/gui/vpDisplayD3D.h>
17 #include <visp3/core/vpIoTools.h>
18 #include <visp3/core/vpPoint.h>
19 #include <visp3/core/vpPixelMeterConversion.h>
20 #include <visp3/core/vpXmlParserCamera.h>
21 #include <visp3/io/vpVideoReader.h>
22 #include <visp3/vision/vpPose.h>
25 void calcChessboardCorners(
int width,
int height,
double squareSize, std::vector<vpPoint> &corners) {
28 for (
int i = 0; i < height; i++) {
29 for (
int j = 0; j < width; j++) {
34 corners.push_back(pt);
40 int main(
int argc,
const char ** argv) {
41 int chessboard_width = 9, chessboard_height = 6;
42 double chessboard_square_size = 0.03;
43 std::string input_filename =
"";
44 std::string intrinsic_file =
"camera.xml";
45 std::string camera_name =
"Camera";
47 for (
int i = 1; i < argc; i++) {
48 if (std::string(argv[i]) ==
"-w" && i+1 < argc) {
49 chessboard_width = atoi(argv[i+1]);
50 }
else if (std::string(argv[i]) ==
"-h" && i+1 < argc) {
51 chessboard_height = atoi(argv[i+1]);
52 }
else if (std::string(argv[i]) ==
"--square_size" && i+1 < argc) {
53 chessboard_square_size = atof(argv[i+1]);
54 }
else if (std::string(argv[i]) ==
"--input" && i+1 < argc) {
55 input_filename = std::string(argv[i+1]);
56 }
else if (std::string(argv[i]) ==
"--intrinsic" && i+1 < argc) {
57 intrinsic_file = std::string(argv[i+1]);
58 }
else if (std::string(argv[i]) ==
"--camera_name" && i+1 < argc) {
59 camera_name = std::string(argv[i+1]);
61 else if (std::string(argv[i]) ==
"--help") {
62 std::cout << argv[0] <<
" [-w <chessboard width>] [-w <chessboard height>] [--square_size <square size in meter>] [--input <input images path>] [--intrinsic <Camera intrinsic parameters xml file>] [--camera_name <Camera name in the xml intrinsic file>]" << std::endl;
67 std::cout <<
"Parameters:" << std::endl;
68 std::cout <<
"chessboard_width=" << chessboard_width << std::endl;
69 std::cout <<
"chessboard_height=" << chessboard_height << std::endl;
70 std::cout <<
"chessboard_square_size=" << chessboard_square_size << std::endl;
71 std::cout <<
"input_filename=" << input_filename << std::endl;
72 std::cout <<
"intrinsic_file=" << intrinsic_file << std::endl;
73 std::cout <<
"camera_name=" << camera_name << std::endl;
76 if (input_filename.empty()) {
77 std::cerr <<
"input_filename.empty()" << std::endl;
87 #elif defined VISP_HAVE_GDI
89 #elif defined VISP_HAVE_OPENCV
93 std::vector<vpPoint> corners_pts;
94 calcChessboardCorners(chessboard_width, chessboard_height, chessboard_square_size, corners_pts);
98 if (!intrinsic_file.empty() && !camera_name.empty()) {
101 std::cout <<
"cam:\n" << cam << std::endl;
112 cv::Size chessboardSize(chessboard_width, chessboard_height);
113 std::vector<cv::Point2f> corners2D;
114 bool found = cv::findChessboardCorners(matImg, chessboardSize, corners2D,
115 #
if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
116 cv::CALIB_CB_ADAPTIVE_THRESH | cv::CALIB_CB_FAST_CHECK | cv::CALIB_CB_NORMALIZE_IMAGE);
118 CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FAST_CHECK | CV_CALIB_CB_NORMALIZE_IMAGE);
124 cv::cvtColor(matImg, matImg_gray, cv::COLOR_BGR2GRAY);
125 cv::cornerSubPix(matImg_gray, corners2D, cv::Size(11,11),
127 #
if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
128 cv::TermCriteria( cv::TermCriteria::EPS+cv::TermCriteria::COUNT, 30, 0.1 ));
130 cv::TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));
133 for (
size_t i = 0; i < corners_pts.size(); i++) {
135 double x = 0.0, y = 0.0;
137 corners_pts[i].set_x(x);
138 corners_pts[i].set_y(y);
144 double r_dementhon = std::numeric_limits<double>::max(), r_lagrange = std::numeric_limits<double>::max();
153 cMo = (r_dementhon < r_lagrange) ? cMo_dementhon : cMo_lagrange;
155 std::cerr <<
"Problem when computing final pose using VVS" << std::endl;
159 cv::drawChessboardCorners(matImg, chessboardSize, corners2D, found);
173 std::stringstream ss;
175 std::cout <<
"Save " << ss.str() << std::endl;
176 pose_vec.saveYAML(ss.str(), pose_vec);
190 }
while (!quit && !reader.
end());
196 std::cerr <<
"OpenCV 2.3.0 or higher is requested to run the calibration." << std::endl;
Generic class defining intrinsic camera parameters.
@ perspectiveProjWithDistortion
static const vpColor none
Display for windows using GDI (available on any windows 32 platform).
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void displayFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, const vpColor &color=vpColor::none, unsigned int thickness=1, const vpImagePoint &offset=vpImagePoint(0, 0))
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Implementation of an homogeneous matrix and operations on such kind of matrices.
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
void set_oY(double oY)
Set the point oY coordinate in the object frame.
void set_oZ(double oZ)
Set the point oZ coordinate in the object frame.
void set_oX(double oX)
Set the point oX coordinate in the object frame.
Implementation of a pose vector and operations on poses.
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
void addPoints(const std::vector< vpPoint > &lP)
double computeResidual(const vpHomogeneousMatrix &cMo) const
Compute and return the sum of squared residuals expressed in meter^2 for the pose matrix cMo.
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool(*func)(const vpHomogeneousMatrix &)=NULL)
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void acquire(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
long getFrameIndex() const
XML parser to load and save intrinsic camera parameters.
int parse(vpCameraParameters &cam, const std::string &filename, const std::string &camera_name, const vpCameraParameters::vpCameraParametersProjType &projModel, unsigned int image_width=0, unsigned int image_height=0)