Fix bugs in calibrate.cc
This commit is contained in:
commit
285c65b6fb
2 changed files with 147 additions and 120 deletions
246
calibrate.cc
246
calibrate.cc
|
@ -19,124 +19,144 @@ enum {INTERACTIVE_MODE, PRESPECIFIED_MODE};
|
||||||
#define COUNT_SQUARES_X 4
|
#define COUNT_SQUARES_X 4
|
||||||
#define COUNT_SQUARES_Y 6
|
#define COUNT_SQUARES_Y 6
|
||||||
|
|
||||||
|
int detect_pattern(Mat frame, vector< vector<Point3f> >& object_points, vector<vector<Point2f> >& image_points)
|
||||||
|
{
|
||||||
|
// interior number of corners
|
||||||
|
Size pattern_size(COUNT_SQUARES_X, COUNT_SQUARES_Y);
|
||||||
|
// will be filled by the detected corners
|
||||||
|
vector<Point2f> corners;
|
||||||
|
|
||||||
|
bool pattern_found = findChessboardCorners(
|
||||||
|
frame, pattern_size, corners,
|
||||||
|
CALIB_CB_ADAPTIVE_THRESH +
|
||||||
|
CALIB_CB_NORMALIZE_IMAGE +
|
||||||
|
CALIB_CB_FAST_CHECK);
|
||||||
|
|
||||||
|
if (!pattern_found)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
// to tweak params: http://bit.ly/QyoU3k
|
||||||
|
cornerSubPix(frame, corners, Size(11, 11), Size(-1, -1),
|
||||||
|
TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 100,
|
||||||
|
0.1));
|
||||||
|
drawChessboardCorners(frame, pattern_size, Mat(corners),
|
||||||
|
pattern_found);
|
||||||
|
vector<Point3f> obj;
|
||||||
|
for (int j = 0; j < COUNT_SQUARES_X * COUNT_SQUARES_Y; ++j)
|
||||||
|
obj.push_back(Point3f(j / COUNT_SQUARES_X, j % COUNT_SQUARES_X, 0.0f));
|
||||||
|
|
||||||
|
object_points.push_back(obj);
|
||||||
|
image_points.push_back(corners);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int run_interactive()
|
||||||
|
{
|
||||||
|
namedWindow("Camera Image", CV_WINDOW_KEEPRATIO);
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
Mat frame;
|
||||||
|
vector<vector<Point3f> > object_points;
|
||||||
|
vector<vector<Point2f> > image_points;
|
||||||
|
Mat intrinsic = Mat(3, 3, CV_32FC1);
|
||||||
|
Mat distCoeffs;
|
||||||
|
vector<Mat> rvecs, tvecs;
|
||||||
|
|
||||||
|
// open the default camera
|
||||||
|
VideoCapture capture(-1);
|
||||||
|
|
||||||
|
// check if opening camera stream succeeded
|
||||||
|
if (!capture.isOpened()) {
|
||||||
|
cerr << "Camera could not be found. Exiting.\n";
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
cout << "Frame: " << count << endl;
|
||||||
|
Mat gray_frame;
|
||||||
|
capture >> frame; // get a new frame from camera
|
||||||
|
cvtColor(frame, gray_frame, CV_BGR2GRAY);
|
||||||
|
imshow("Camera Image", frame);
|
||||||
|
|
||||||
|
int key_pressed = waitKey(0);
|
||||||
|
if (key_pressed == KEY_CLOSE_WINDOW || key_pressed == KEY_ESCAPE)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (key_pressed == KEY_SPACE) {
|
||||||
|
if (detect_pattern(gray_frame, object_points, image_points) == 0) {
|
||||||
|
cout << "Frame " << count << " grabbed." << endl;
|
||||||
|
} else {
|
||||||
|
cout << "pattern not found" << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
calibrateCamera(object_points, image_points, frame.size(), intrinsic,
|
||||||
|
distCoeffs, rvecs, tvecs);
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
capture >> frame;
|
||||||
|
Mat imageUndistorted;
|
||||||
|
undistort(frame, imageUndistorted, intrinsic, distCoeffs);
|
||||||
|
imshow("win1", frame);
|
||||||
|
imshow("win2", imageUndistorted);
|
||||||
|
waitKey(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
capture.release();
|
||||||
|
}
|
||||||
|
|
||||||
|
int run_prespecified(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int count = 0;
|
||||||
|
Mat frame;
|
||||||
|
vector<vector<Point3f> > object_points;
|
||||||
|
vector<vector<Point2f> > image_points;
|
||||||
|
Mat intrinsic = Mat(3, 3, CV_32FC1);
|
||||||
|
Mat distCoeffs;
|
||||||
|
vector<Mat> rvecs, tvecs;
|
||||||
|
|
||||||
|
for (int i = 0; i < argc; i++) {
|
||||||
|
Mat gray_frame;
|
||||||
|
cerr << endl << argv[i] << endl;
|
||||||
|
frame = imread(argv[i], CV_LOAD_IMAGE_COLOR);
|
||||||
|
cvtColor(frame, gray_frame, CV_BGR2GRAY);
|
||||||
|
imshow("Camera Image", frame);
|
||||||
|
if (detect_pattern(gray_frame, object_points, image_points) == 0) {
|
||||||
|
cout << "Frame " << i << " grabbed." << endl;
|
||||||
|
} else {
|
||||||
|
cout << "Pattern not found in frame " << i << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << "Calibrating..." << endl;
|
||||||
|
calibrateCamera(object_points, image_points, frame.size(), intrinsic,
|
||||||
|
distCoeffs, rvecs, tvecs);
|
||||||
|
|
||||||
|
for (int i = 0; i < argc; i++) {
|
||||||
|
frame = imread(argv[i], CV_LOAD_IMAGE_COLOR);
|
||||||
|
Mat imageUndistorted;
|
||||||
|
undistort(frame, imageUndistorted, intrinsic, distCoeffs);
|
||||||
|
imshow("win1", frame);
|
||||||
|
imshow("win2", imageUndistorted);
|
||||||
|
waitKey(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int user_mode;
|
int user_mode;
|
||||||
int specified_boards;
|
int specified_boards;
|
||||||
if (argc > 2)
|
|
||||||
cerr << "Usage: ./calibrate [number of frames]\n";
|
/* no arguments means interactive mode
|
||||||
else if (argc == 1)
|
* one or more arguments are image filenames */
|
||||||
{
|
if (argc == 1) {
|
||||||
user_mode = INTERACTIVE_MODE;
|
run_interactive();
|
||||||
cout << "Camera calibration using interactive behavior. Press SPACE to grab frame, ESC to quit.\n";
|
} else {
|
||||||
}
|
run_prespecified(argc-1, ++argv);
|
||||||
else
|
|
||||||
{
|
|
||||||
user_mode = PRESPECIFIED_MODE;
|
|
||||||
stringstream ss; ss << argv[1];
|
|
||||||
ss >> specified_boards;
|
|
||||||
cout << "Camera calibration using " << specified_boards << " frames.\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
VideoCapture capture(0); // open the default camera
|
|
||||||
if (!capture.isOpened()) // check if opening camera stream succeeded
|
|
||||||
{
|
|
||||||
cerr << "Camera could not be found. Exiting.\n";
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
// set frame width and height by hand, defaults to 160x120
|
|
||||||
capture.set (CV_CAP_PROP_FRAME_WIDTH, 640);
|
|
||||||
capture.set (CV_CAP_PROP_FRAME_HEIGHT, 480);
|
|
||||||
|
|
||||||
// show camera image in a separate window
|
|
||||||
namedWindow("Camera Image", CV_WINDOW_KEEPRATIO);
|
|
||||||
// store object points (world coords) and image points (image coords) for use in calibrateCamera
|
|
||||||
vector< vector <Point3f> > object_points;
|
|
||||||
vector< vector <Point2f> > image_points;
|
|
||||||
|
|
||||||
/// current frame, its grayscale counterpart and storage for the first frame
|
|
||||||
Mat frame, gray_frame, first_frame;
|
|
||||||
// flag for determining whether pattern was detected in at least one of the camera grabs
|
|
||||||
bool acquired_samples = false;
|
|
||||||
|
|
||||||
// loop indefinitely and keep frame counter
|
|
||||||
for(int count_frames = 0; ; ++count_frames)
|
|
||||||
{
|
|
||||||
capture >> frame; // get a new frame from camera
|
|
||||||
cvtColor(frame, gray_frame, CV_BGR2GRAY); // convert current frame to grayscale
|
|
||||||
imshow("Camera Image", frame); // update camera image
|
|
||||||
|
|
||||||
int key_pressed = waitKey(0); // get user key press
|
|
||||||
if (key_pressed == KEY_CLOSE_WINDOW || key_pressed == KEY_ESCAPE) break;
|
|
||||||
|
|
||||||
if (user_mode == INTERACTIVE_MODE && key_pressed == KEY_SPACE)
|
|
||||||
{
|
|
||||||
Size pattern_size(COUNT_SQUARES_X, COUNT_SQUARES_Y); // number of squares in the pattern, a.k.a, interior number of corners
|
|
||||||
vector <Point2f> corners; // storage for the detected corners in findChessboardConrners
|
|
||||||
bool pattern_found = findChessboardCorners( gray_frame, pattern_size, corners, CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE + CALIB_CB_FAST_CHECK);
|
|
||||||
|
|
||||||
if (pattern_found)
|
|
||||||
{
|
|
||||||
if (count_frames == 0) first_frame = frame;
|
|
||||||
// if corners are detected, they are further refined by calculating subpixel corners from the grayscale image
|
|
||||||
// this iterative process terminates after the given number of iterations and error epsilon
|
|
||||||
cornerSubPix(gray_frame, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 100, 0.1));
|
|
||||||
// draw the detected corners for debugging purposes
|
|
||||||
drawChessboardCorners(frame, pattern_size, Mat(corners), pattern_found);
|
|
||||||
// show detected corners in a different window
|
|
||||||
imshow("Detected pattern", frame);
|
|
||||||
|
|
||||||
// build a grid of 3D points (z component is 0 because the pattern is in one plane) to fit the square pattern area (COUNT_SQUARES_X * COUNT_SQUARES_Y)
|
|
||||||
vector< Point3f > pattern_points;
|
|
||||||
for(int j = 0; j < COUNT_SQUARES_X * COUNT_SQUARES_Y; ++j)
|
|
||||||
pattern_points.push_back(Point3f(j/COUNT_SQUARES_X, j%COUNT_SQUARES_X, 0.0f));
|
|
||||||
|
|
||||||
// populate image points with corners and object points with grid points
|
|
||||||
image_points.push_back(corners);
|
|
||||||
object_points.push_back(pattern_points);
|
|
||||||
cout << "Frame " << count_frames << " grabbed.\n";
|
|
||||||
acquired_samples = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// if at least one video capture contains the pattern, perform calibration
|
|
||||||
if (acquired_samples)
|
|
||||||
{
|
|
||||||
Mat intrinsic = Mat(3, 3, CV_32FC1);
|
|
||||||
Mat distCoeffs;
|
|
||||||
vector<Mat> rvecs;
|
|
||||||
vector<Mat> tvecs;
|
|
||||||
|
|
||||||
// perform calibration, obtain instrinsic parameters and distortion coefficients
|
|
||||||
cout << "\nCalibrating...\n";
|
|
||||||
calibrateCamera(object_points, image_points, frame.size(), intrinsic, distCoeffs, rvecs, tvecs);
|
|
||||||
|
|
||||||
/*
|
|
||||||
Mat grab2 = imread("./data/grab2.jpg"), grab2_undistorted;
|
|
||||||
Mat grab4 = imread("./data/grab4.jpg"), grab4_undistorted;
|
|
||||||
undistort(grab2, grab2_undistorted, intrinsic, distCoeffs);
|
|
||||||
undistort(grab4, grab4_undistorted, intrinsic, distCoeffs);
|
|
||||||
|
|
||||||
imwrite("./data/grab2_undistorted.jpg", grab2_undistorted);
|
|
||||||
imwrite("./data/grab4_undistorted.jpg", grab4_undistorted);
|
|
||||||
*/
|
|
||||||
|
|
||||||
Mat undistorted_frame;
|
|
||||||
// apply the calibration transformation to the first frame and store image on disk
|
|
||||||
undistort(first_frame, undistorted_frame, intrinsic, distCoeffs);
|
|
||||||
imwrite("capture.jpg", first_frame);
|
|
||||||
imwrite("undistorted_frame.jpg", undistorted_frame);
|
|
||||||
cout << "Saved first frame to undistorted_frame.jpg\n";
|
|
||||||
cout << "Intrinsic parameters: \n" << intrinsic << "\n";
|
|
||||||
cout << "Distortion coefficients: \n" << distCoeffs << "\n";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
cerr << "No frames grabbed!\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
// the camera will be deinitialized automatically in VideoCapture destructor
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
15
calibrate.py
15
calibrate.py
|
@ -8,6 +8,7 @@ if __name__ == "__main__":
|
||||||
im = cv.LoadImage(filename, cv.CV_LOAD_IMAGE_GRAYSCALE)
|
im = cv.LoadImage(filename, cv.CV_LOAD_IMAGE_GRAYSCALE)
|
||||||
im3 = cv.LoadImage(filename, cv.CV_LOAD_IMAGE_COLOR)
|
im3 = cv.LoadImage(filename, cv.CV_LOAD_IMAGE_COLOR)
|
||||||
board_width, board_height = (4, 6)
|
board_width, board_height = (4, 6)
|
||||||
|
square_size = 4
|
||||||
corner_count = board_width * board_height
|
corner_count = board_width * board_height
|
||||||
|
|
||||||
found_all, corners = cv.FindChessboardCorners( im, (board_width, board_height))
|
found_all, corners = cv.FindChessboardCorners( im, (board_width, board_height))
|
||||||
|
@ -24,19 +25,25 @@ if __name__ == "__main__":
|
||||||
image_points = cv.CreateMat(corner_count, 2, cv.CV_32FC1)
|
image_points = cv.CreateMat(corner_count, 2, cv.CV_32FC1)
|
||||||
point_counts = cv.CreateMat(1, 1, cv.CV_32SC1)
|
point_counts = cv.CreateMat(1, 1, cv.CV_32SC1)
|
||||||
for jj in range(corner_count):
|
for jj in range(corner_count):
|
||||||
cv.Set2D(object_points, jj, 0, float(jj/board_width))
|
#cv.Set2D(object_points, jj, 0, float(jj/board_width))
|
||||||
cv.Set2D(object_points, jj, 1, float(jj%board_width))
|
#cv.Set2D(object_points, jj, 1, float(jj%board_width))
|
||||||
cv.Set2D(object_points, jj, 2, float(0.0))
|
#cv.Set2D(object_points, jj, 2, float(0.0))
|
||||||
cv.Set2D(image_points, jj, 0, corners[jj][0])
|
cv.Set2D(image_points, jj, 0, corners[jj][0])
|
||||||
cv.Set2D(image_points, jj, 1, corners[jj][1])
|
cv.Set2D(image_points, jj, 1, corners[jj][1])
|
||||||
|
for i in range(board_height):
|
||||||
|
for j in range(board_width):
|
||||||
|
cv.Set2D(object_points, i*board_width+j, 0, float(i*square_size))
|
||||||
|
cv.Set2D(object_points, i*board_width+j, 1, float(j*square_size))
|
||||||
|
cv.Set2D(object_points, i*board_width+j, 2, float(0.0))
|
||||||
|
print i*board_width+j, i*square_size, j*square_size
|
||||||
cv.Set1D(point_counts, 0, corner_count)
|
cv.Set1D(point_counts, 0, corner_count)
|
||||||
cv.CalibrateCamera2(object_points, image_points, point_counts, cv.GetSize(im3), camera_intrinsic_mat, distortion_mat, rotation_vecs, translation_vecs)
|
cv.CalibrateCamera2(object_points, image_points, point_counts, cv.GetSize(im3), camera_intrinsic_mat, distortion_mat, rotation_vecs, translation_vecs)
|
||||||
|
|
||||||
undistorted = cv.CloneImage(im3);
|
undistorted = cv.CloneImage(im3);
|
||||||
|
|
||||||
cv.Undistort2(im3, undistorted, camera_intrinsic_mat, distortion_mat)
|
cv.Undistort2(im3, undistorted, camera_intrinsic_mat, distortion_mat)
|
||||||
|
|
||||||
cv.ShowImage("win", undistorted);
|
cv.ShowImage("win", undistorted);
|
||||||
|
#cv.ShowImage("win", im3);
|
||||||
cv.WaitKey()
|
cv.WaitKey()
|
||||||
else:
|
else:
|
||||||
print "not found"
|
print "not found"
|
||||||
|
|
Loading…
Reference in a new issue