Fix bugs in calibrate.cc

main
Razvan Mihalyi 12 years ago
commit 285c65b6fb

@ -10,7 +10,7 @@
using namespace std; using namespace std;
using namespace cv; using namespace cv;
enum {INTERACTIVE_MODE, PRESPECIFIED_MODE}; enum { INTERACTIVE_MODE, PRESPECIFIED_MODE };
#define KEY_ESCAPE 1048603 #define KEY_ESCAPE 1048603
#define KEY_SPACE 1048608 #define KEY_SPACE 1048608
@ -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 main(int argc, char** argv) int detect_pattern(Mat frame, vector< vector<Point3f> >& object_points, vector<vector<Point2f> >& image_points)
{ {
int user_mode; // interior number of corners
int specified_boards; Size pattern_size(COUNT_SQUARES_X, COUNT_SQUARES_Y);
if (argc > 2) // will be filled by the detected corners
cerr << "Usage: ./calibrate [number of frames]\n"; vector<Point2f> corners;
else if (argc == 1)
{ bool pattern_found = findChessboardCorners(
user_mode = INTERACTIVE_MODE; frame, pattern_size, corners,
cout << "Camera calibration using interactive behavior. Press SPACE to grab frame, ESC to quit.\n"; CALIB_CB_ADAPTIVE_THRESH +
} CALIB_CB_NORMALIZE_IMAGE +
else CALIB_CB_FAST_CHECK);
{
user_mode = PRESPECIFIED_MODE; if (!pattern_found)
stringstream ss; ss << argv[1]; return -1;
ss >> specified_boards;
cout << "Camera calibration using " << specified_boards << " frames.\n"; // to tweak params: http://bit.ly/QyoU3k
} cornerSubPix(frame, corners, Size(11, 11), Size(-1, -1),
TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 100,
VideoCapture capture(0); // open the default camera 0.1));
if (!capture.isOpened()) // check if opening camera stream succeeded drawChessboardCorners(frame, pattern_size, Mat(corners),
{ pattern_found);
cerr << "Camera could not be found. Exiting.\n"; vector<Point3f> obj;
return -1; 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));
// set frame width and height by hand, defaults to 160x120
capture.set (CV_CAP_PROP_FRAME_WIDTH, 640); object_points.push_back(obj);
capture.set (CV_CAP_PROP_FRAME_HEIGHT, 480); image_points.push_back(corners);
return 0;
// 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 int run_interactive()
vector< vector <Point3f> > object_points; {
vector< vector <Point2f> > image_points; namedWindow("Camera Image", CV_WINDOW_KEEPRATIO);
/// current frame, its grayscale counterpart and storage for the first frame int count = 0;
Mat frame, gray_frame, first_frame; Mat frame;
// flag for determining whether pattern was detected in at least one of the camera grabs vector<vector<Point3f> > object_points;
bool acquired_samples = false; vector<vector<Point2f> > image_points;
Mat intrinsic = Mat(3, 3, CV_32FC1);
// loop indefinitely and keep frame counter Mat distCoeffs;
for(int count_frames = 0; ; ++count_frames) vector<Mat> rvecs, tvecs;
{
capture >> frame; // get a new frame from camera // open the default camera
cvtColor(frame, gray_frame, CV_BGR2GRAY); // convert current frame to grayscale VideoCapture capture(-1);
imshow("Camera Image", frame); // update camera image
// check if opening camera stream succeeded
int key_pressed = waitKey(0); // get user key press if (!capture.isOpened()) {
if (key_pressed == KEY_CLOSE_WINDOW || key_pressed == KEY_ESCAPE) break; cerr << "Camera could not be found. Exiting.\n";
return -1;
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 for (;;) {
vector <Point2f> corners; // storage for the detected corners in findChessboardConrners cout << "Frame: " << count << endl;
bool pattern_found = findChessboardCorners( gray_frame, pattern_size, corners, CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE + CALIB_CB_FAST_CHECK); Mat gray_frame;
capture >> frame; // get a new frame from camera
if (pattern_found) cvtColor(frame, gray_frame, CV_BGR2GRAY);
{ imshow("Camera Image", frame);
if (count_frames == 0) first_frame = frame;
// if corners are detected, they are further refined by calculating subpixel corners from the grayscale image int key_pressed = waitKey(0);
// this iterative process terminates after the given number of iterations and error epsilon if (key_pressed == KEY_CLOSE_WINDOW || key_pressed == KEY_ESCAPE)
cornerSubPix(gray_frame, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 100, 0.1)); break;
// draw the detected corners for debugging purposes
drawChessboardCorners(frame, pattern_size, Mat(corners), pattern_found); if (key_pressed == KEY_SPACE) {
// show detected corners in a different window if (detect_pattern(gray_frame, object_points, image_points) == 0) {
imshow("Detected pattern", frame); cout << "Frame " << count << " grabbed." << endl;
} else {
// 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) cout << "pattern not found" << endl;
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 calibrateCamera(object_points, image_points, frame.size(), intrinsic,
image_points.push_back(corners); distCoeffs, rvecs, tvecs);
object_points.push_back(pattern_points);
cout << "Frame " << count_frames << " grabbed.\n"; for (;;) {
acquired_samples = true; capture >> frame;
} Mat imageUndistorted;
} undistort(frame, imageUndistorted, intrinsic, distCoeffs);
imshow("win1", frame);
} imshow("win2", imageUndistorted);
waitKey(1);
// if at least one video capture contains the pattern, perform calibration }
if (acquired_samples)
{ capture.release();
Mat intrinsic = Mat(3, 3, CV_32FC1); }
Mat distCoeffs;
vector<Mat> rvecs; int run_prespecified(int argc, char **argv)
vector<Mat> tvecs; {
int count = 0;
// perform calibration, obtain instrinsic parameters and distortion coefficients Mat frame;
cout << "\nCalibrating...\n"; vector<vector<Point3f> > object_points;
calibrateCamera(object_points, image_points, frame.size(), intrinsic, distCoeffs, rvecs, tvecs); vector<vector<Point2f> > image_points;
Mat intrinsic = Mat(3, 3, CV_32FC1);
/* Mat distCoeffs;
Mat grab2 = imread("./data/grab2.jpg"), grab2_undistorted; vector<Mat> rvecs, tvecs;
Mat grab4 = imread("./data/grab4.jpg"), grab4_undistorted;
undistort(grab2, grab2_undistorted, intrinsic, distCoeffs); for (int i = 0; i < argc; i++) {
undistort(grab4, grab4_undistorted, intrinsic, distCoeffs); Mat gray_frame;
cerr << endl << argv[i] << endl;
imwrite("./data/grab2_undistorted.jpg", grab2_undistorted); frame = imread(argv[i], CV_LOAD_IMAGE_COLOR);
imwrite("./data/grab4_undistorted.jpg", grab4_undistorted); cvtColor(frame, gray_frame, CV_BGR2GRAY);
*/ imshow("Camera Image", frame);
if (detect_pattern(gray_frame, object_points, image_points) == 0) {
Mat undistorted_frame; cout << "Frame " << i << " grabbed." << endl;
// apply the calibration transformation to the first frame and store image on disk } else {
undistort(first_frame, undistorted_frame, intrinsic, distCoeffs); cout << "Pattern not found in frame " << i << endl;
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 << "Calibrating..." << endl;
cout << "Distortion coefficients: \n" << distCoeffs << "\n"; calibrateCamera(object_points, image_points, frame.size(), intrinsic,
} distCoeffs, rvecs, tvecs);
else
{ for (int i = 0; i < argc; i++) {
cerr << "No frames grabbed!\n"; frame = imread(argv[i], CV_LOAD_IMAGE_COLOR);
} Mat imageUndistorted;
undistort(frame, imageUndistorted, intrinsic, distCoeffs);
// the camera will be deinitialized automatically in VideoCapture destructor imshow("win1", frame);
return 0; imshow("win2", imageUndistorted);
waitKey(0);
}
return 0;
}
int main(int argc, char **argv)
{
int user_mode;
int specified_boards;
/* no arguments means interactive mode
* one or more arguments are image filenames */
if (argc == 1) {
run_interactive();
} else {
run_prespecified(argc-1, ++argv);
}
return 0;
} }

@ -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…
Cancel
Save