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);
97 #ifdef VISP_HAVE_PUGIXML 99 if (!intrinsic_file.empty() && !camera_name.empty()) {
103 std::cout <<
"cam:\n" << cam << std::endl;
106 while (!quit && !reader.
end()) {
114 cv::Size chessboardSize(chessboard_width, chessboard_height);
115 std::vector<cv::Point2f> corners2D;
116 bool found = cv::findChessboardCorners(matImg, chessboardSize, corners2D,
117 #
if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
118 cv::CALIB_CB_ADAPTIVE_THRESH | cv::CALIB_CB_FAST_CHECK | cv::CALIB_CB_NORMALIZE_IMAGE);
120 CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FAST_CHECK | CV_CALIB_CB_NORMALIZE_IMAGE);
126 cv::cvtColor(matImg, matImg_gray, cv::COLOR_BGR2GRAY);
127 cv::cornerSubPix(matImg_gray, corners2D, cv::Size(11,11),
129 #
if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
130 cv::TermCriteria( cv::TermCriteria::EPS+cv::TermCriteria::COUNT, 30, 0.1 ));
132 cv::TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));
135 for (
size_t i = 0; i < corners_pts.size(); i++) {
137 double x = 0.0, y = 0.0;
139 corners_pts[i].set_x(x);
140 corners_pts[i].set_y(y);
146 double r_dementhon = std::numeric_limits<double>::max(), r_lagrange = std::numeric_limits<double>::max();
155 cMo = (r_dementhon < r_lagrange) ? cMo_dementhon : cMo_lagrange;
157 std::cerr <<
"Problem when computing final pose using VVS" << std::endl;
161 cv::drawChessboardCorners(matImg, chessboardSize, corners2D, found);
175 std::stringstream ss;
177 std::cout <<
"Save " << ss.str() << std::endl;
178 pose_vec.saveYAML(ss.str(), pose_vec);
198 std::cerr <<
"OpenCV 2.3.0 or higher is requested to run the calibration." << std::endl;
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool(*func)(const vpHomogeneousMatrix &)=NULL)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Implementation of an homogeneous matrix and operations on such kind of matrices.
Display for windows using GDI (available on any windows 32 platform).
void addPoints(const std::vector< vpPoint > &lP)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
static const vpColor none
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
XML parser to load and save intrinsic camera parameters.
static void flush(const vpImage< unsigned char > &I)
Class that defines what is a point.
void open(vpImage< vpRGBa > &I)
void set_oY(double oY)
Set the point Y coordinate in the object frame.
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
Generic class defining intrinsic camera parameters.
void acquire(vpImage< vpRGBa > &I)
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)
void set_oZ(double oZ)
Set the point Z coordinate in the object frame.
long getFrameIndex() const
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))
void set_oX(double oX)
Set the point X coordinate in the object frame.
Implementation of a pose vector and operations on poses.
void setFileName(const std::string &filename)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
double computeResidual(const vpHomogeneousMatrix &cMo) const
Compute and return the sum of squared residuals expressed in meter^2 for the pose matrix cMo...