備案期間網站要關閉嗎seo優(yōu)化查詢
- OpenCV 相機標定流程指南
- 前置準備
- 標定流程
- 結果輸出與驗證
- 建議
- 源代碼
OpenCV 相機標定流程指南
https://docs.opencv.org/4.x/dc/dbb/tutorial_py_calibration.html
https://learnopencv.com/camera-calibration-using-opencv/
前置準備
- 制作標定板:生成高精度棋盤格或圓點標定板。
- 采集標定板圖像:在不同角度、距離和光照條件下采集多張標定板圖像。
OpenCV 官方標定板生成腳本使用教程
!OpenCV 官方標定板腳本下載
訪問我的源代碼倉庫下載已經生成的矢量棋盤網格,使用打印機打印出來即可進行圖像標定采集工作。
標定流程
使用 CameraCalib 類進行相機標定:
- 添加圖像樣本:將采集的標定板圖像導入標定系統(tǒng)。
- 并發(fā)檢測角點:利用多線程技術并行檢測圖像中的角點或特征點。
- 相機標定:基于檢測到的角點,計算相機內參(焦距、主點坐標)和外參(旋轉矩陣、平移向量),并優(yōu)化畸變系數。
結果輸出與驗證
- 打印標定結果:輸出相機內參、外參及畸變系數。
- 測試圖像標定:使用標定結果對測試圖像進行畸變校正,驗證標定精度。
建議
可信誤差:重投影誤差應小于 0.5 像素,最大不超過 1.0 像素。
采集夾角要求:攝像頭與標定板平面的夾角應控制在 30°~60° 之間,避免極端角度。
[1] https://www.microsoft.com/en-us/research/publication/a-flexible-new-technique-for-camera-calibration/
源代碼
#include <opencv2/opencv.hpp>
#include <algorithm>
#include <memory>
#include <vector>
#include <string>
#include <print>
#include <iostream>class CameraCalib
{
public:// 校準模式enum class Pattern : uint32_t {CALIB_SYMMETRIC_CHESSBOARD_GRID, // 規(guī)則排列的棋盤網格 // chessboardCALIB_MARKER_CHESSBOARD_GRID, // 額外標記的棋盤網格 // marker chessboardCALIB_SYMMETRIC_CIRCLES_GRID, // 規(guī)則排列的圓形網格 // circlesCALIB_ASYMMETRIC_CIRCLES_GRID, // 交錯排列的圓形網格 // acirclesCALIB_PATTERN_COUNT, // 標定模式的總數量 用于 for 循環(huán)遍歷 std::to_underlying(Pattern::CALIB_PATTERN_COUNT);};struct CameraCalibrationResult {cv::Mat cameraMatrix; // 相機矩陣(內參數)cv::Mat distortionCoefficients; // 畸變系數double reprojectionError; // 重投影誤差(標定精度指標)std::vector<cv::Mat> rotationVectors; // 旋轉向量(外參數)std::vector<cv::Mat> translationVectors; // 平移向量(外參數)};explicit CameraCalib(int columns, int rows, double square_size /*mm*/, Pattern pattern): patternSize_(columns, rows), squareSize_(square_size), pattern_(pattern) {// 構造一個與標定板對應的真實的世界角點數據for(int y = 0; y < patternSize_.height; ++y) {for(int x = 0; x < patternSize_.width; ++x) {realCorners_.emplace_back(x * square_size, y * square_size, 0.0f);}}}void addImageSample(const cv::Mat &image) { samples_.emplace_back(image); }void addImageSample(const std::string &filename) {cv::Mat mat = cv::imread(filename, cv::IMREAD_COLOR);if(mat.empty()) {std::println(stderr, "can not load filename: {}", filename);return;}addImageSample(mat);}bool detectCorners(const cv::Mat &image, std::vector<cv::Point2f> &corners) {bool found;switch(pattern_) {using enum Pattern;case CALIB_SYMMETRIC_CHESSBOARD_GRID: detectSymmetricChessboardGrid(image, corners, found); break;case CALIB_MARKER_CHESSBOARD_GRID: detectMarkerChessboardGrid(image, corners, found); break;case CALIB_SYMMETRIC_CIRCLES_GRID: detectSymmetricCirclesGrid(image, corners, found); break;case CALIB_ASYMMETRIC_CIRCLES_GRID: detectAsymmetricCirclesGrid(image, corners, found); break;default: break;}return found;}std::vector<std::vector<cv::Point2f>> detect() {std::vector<std::vector<cv::Point2f>> detectedCornerPoints;std::mutex mtx; // 使用 mutex 來保護共享資源std::atomic<int> count;std::for_each(samples_.cbegin(), samples_.cend(), [&](const cv::Mat &image) {std::vector<cv::Point2f> corners;bool found = detectCorners(image, corners);if(found) {count++;std::lock_guard<std::mutex> lock(mtx); // 使用 lock_guard 來保護共享資源detectedCornerPoints.push_back(corners);}});std::println("Detection successful: {} corners, total points: {}", int(count), detectedCornerPoints.size());return detectedCornerPoints;}std::unique_ptr<CameraCalibrationResult> calib(std::vector<std::vector<cv::Point2f>> detectedCornerPoints, int width, int height) {// 準備真實角點的位置std::vector<std::vector<cv::Point3f>> realCornerPoints;for(size_t i = 0; i < detectedCornerPoints.size(); ++i) {realCornerPoints.emplace_back(realCorners_);}cv::Size imageSize(width, height);// 初始化相機矩陣和畸變系數cv::Mat cameraMatrix = cv::Mat::eye(3, 3, CV_64F);cv::Mat distCoeffs = cv::Mat::zeros(5, 1, CV_64F);std::vector<cv::Mat> rvecs, tvecs;// 進行相機標定double reproError = cv::calibrateCamera(realCornerPoints, detectedCornerPoints, imageSize, cameraMatrix, distCoeffs, rvecs, tvecs, cv::CALIB_FIX_K1 + cv::CALIB_FIX_K2 + cv::CALIB_FIX_K3 + cv::CALIB_FIX_K4 + cv::CALIB_FIX_K5);// 將標定結果存儲到結構體中auto result = std::make_unique<CameraCalibrationResult>();result->cameraMatrix = cameraMatrix;result->distortionCoefficients = distCoeffs;result->reprojectionError = reproError;result->rotationVectors = rvecs;result->translationVectors = tvecs;return result;}// 打印標定結果void print(const std::unique_ptr<CameraCalibrationResult> &result) {std::cout << "重投影誤差: " << result->reprojectionError << std::endl;std::cout << "相機矩陣:\n" << result->cameraMatrix << std::endl;std::cout << "畸變系數:\n" << result->distortionCoefficients << std::endl;}// 進行畸變校正測試void test(const std::string &filename, const std::unique_ptr<CameraCalibrationResult> ¶m) {// 讀取一張測試圖像cv::Mat image = cv::imread(filename);if(image.empty()) {std::println("can not load filename");return;}cv::Mat undistortedImage;cv::undistort(image, undistortedImage, param->cameraMatrix, param->distortionCoefficients);// 顯示原圖和校準后的圖cv::namedWindow("Original Image", cv::WINDOW_NORMAL);cv::namedWindow("Undistorted Image", cv::WINDOW_NORMAL);cv::imshow("Original Image", image);cv::imshow("Undistorted Image", undistortedImage);// 等待用戶輸入任意鍵cv::waitKey(0);}private:void dbgView(const cv::Mat &image, const std::vector<cv::Point2f> &corners, bool &found) {if(!found) {std::println("Cannot find corners in the image");}// Debug and view detected corner points in imagesif constexpr(false) {cv::drawChessboardCorners(image, patternSize_, corners, found);cv::namedWindow("detectCorners", cv::WINDOW_NORMAL);cv::imshow("detectCorners", image);cv::waitKey(0);cv::destroyAllWindows();}}void detectSymmetricChessboardGrid(const cv::Mat &image, std::vector<cv::Point2f> &image_corners, bool &found) {if(found = cv::findChessboardCorners(image, patternSize_, image_corners); found) {cv::Mat gray;cv::cvtColor(image, gray, cv::COLOR_BGR2GRAY);cv::cornerSubPix(gray, image_corners, cv::Size(11, 11), cv::Size(-1, -1), cv::TermCriteria(cv::TermCriteria::EPS + cv::TermCriteria::COUNT, 30, 0.01));dbgView(image, image_corners, found);}}void detectMarkerChessboardGrid(const cv::Mat &image, std::vector<cv::Point2f> &image_corners, bool &found) {if(found = cv::findChessboardCornersSB(image, patternSize_, image_corners); found) {dbgView(image, image_corners, found);}}void detectSymmetricCirclesGrid(const cv::Mat &image, std::vector<cv::Point2f> &image_corners, bool &found) {if(found = cv::findCirclesGrid(image, patternSize_, image_corners, cv::CALIB_CB_SYMMETRIC_GRID); found) {cv::Mat gray;cv::cvtColor(image, gray, cv::COLOR_BGR2GRAY);cv::cornerSubPix(gray, image_corners, cv::Size(11, 11), cv::Size(-1, -1), cv::TermCriteria(cv::TermCriteria::EPS + cv::TermCriteria::COUNT, 30, 0.01));dbgView(image, image_corners, found);}}void detectAsymmetricCirclesGrid(const cv::Mat &image, std::vector<cv::Point2f> &image_corners, bool &found) {cv::SimpleBlobDetector::Params params;params.minThreshold = 8;params.maxThreshold = 255;params.filterByArea = true;params.minArea = 50; // 適當降低,以便檢測小圓點params.maxArea = 5000; // 適當降低,以避免誤檢大區(qū)域params.minDistBetweenBlobs = 10; // 調小以適應緊密排列的圓點params.filterByCircularity = false; // 允許更圓的形狀params.minCircularity = 0.7; // 只有接近圓的目標才被識別params.filterByConvexity = true;params.minConvexity = 0.8; // 只允許較凸的形狀params.filterByInertia = true;params.minInertiaRatio = 0.1; // 適應不同形狀params.filterByColor = false; // 關閉顏色過濾,避免黑白檢測問題auto blobDetector = cv::SimpleBlobDetector::create(params);if(found = cv::findCirclesGrid(image, patternSize_, image_corners, cv::CALIB_CB_ASYMMETRIC_GRID | cv::CALIB_CB_CLUSTERING, blobDetector); found) {cv::Mat gray;cv::cvtColor(image, gray, cv::COLOR_BGR2GRAY);cv::cornerSubPix(gray, image_corners, cv::Size(11, 11), cv::Size(-1, -1), cv::TermCriteria(cv::TermCriteria::EPS + cv::TermCriteria::COUNT, 30, 0.01));dbgView(image, image_corners, found);}}private:cv::Size patternSize_;double squareSize_;Pattern pattern_;std::vector<cv::Point3f> realCorners_;std::vector<cv::Mat> samples_;
};// 測試函數
static void test_CameraCalib() {// 創(chuàng)建一個 CameraCalib 對象,指定標定板大小、每個方格的邊長和校準模式CameraCalib calib(14, 9, 12.1, CameraCalib::Pattern::CALIB_MARKER_CHESSBOARD_GRID);// 加載圖像樣本std::vector<cv::String> result;cv::glob("calibration_images/*.png", result, false);for (auto &&filename : result) {calib.addImageSample(filename);}// 檢測角點auto detectedCornerPoints = calib.detect();// 進行相機標定std::string filename = "calibration_images/checkerboard_radon.png";cv::Mat image = cv::imread(filename);if (image.empty()) {std::println("can not load image");return;}auto param = calib.calib(detectedCornerPoints, image.cols, image.cols);// 打印標定結果calib.print(param);// 測試函數calib.test(filename, param);
}
運行測試函數,輸出結果如下所示:
Detection successful: 2 corners, total points: 2
重投影誤差: 0.0373256
相機矩陣:
[483030.3184975122, 0, 1182.462802265994;0, 483084.13533141, 1180.358683128085;0, 0, 1]
畸變系數:
[0;0;-0.002454905573938355;9.349667940808669e-05;0]
// 保存標定結果
cv::FileStorage fs("calibration_result.yml", cv::FileStorage::WRITE);
fs << "camera_matrix" << result.cameraMatrix;
fs << "distortion_coefficients" << result.distCoeffs;
fs << "image_size" << result.imageSize;
fs.release();