43 #include <visp/vpConfig.h>
45 #if VISP_HAVE_OPENCV_VERSION >= 0x020300
47 #include <opencv2/core/core.hpp>
48 #include <opencv2/imgproc/imgproc.hpp>
49 #include <opencv2/calib3d/calib3d.hpp>
50 #include <opencv2/highgui/highgui.hpp>
52 #include <visp/vpCalibration.h>
54 #include <visp/vpDisplayX.h>
55 #include <visp/vpDisplayGDI.h>
56 #include <visp/vpDisplayOpenCV.h>
57 #include <visp/vpDisplayD3D.h>
58 #include <visp/vpDisplayGTK.h>
59 #include <visp/vpIoTools.h>
60 #include <visp/vpPoint.h>
61 #include <visp/vpVideoReader.h>
62 #include <visp/vpXmlParserCamera.h>
70 bool read(
const std::string &filename)
81 std::cout <<
"grid width : " <<
boardSize.width << std::endl;
82 std::cout <<
"grid height: " <<
boardSize.height << std::endl;
83 std::cout <<
"square size: " <<
squareSize << std::endl;
84 std::cout <<
"pattern : " << patternToUse << std::endl;
85 std::cout <<
"input seq : " <<
input << std::endl;
93 std::cerr <<
"Invalid Board size: " <<
boardSize.width <<
" " <<
boardSize.height << std::endl;
97 std::cerr <<
"Invalid square size " <<
squareSize << std::endl;
108 std::cerr <<
" Inexistent camera calibration mode: " << patternToUse << std::endl;
121 std::string patternToUse;
124 int main(
int argc,
const char ** argv)
126 std::string outputFileName =
"camera.xml";
129 const std::string inputSettingsFile = argc > 1 ? argv[1] :
"default.cfg";
130 if (! s.
read(inputSettingsFile) ) {
131 std::cout <<
"Could not open the configuration file: \"" << inputSettingsFile <<
"\"" << std::endl;
132 std::cout << std::endl <<
"Usage: " << argv[0] <<
" <configuration file>.cfg" << std::endl;
138 std::cout <<
"Invalid input detected. Application stopping. " << std::endl;
150 #elif defined VISP_HAVE_GDI
152 #elif defined VISP_HAVE_GTK
154 #elif defined VISP_HAVE_OPENCV
158 std::vector<vpPoint> model;
159 std::vector<vpCalibration> calibrator;
161 for (
int i=0; i< s.
boardSize.height; i++) {
162 for (
int j=0; j< s.
boardSize.width; j++) {
170 while(! reader.
end()) {
176 std::vector<cv::Point2f> pointBuf;
183 found = findChessboardCorners( cvI, s.
boardSize, pointBuf,
184 CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FAST_CHECK | CV_CALIB_CB_NORMALIZE_IMAGE);
187 found = findCirclesGrid( cvI, s.
boardSize, pointBuf, cv::CALIB_CB_SYMMETRIC_GRID );
193 std::cout <<
"frame: " << frame_index <<
" status: " << found << std::endl;
197 std::vector<vpImagePoint> data;
201 cornerSubPix( cvI, pointBuf, cv::Size(11,11),
202 cv::Size(-1,-1), cv::TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));
204 char title[20]; sprintf(title,
"image %ld", frame_index);
206 for (
unsigned int i=0; i < pointBuf.size(); i++) {
217 for (
unsigned int i=0; i<model.size(); i++) {
218 calib.
addPoint(model[i].get_oX(), model[i].get_oY(), model[i].get_oZ(), data[i]);
232 calibrator.push_back(calib);
242 if (calibrator.empty()) {
243 std::cerr <<
"Unable to calibrate. Image processing failed !" << std::endl;
247 std::cout <<
"\nCalibration without distorsion in progress on " << calibrator.size() <<
" images..." << std::endl;
251 std::cout << cam << std::endl;
252 std::cout <<
"Global reprojection error: " << error << std::endl;
253 #ifdef VISP_HAVE_XML2
257 std::cout <<
"Camera parameters without distortion successfully saved in \"" << outputFileName <<
"\"" << std::endl;
259 std::cout <<
"Failed to save the camera parameters without distortion in \"" << outputFileName <<
"\"" << std::endl;
260 std::cout <<
"A file with the same name exists. Remove it to be able to save the parameters..." << std::endl;
265 std::cout <<
"Calibration without distortion failed." << std::endl;
267 std::cout <<
"\nCalibration with distorsion in progress on " << calibrator.size() <<
" images..." << std::endl;
269 std::cout << cam << std::endl;
270 std::cout <<
"Global reprojection error: " << error << std::endl;
271 #ifdef VISP_HAVE_XML2
275 std::cout <<
"Camera parameters without distortion successfully saved in \"" << outputFileName <<
"\"" << std::endl;
277 std::cout <<
"Failed to save the camera parameters without distortion in \"" << outputFileName <<
"\"" << std::endl;
278 std::cout <<
"A file with the same name exists. Remove it to be able to save the parameters..." << std::endl;
283 std::cout <<
"Calibration with distortion failed." << std::endl;
288 std::cout <<
"OpenCV 2.3.0 or higher is requested to run the calibration." << std::endl;
long getFrameIndex() const
unsigned int getWidth() const
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
Display for windows using GDI (available on any windows 32 platform).
Define the X11 console to display images.
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
int addPoint(double X, double Y, double Z, vpImagePoint &ip)
int save(const vpCameraParameters &cam, const char *filename, const std::string &camera_name, const unsigned int image_width=0, const unsigned int image_height=0)
Tools for perspective camera calibration.
XML parser to load and save intrinsic camera parameters.
static void flush(const vpImage< unsigned char > &I)
int computeCalibration(vpCalibrationMethodType method, vpHomogeneousMatrix &cMo, vpCameraParameters &cam, bool verbose=false)
Class that defines what is a point.
static void setLambda(const double &lambda)
set the gain for the virtual visual servoing algorithm
void initPersProjWithoutDistortion(const double px, const double py, const double u0, const double v0)
void open(vpImage< vpRGBa > &I)
int clearPoint()
Suppress all the point in the array of point.
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the opencv library.
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
Generic class defining intrinsic camera parameters.
virtual void setTitle(const char *title)=0
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
void acquire(vpImage< vpRGBa > &I)
void setFileName(const char *filename)
Pattern calibrationPattern
unsigned int getHeight() const
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
static int computeCalibrationMulti(vpCalibrationMethodType method, std::vector< vpCalibration > &table_cal, vpCameraParameters &cam, double &globalReprojectionError, bool verbose=false)
bool read(const std::string &filename)
void setWorldCoordinates(const double ox, const double oy, const double oz)
Set the point world coordinates. We mean here the coordinates of the point in the object frame...