OpenCV 相机标定流程指南

  • OpenCV 相机标定流程指南
    • 前置准备
    • 标定流程
    • 结果输出与验证
    • 建议
    • 源代码

请添加图片描述

在这里插入图片描述

OpenCV 相机标定流程指南

https://docs.opencv.org/4.x/dc/dbb/tutorial_py_calibration.html
https://learnopencv.com/camera-calibration-using-opencv/

前置准备

  1. 制作标定板:生成高精度棋盘格或圆点标定板。
  2. 采集标定板图像:在不同角度、距离和光照条件下采集多张标定板图像。

OpenCV 官方标定板生成脚本使用教程
!OpenCV 官方标定板脚本下载

请添加图片描述

访问我的源代码仓库下载已经生成的矢量棋盘网格,使用打印机打印出来即可进行图像标定采集工作。

标定流程

使用 CameraCalib 类进行相机标定:

  1. 添加图像样本:将采集的标定板图像导入标定系统。
  2. 并发检测角点:利用多线程技术并行检测图像中的角点或特征点。
  3. 相机标定:基于检测到的角点,计算相机内参(焦距、主点坐标)和外参(旋转矩阵、平移向量),并优化畸变系数。

结果输出与验证

  1. 打印标定结果:输出相机内参、外参及畸变系数。
  2. 测试图像标定:使用标定结果对测试图像进行畸变校正,验证标定精度。

建议

可信误差:重投影误差应小于 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,  // 规则排列的棋盘网格 // chessboardCALIB_MARKER_CHESSBOARD_GRID,     // 额外标记的棋盘网格 // marker chessboardCALIB_SYMMETRIC_CIRCLES_GRID,     // 规则排列的圆形网格 // circlesCALIB_ASYMMETRIC_CIRCLES_GRID,    // 交错排列的圆形网格 // acirclesCALIB_PATTERN_COUNT,              // 标定模式的总数量 用于 for 循环遍历 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> &param) {// 读取一张测试图像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;  // 适当降低,以避免误检大区域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() {// 创建一个 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();

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/69734.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

没有服务器和显卡电脑如何本地化使用deepseek|如何通过API使用满血版deepseek

目录 一、前言二、使用siliconflow硅基流动 API密钥1、注册硅基流动2、创建API密钥3、下载AI客户端4、使用API密钥5、效果演示 三、使用deepseek官方API密钥1、创建API密钥2、使用API密钥3、效果演示 四、总结 一、前言 上篇文章我介绍了如何通过云服务器或者显卡电脑来本地化…

python+unity落地方案实现AI 换脸融合

先上效果再说技术结论&#xff0c;使用的是自行搭建的AI人脸融合库&#xff0c;可以离线不受限制无限次生成&#xff0c;有需要的可以后台私信python ai换脸融合。 TODO 未来的方向&#xff1a;3D人脸融合和AI数据训练 这个技术使用的是openvcinsighface&#xff0c;openvc…

windows + visual studio 2019 使用cmake 编译构建静、动态库并调用详解

环境 windows visual studio 2019 visual studio 2019创建cmake工程 1. 静态库.lib 1.1 静态库编译生成 以下是我创建的cmake工程文件结构&#xff0c;只关注高亮文件夹部分 libout 存放编译生成的.lib文件libsrc 存放编译用的源代码和头文件CMakeLists.txt 此次编译CMak…

【前端】几种常见的跨域解决方案代理的概念

几种常见的跨域解决方案&代理的概念 一、常见的跨域解决方案1. 服务端配置CORS&#xff08;Cross-Origin Resource Sharing&#xff09;&#xff1a;2. Nginx代理3. Vue CLI配置代理&#xff1a;4 .uni-app在manifest.json中配置代理来解决&#xff1a;5. 使用WebSocket通讯…

C++--iomanip库

目录 1. 设置字段宽度&#xff1a;std::setw() 2. 设置浮点数精度&#xff1a;std::setprecision() 3. 设置填充字符&#xff1a;std::setfill() 4. 控制对齐方式&#xff1a;std::left 和 std::right&#xff0c;std::internal 5. 控制进制输出&#xff1a;std::hex、std…

java项目当中使用redis

分类数据一般情况下不会做过多的修改&#xff0c;因此可以将分类数据进行缓存&#xff0c;以提高页面的加载速度。 1 使用缓存 先将首页接口获取一级分类数据缓存 步骤&#xff1a; 1、在service-product微服务中集成Spring Data Redis&#xff0c;如下所示&#xff1a; 在…

Git 分布式版本控制工具使用教程

1.关于Git 1.1 什么是Git Git是一款免费、开源的分布式版本控制工具&#xff0c;由Linux创始人Linus Torvalds于2005年开发。它被设计用来处理从很小到非常大的项目&#xff0c;速度和效率都非常高。Git允许多个开发者几乎同时处理同一个项目而不会互相干扰&#xff0c;并且在…

【Pycharm+Git+Gitlab】安装部署(粗糙版)

1、安装Git 2、安装Pycharm&#xff08;这里选择的是社区版&#xff09; 3、桌面右键打开Git Bash 1&#xff09;设置全局用户名&#xff08;准备连接的Gitlab仓库的访问用户名&#xff09; git config ---global user.name "username"2&#xff09;设置全局邮箱&…

基于java手机销售网站设计和实现(LW+源码+讲解)

专注于大学生项目实战开发,讲解,毕业答疑辅导&#xff0c;欢迎高校老师/同行前辈交流合作✌。 技术范围&#xff1a;SpringBoot、Vue、SSM、HLMT、小程序、Jsp、PHP、Nodejs、Python、爬虫、数据可视化、安卓app、大数据、物联网、机器学习等设计与开发。 主要内容&#xff1a;…

Android Camera API 介绍

一 StreamConfigurationMap 1. StreamConfigurationMap 的作用 StreamConfigurationMap 是 Android Camera2 API 中的一个核心类&#xff0c;用于描述相机设备支持的输出流配置&#xff0c;包含以下信息&#xff1a; 支持的格式与分辨率&#xff1a;例如 YUV_420_888、JPEG、…

GitHub Pages + Jekyll 博客搭建指南(静态网站搭建)

目录 &#x1f680; 静态网站及其生成工具指南&#x1f30d; 什么是静态网站&#xff1f;&#x1f4cc; 静态网站的优势⚖️ 静态网站 VS 动态网站 &#x1f680; 常见的静态网站生成器对比&#x1f6e0;️ 使用 GitHub Pages Jekyll 搭建个人博客&#x1f4cc; 1. 创建 GitHu…

wow-agent

一、什么是wow-agent&#xff1f; wow-agent致力于在代码行数和依赖库数量之间取得均衡的最小值&#xff0c;用最划算的方式帮助您在本地搭建AI Agent&#xff0c;嵌入到您的生产工作环节中 Agent 核心组件&#xff1a;模型、工具、编排层 模型-- 用于理解输入、进行推理和决…

React进行路由跳转的方法汇总

在 React 中进行路由跳转有多种方法&#xff0c;具体取决于你使用的路由库和版本。以下是常见的路由跳转方法汇总&#xff0c;主要基于 react-router-dom 库。 1. 使用 useNavigate 钩子&#xff08;适用于 react-router-dom v6&#xff09; useNavigate 是 react-router-dom…

java8、9新特性

JAVA8 Lambda 表达式 (parameters) -> expression 或 (parameters) ->{ statements; } 提供了一种更为简洁的语法&#xff0c;尤其适用于函数式接口。相比于传统的匿名内部类&#xff0c;Lambda 表达式使得代码更为紧凑&#xff0c;减少了样板代码的编写。 它允许将函…

【Elasticsearch】cumulative_cardinality

1.定义与用途 cumulative_cardinality是一种父级管道聚合&#xff08;Parent Pipeline Aggregation&#xff09;&#xff0c;用于在父级直方图&#xff08;histogram&#xff09;或日期直方图&#xff08;date_histogram&#xff09;聚合中计算累计基数。它主要用于统计在某个…

1.【线性代数】——方程组的几何解释

一 方程组的几何解释 概述举例举例一1. matrix2.row picture3.column picture 概述 三种表示方法 matrixrow picturecolumn picture 举例 举例一 { 2 x − y 0 − x 2 y 3 \begin{cases} 2x - y 0 \\ -x 2y 3 \end{cases} {2x−y0−x2y3​ 1. matrix [ 2 − 1 − 1 …

DeepSeek小白初识指南

1.什么是DeepSeek&#xff1f; DeepSeek是一个基于大语言模型&#xff08;LLM&#xff09;的智能助手&#xff0c;能够处理自然语言理解、生成、对话等任务。它广泛应用于聊天机器人、内容生成、数据分析等领域。 2.DeepSeek和OpenAI等大模型差异&#xff1f; 虽然DeepSeek和Op…

ZZNUOJ(C/C++)基础练习1091——1100(详解版)⭐

目录 1091 : 童年生活二三事&#xff08;多实例测试&#xff09; C C 1092 : 素数表(函数专题&#xff09; C C 1093 : 验证哥德巴赫猜想&#xff08;函数专题&#xff09; C C 1094 : 统计元音&#xff08;函数专题&#xff09; C C 1095 : 时间间隔&#xff08;多…

使用epoll与sqlite3进行注册登录

将 epoll 服务器 客户端拿来用 客户端&#xff1a;写一个界面&#xff0c;里面有注册登录 服务器&#xff1a;处理注册和登录逻辑&#xff0c;注册的话将注册的账号密码写入数据库&#xff0c;登录的话查询数据库中是否存在账号&#xff0c;并验证密码是否正确 额外功能&…

innovus如何分步长func和dft时钟

在Innovus工具中&#xff0c;分步处理功能时钟&#xff08;func clock&#xff09;和DFT时钟&#xff08;如扫描测试时钟&#xff09;需要结合设计模式&#xff08;Function Mode和DFT Mode&#xff09;进行约束定义、时钟树综合&#xff08;CTS&#xff09;和时序分析。跟随分…