|
| 1 | +#include "opencv_all.hpp" |
| 2 | + |
| 3 | +int main(void) |
| 4 | +{ |
| 5 | + cv::Mat K = (cv::Mat_<double>(3, 3) << 434.3588729143197, 0, 476.1230925877231, 0, 432.7138830286992, 289.2709802508451, 0, 0, 1); |
| 6 | + cv::Mat dist_coeff = (cv::Mat_<double>(5, 1) << -0.2918143346191932, 0.1095347774113121, -0.000105133686343854, 4.350475599617356e-005, -0.02083205595737927); |
| 7 | + cv::Size board_pattern(10, 7); |
| 8 | + double board_cellsize = 0.025; |
| 9 | + |
| 10 | + // Open an video |
| 11 | + cv::VideoCapture video; |
| 12 | + if (!video.open("data/chessboard.avi")) return -1; |
| 13 | + |
| 14 | + // Run pose estimation |
| 15 | + std::vector<cv::Point3f> object_points; |
| 16 | + for (int r = 0; r < board_pattern.height; r++) |
| 17 | + for (int c = 0; c < board_pattern.width; c++) |
| 18 | + object_points.push_back(cv::Point3f(board_cellsize * c, board_cellsize * r, 0)); |
| 19 | + std::vector<cv::Point3f> stick_points; |
| 20 | + stick_points.push_back(cv::Point3f(0, 0, 0)); |
| 21 | + stick_points.push_back(cv::Point3f(0, 0, -board_cellsize * 2)); |
| 22 | + stick_points.push_back(cv::Point3f(board_cellsize * (board_pattern.width - 1), 0, 0)); |
| 23 | + stick_points.push_back(cv::Point3f(board_cellsize * (board_pattern.width - 1), 0, -board_cellsize * 2)); |
| 24 | + stick_points.push_back(cv::Point3f(0, board_cellsize * (board_pattern.height - 1), 0)); |
| 25 | + stick_points.push_back(cv::Point3f(0, board_cellsize * (board_pattern.height - 1), -board_cellsize * 2)); |
| 26 | + stick_points.push_back(cv::Point3f(board_cellsize * (board_pattern.width - 1), board_cellsize * (board_pattern.height - 1), 0)); |
| 27 | + stick_points.push_back(cv::Point3f(board_cellsize * (board_pattern.width - 1), board_cellsize * (board_pattern.height - 1), -board_cellsize * 2)); |
| 28 | + while (true) |
| 29 | + { |
| 30 | + // Grab an image from the video |
| 31 | + cv::Mat image; |
| 32 | + video >> image; |
| 33 | + if (image.empty()) break; |
| 34 | + |
| 35 | + // Esimate camera pose |
| 36 | + std::vector<cv::Point2f> image_points; |
| 37 | + bool complete = cv::findChessboardCorners(image, board_pattern, image_points, cv::CALIB_CB_ADAPTIVE_THRESH + cv::CALIB_CB_NORMALIZE_IMAGE + cv::CALIB_CB_FAST_CHECK); |
| 38 | + if (complete) |
| 39 | + { |
| 40 | + cv::Mat rvec, tvec; |
| 41 | + cv::solvePnP(object_points, image_points, K, dist_coeff, rvec, tvec); |
| 42 | + |
| 43 | + // Draw four sticks |
| 44 | + std::vector<cv::Point2f> line_points; |
| 45 | + cv::projectPoints(stick_points, rvec, tvec, K, dist_coeff, line_points); |
| 46 | + for (int i = 0; i < 8; i += 2) |
| 47 | + cv::line(image, line_points[i], line_points[i + 1], cv::Scalar(0, 0, 255), 4); |
| 48 | + |
| 49 | + // Print camera position |
| 50 | + cv::Mat R; |
| 51 | + cv::Rodrigues(rvec, R); |
| 52 | + cv::Mat p = -R.t() * tvec; |
| 53 | + cv::String info = cv::format("XYZ: [%.3f, %.3f, %.3f]", p.at<double>(0), p.at<double>(1), p.at<double>(2)); |
| 54 | + cv::putText(image, info, cv::Point(5, 15), cv::FONT_HERSHEY_PLAIN, 1, cv::Scalar(0, 255, 0)); |
| 55 | + } |
| 56 | + |
| 57 | + // Show the image |
| 58 | + cv::imshow("3DV Tutorial: Pose Estimation", image); |
| 59 | + int key = cv::waitKey(1); |
| 60 | + if (key == 27) break; // 'ESC' key |
| 61 | + else if (key == 32) // 'Space' key |
| 62 | + { |
| 63 | + key = cv::waitKey(); |
| 64 | + if (key == 27) break; // 'ESC' key |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + video.release(); |
| 69 | + return 0; |
| 70 | +} |
0 commit comments